当前位置: 首页 > 编程笔记 >

Fresco加载手机图片墙

尉迟轶
2023-03-14
本文向大家介绍Fresco加载手机图片墙,包括了Fresco加载手机图片墙的使用技巧和注意事项,需要的朋友参考一下

本文实例为大家分享了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);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • FaceBook推出的Android图片加载库-Fresco > 原文链接:Introducing Fresco: A new image library for Android 作者 : tyrone Nicholas 译者 : ZhaoKaiQiang 校对者: Chaossss 校对者: bboyfeiyu 校对者: BillionWang 状态 : 完成 在Android设备上面,快速高效

  • 我正在尝试加载图像使用壁画库,但我有一个奇怪的问题。 png图像正在加载,但我加载的是jpg图像我使用的壁画版本是2.0.0,我尝试了一切无效缓存,改变壁画版本,清除数据,重建项目。但对我来说没有任何意义。 不工作 //初始化壁画 您可以在下面看到所有的日志:

  • 本文向大家介绍详解Android之图片加载框架Fresco基本使用(一),包括了详解Android之图片加载框架Fresco基本使用(一)的使用技巧和注意事项,需要的朋友参考一下 PS:Fresco这个框架出的有一阵子了,也是现在非常火的一款图片加载框架.听说内部实现的挺牛逼的,虽然自己还没研究原理.不过先学了一下基本的功能,感受了一下这个框架的强大之处.本篇只说一下在xml中设置属性的相关用法.

  • 使用指南 组件介绍 Lazyload 是 Vue 指令,使用前需要对指令进行注册 引入方式 import Vue from 'vue'; import Lazyload from 'vue-lazyload'; Vue.use(Lazyload, { lazyComponent: true, //失败时显示 (可以自定义) error: 'https://nuofe.nnte

  • DWZ 火山引擎图片加载插件 dwz-BytedanceImageX 字节跳动火山引擎图片加载 SDK 插件【dcloud】 功能介绍 uni 原生插件市场中查看 本插件是针对字节跳动火山引擎图片加载 SDK 的封装: 支持现代图片格式 heic、heif、webp、heif、avif 在 HTML5 混合 App 中展示 支持用户感知失败率、解码失败率、解码耗时、超分成功率、超分耗时、排队耗时、

  • DWZ 火山引擎图片加载插件 dwzBytedanceImageX 字节跳动火山引擎图片加载 SDK 插件【apicloud】 功能介绍 uni 原生插件市场中查看 本插件是针对字节跳动火山引擎图片加载 SDK 的封装: 支持现代图片格式 heic、heif、webp、heif、avif 在 HTML5 混合 App 中展示 支持用户感知失败率、解码失败率、解码耗时、超分成功率、超分耗时、排队耗时