亲宝软件园·资讯

展开

Fresco加载手机图片墙 Fresco加载手机图片墙

Stagefright 人气:0
想了解Fresco加载手机图片墙的相关内容吗,Stagefright在本文为您仔细讲解Fresco加载手机图片墙的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Fresco加载手机图片墙,Fresco加载图片墙,Fresco加载手机,下面大家一起来学习吧。

//网络权限

 <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);
  }
}

加载全部内容

相关教程
猜你喜欢
用户评论