温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Fresco加载手机图片墙

发布时间:2020-10-12 17:21:40 来源:脚本之家 阅读:134 作者:Stagefright 栏目:移动开发

本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下

//网络权限

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.INTERNET" />

//添加依赖

compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
compile 'com.facebook.fresco:fresco:0.12.0'
testCompile 'junit:junit:4.12'

布局文件

activity_main

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" tools:context="com.spl.dell.fresco_photowall.MainActivity">

  <android.support.v7.widget.RecyclerView
    android:id="@+id/main_recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

activity_zoom_image

<RelativeLayout
  xmlns:fresco="http://schemas.android.com/apk/res-auto"
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.spl.dell.fresco_photowall.ZoomImageActivity">

  <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/img_sdv"
    android:background="@mipmap/ic_launcher"
    android:layout_centerInParent="true"
    android:layout_width="400dp"
    android:layout_height="400dp" />

</RelativeLayout>

fresco_item

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:fresco="http://schemas.android.com/apk/res-auto"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">

  <com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/sdv"
    android:layout_marginLeft="35dp"
    android:layout_width="100dp"
    android:layout_height="100dp"
    fresco:placeholderImage="@drawable/icon_placeholder"
    fresco:placeholderImageScaleType="fitCenter"

    fresco:progressBarImage="@drawable/icon_progress_bar"
    fresco:progressBarImageScaleType="centerInside"
    fresco:progressBarAutoRotateInterval="5000"

    fresco:failureImage="@drawable/icon_failure"
    fresco:failureImageScaleType="centerInside"

    fresco:retryImage="@drawable/icon_retry"
    fresco:retryImageScaleType="centerCrop"
    />

</LinearLayout>

MyViewHolder

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.spl.dell.fresco_photowall.R;

/**
 * Created by dell on 2018/3/8.
 */

public class MyViewHolder extends RecyclerView.ViewHolder {

  public SimpleDraweeView simpleDraweeView;

  public MyViewHolder(View itemView) {
    super(itemView);
    simpleDraweeView = itemView.findViewById(R.id.sdv);
  }
}

MyRecyclerViewAdapter

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.spl.dell.fresco_photowall.R;
import com.spl.dell.fresco_photowall.recyclerview_adapter.viewholder.MyViewHolder;

import java.io.File;
import java.util.List;

/**
 * Created by dell on 2018/3/8.
 */

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyViewHolder> {

  Context context;
  List<String> list;

  public MyRecyclerViewAdapter(Context context, List<String> list) {
    this.context = context;
    this.list = list;
  }

  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = View.inflate(context, R.layout.fresco_item, null);
    MyViewHolder viewHolder = new MyViewHolder(view);
    return viewHolder;
  }

  @Override
  public void onBindViewHolder(MyViewHolder holder, final int position) {
    Uri uri = Uri.fromFile(new File(list.get(position)));
    holder.simpleDraweeView.setImageURI(uri);


    holder.simpleDraweeView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        onRecyclerItemClick.onItemClicklistener(position);
      }
    });
  }

  public void setOnItemClick(onRecyclerItemClick onRecyclerItemClick){
    this.onRecyclerItemClick = onRecyclerItemClick;
  }

  @Override
  public int getItemCount() {
    return list.size();
  }
  onRecyclerItemClick onRecyclerItemClick;
  public interface onRecyclerItemClick{
    void onItemClicklistener(int position);
  }

}

MyApp

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;

/**
 * Created by dell on 2018/3/8.
 */

public class MyApp extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    Fresco.initialize(this);
  }
}

ZoomImageActivity

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.facebook.drawee.view.SimpleDraweeView;

import java.io.File;

public class ZoomImageActivity extends AppCompatActivity {

  private SimpleDraweeView sdv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_zoom_image);
    Intent intent = getIntent();
    String imgurl = intent.getStringExtra("imgurl");
    Uri uri = Uri.fromFile(new File(imgurl));
    sdv = (SimpleDraweeView) findViewById(R.id.img_sdv);
    sdv.setImageURI(uri);
  }
}

MainActivity

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;

import com.spl.dell.fresco_photowall.recyclerview_adapter.MyRecyclerViewAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

  private RecyclerView recycler;
  private List<String> docs = new ArrayList<>();

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().hide();
    setContentView(R.layout.activity_main);
    initView();
    //recyclerview的数据源需要从手机相册中获取
    initData();
  }

  private void initData() {

    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
    while(cursor.moveToNext()){
      String paths = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
      File file = new File(paths);
      String absolutePath = file.getAbsolutePath();
      docs.add(absolutePath);
    }
    cursor.close();

//    for(int i = 0;i<docs.size();i++){
//      Log.d("TAG",docs.get(i).toString());
//    }

    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(MainActivity.this,docs);
        recycler.setAdapter(adapter);

        adapter.setOnItemClick(new MyRecyclerViewAdapter.onRecyclerItemClick() {
          @Override
          public void onItemClicklistener(int position) {
            Intent intent = new Intent(MainActivity.this,ZoomImageActivity.class);
            intent.putExtra("imgurl",docs.get(position));
            startActivity(intent);
          }
        });
      }
    });
  }

  private void initView() {
    recycler = (RecyclerView) findViewById(R.id.main_recycler);
    RecyclerView.LayoutManager manager = new GridLayoutManager(MainActivity.this,3 ,OrientationHelper.VERTICAL,false);
    recycler.setLayoutManager(manager);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI