Fresco的使用
来源:互联网 发布:布朗熊玩偶淘宝 编辑:程序博客网 时间:2024/06/03 13:36
Fresco 是 Facebook 开源的图片缓存,主要特点包括:
(1) 两个内存缓存加上 Native 缓存构成了三级缓存
(2) 支持流式,可以类似网页上模糊渐进式显示图片
(3) 对多帧动画图片支持更好,如 Gif、WebP
鉴于 Fresco 还没发布正式的 1.0 版本,同时一直没太多时间熟悉 Fresco 源码,后面对比不包括 Fresco,以后有时间再加入对比
一.首先加入依赖
//使用+代表依赖库的最新版本 //fresco compile 'com.facebook.fresco:fresco:+' //支持gif compile 'com.facebook.fresco:animated-gif:+'
二.然后在添加布局
<?xml version="1.0" encoding="utf-8"?><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.bwie.fresco.MainActivity"> <!--宽高要固定格式--> <!--fresco:failureImage=""加载失败的时候图片 fresco:progressBarImage="@drawable/icon_progress_bar" 加载过程中的图片 fresco:roundedCornerRadius="20dp" 边角圆形弧度 fresco:roundAsCircle="true" 圆形图片 fresco:roundingBorderWidth="3dp" 图片表框宽度 fresco:roundingBorderColor="#FFEE00" 图片边框颜色 --> <com.facebook.drawee.view.SimpleDraweeView android:layout_centerInParent="true" android:id="@+id/simple_drawee_view" android:layout_width="300dp" android:layout_height="wrap_content" fresco:viewAspectRatio="1" fresco:failureImage="@drawable/icon_failure" fresco:progressBarImage="@drawable/icon_progress_bar" fresco:progressBarAutoRotateInterval="1000" fresco:retryImageScaleType="centerInside" fresco:roundedCornerRadius="20dp" fresco:roundTopLeft="false" fresco:roundAsCircle="true" fresco:roundingBorderWidth="3dp" fresco:roundingBorderColor="#FFEE00" /></RelativeLayout>
三.代码进行调试
package com.bwie.fresco;import android.graphics.drawable.Animatable;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.Toast;import com.facebook.drawee.backends.pipeline.Fresco;import com.facebook.drawee.controller.AbstractDraweeController;import com.facebook.drawee.controller.BaseControllerListener;import com.facebook.drawee.view.SimpleDraweeView;import com.facebook.imagepipeline.image.ImageInfo;import com.facebook.imagepipeline.request.ImageRequest;import com.facebook.imagepipeline.request.ImageRequestBuilder;public class MainActivity extends AppCompatActivity { private SimpleDraweeView simple_drawee_view; public static final String mImageUrl = "http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg"; public static final String mErrorUrl = "http://pic39.nipic.com/20140226/18071023_1643006080dddd00_2.jpg"; public static final String mGifUrl = "http://img.zcool.cn/community/0139505792e5fc0000018c1bbb7271.gif"; public static final String mJpegUrl = "http://attach.foyuan.net/portal/201308/03/09/2013080309223742492.jpg"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //初始化Fresco Fresco.initialize(this); setContentView(R.layout.activity_main); Uri uri = Uri.parse(mGifUrl); simple_drawee_view = (SimpleDraweeView) findViewById(R.id.simple_drawee_view); //第一种设置图片路径的地址 //simple_drawee_view.setImageURI(uri); BaseControllerListener<ImageInfo> baseControllerListener = new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) { animatable.start(); } @Override public void onFailure(String id, Throwable throwable) { Toast.makeText(MainActivity.this, "加载失败", Toast.LENGTH_SHORT).show(); } }; //也可以控制图片请求的一些特性 ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri) //设置支持jpeg渐进式展示(从模糊到清晰) .setProgressiveRenderingEnabled(true) .build(); //控制图片加载的一些特性 AbstractDraweeController controller = Fresco.newDraweeControllerBuilder() .setImageRequest(imageRequest) //第二种设置图片地址 .setUri(uri) //设置可以重试 (重试4次) .setTapToRetryEnabled(true) //设置自动播放 .setAutoPlayAnimations(true) //监听图片加载 .setControllerListener(baseControllerListener) .setOldController(simple_drawee_view.getController()) .build(); simple_drawee_view.setController(controller); }}
阅读全文