GithubHelp home page GithubHelp logo

fresco-helper's Introduction

fresco-helper

Android上图片加载库Fresco的使用帮助类

Fresco在GitHub上的项目地址:https://github.com/facebook/fresco

目前是基于Fresco 0.14.1这个版本。

在使用到fresco-helper库的Module中的build.gradle文件里,添加以下依赖:

 allprojects {
    repositories {
        jcenter()

        maven {
            url 'https://dl.bintray.com/hpdx/maven/'
        }
    }
 }

 compile 'com.facebook.fresco.helper:fresco-helper:1.1.8'
 compile 'com.facebook.fresco.helper:fresco-photoview:1.1.6'

目前对以下需求进行了封装

  • ImagePipeline对象的初始化配置,默认配置了两个磁盘缓存

  • 从网络加载图片并显示

  • 从本地文件加载图片并显示

  • 从本地res中加载图片并显示

  • 加载并显示gif格式的图片

  • 加载并显示webp格式的图片

  • 只知道要显示图片的高或者宽的值,另一个值可以从设置的比例得出

  • 解码指定尺寸大小的图片,图片数据来源支持网络、本地文件和本地res中的,并显示

  • 支持不知道图片原始尺寸,也不知道要显示的图片尺寸,加载并显示

  • 根据url下载图片(未解码),并保存到本地指定的路径

  • 根据url获取已解码的对象(bitmap)

  • 图片的高斯模糊处理

  • 根据url下载图片,并对其进行高斯模糊处理,之后显示为任意View的背景

  • 查找一张图片在已解码的缓存中是否存在

  • 查找一张图片在磁盘缓存中是否存在,若配有两个磁盘缓存,则只要其中一个存在,就会返回true

  • 查找一张图片在磁盘缓存中是否存在,可以指定是哪个磁盘缓存

  • 从内存缓存中移除指定图片的缓存

  • 从磁盘缓存中移除指定图片的缓存

  • 移除指定图片的所有缓存(包括内存+磁盘)

  • 清空所有内存缓存

  • 清空所有磁盘缓存,若你配置有两个磁盘缓存,则两个都会清除

  • 清除所有缓存(包括内存+磁盘)

  • 当前网络请求是否处于暂停状态

  • 暂停所有图片的网络请求

  • 恢复所有图片的网络请求

##基于Fresco实现的大图浏览器

  • 点击照片墙中的缩略图,打开和关闭效果类似微信朋友圈的图片查看效果
  • 支持双击放大效果
  • 支持单击关闭大图浏览
  • 支持手势缩放功能

使用:

初始化

 Phoenix.init(this); // Context

从网络加载一张图片

String url = "http://ww3.sinaimg.cn/large/610dc034jw1f6m4aj83g9j20zk1hcww3.jpg";
SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView).load(url);

从本地加载一张图片

String filePath = "/sdcard/image/test.jpg";
SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView).load(filePath);

从res下面加载一张图片

SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView).load(R.drawable.meizi);

在写布局文件xml时,我不知道要显示的图片尺寸,需要根据业务逻辑动态的设置要显示的图片的大小,可以像下面这样写:

String url = "http://ww3.sinaimg.cn/large/610dc034jw1f6m4aj83g9j20zk1hcww3.jpg";
SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView)
       .setWidth(100)
       .setHeight(100)
       .load(url);

只知道要显示图片的高或者宽的值,另一个值可以从设置的比例得出

String url = "http://ww3.sinaimg.cn/large/610dc034jw1f6m4aj83g9j20zk1hcww3.jpg";
SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView)
       .setWidth(100)
       .setAspectRatio(0.6f) // w/h = 6/10
       .load(url);

图片的高斯模糊处理

String url = "http://ww3.sinaimg.cn/large/610dc034jw1f6m4aj83g9j20zk1hcww3.jpg";
SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView)
       .setNeedBlur(true)
       .load(url);

加载并显示gif格式的图片

String url = "http://img4.178.com/acg1/201506/227753817857/227754566617.gif";
SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView)
       .load(url);

加载并显示webp格式的图片

SimpleDraweeView simpleDraweeView = (SimpleDraweeView)findViewById(R.id.sdv_1);
Phoenix.with(simpleDraweeView)
        .load(R.drawable.meizi_webp);

将图片资源预加载到磁盘缓存

String url = "http://ww1.sinaimg.cn/large/610dc034jw1fahy9m7xw0j20u00u042l.jpg";
Phoenix.prefetchToDiskCache(url);

从网络下载一张图片

String url = "http://feed.chujianapp.com/20161108/452ab5752287a99a1b5387e2cd849006.jpg@1080w";
String filePath = "";
Phoenix.with(url)
       .setResult(new IDownloadResult(filePath) {
           @Override
           public void onResult(String filePath) {
               MLog.i("filePath = " + filePath);

           }
       })
       .download();

......

###大图浏览器 详细细节请查看PhotoWallActivity中的示例

带动画的效果打开方式(多图)

ArrayList<PhotoInfo> photos = null;
PictureBrowse.newBuilder(PhotoWallActivity.this)
             .setLayoutManager(mLayoutManager)
             .setPhotoList(photos)
             .setCurrentPosition(position)
             .enabledAnimation(true)
             .start();

无动画效果的打开方式(多图)

 ArrayList<PhotoInfo> photos = null;
 PictureBrowse.newBuilder(PhotoWallActivity.this)
              .setPhotoList(photos)
              .setCurrentPosition(position)
              .start();

带动画效果的打开方式(只有一张图片)

String originalUrl = photos.get(position).originalUrl;
PictureBrowse.newBuilder(PhotoWallActivity.this)
             .setThumbnailView(view)
             .setOriginalUrl(originalUrl)
             .enabledAnimation(true)
             .start();

无动画效果的打开方式(只有一张图片)

String originalUrl = photos.get(position).originalUrl;
PictureBrowse.newBuilder(PhotoWallActivity.this)
             .setOriginalUrl(originalUrl)
             .start();

示例效果如下:

常见的各种效果

在进行高斯模糊前的照片

对照片进行高斯模糊后的效果

从网络加载的图片墙

点击图片墙中的照片后,打开的浏览大图界面

下载示例Apk

更详细的讲解,请查阅我的这篇博客:Android图片加载神器之Fresco,基于各种使用场景的讲解。

在使用过程中有满足不了的使用场景或遇到bug,欢迎提issuse ! 若你觉得还不错,请点Star, 谢谢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.