Fresco加载手机图片墙

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

成都创新互联是一家专业提供凤山企业网站建设,专注与网站设计、网站制作、H5高端网站建设、小程序制作等业务。10年已为凤山众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

//网络权限

 
 

//添加依赖

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



  



activity_zoom_image



  



fresco_item



  



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 {

  Context context;
  List list;

  public MyRecyclerViewAdapter(Context context, List 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 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

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


分享标题:Fresco加载手机图片墙
网站URL:http://myzitong.com/article/ihdiig.html