Fresco网络图片加载

Fresco

内存管理

Fresco 的最大亮点在于它的内存管理。解压后的图片,即 Android 中的 Bitmap ,占用大量的内存,在 Android 5.0以下系统中,这会显著地引发界面卡顿。而使用 Fresco 将很好地解决这个问题,Fresco 会将图片放到一个特别的内存区域,当图片不再显示的时候,占用的内存会自动被释放,这会使得 APP 更流畅,减少因图片内存占用而引发的 OOM。当 APP 包含的图片较多时,这个效果尤其明显。

图像

Fresco 支持图像的渐进式呈现,渐进式的图片格式先呈现大致的图片轮廓,然后随着图片下载的继续,逐渐呈现清晰的图片,这在低网速情况下浏览图片十分有帮助,可以带来更好地用户体验。另外,Fresco 支持加载 gif 图,支持 WebP 格式。
 
 

如果了解更多: Fresco官方文档 ; 同时, 建议在看文档的时候优先选择阅读英文文档, 因为中文版文档可能有滞后的情况,这样会避免很多不必要的麻烦。另外,当有问题产生时, 建议去其Fresco Github Issues去进行翻阅查找, 此处汇聚了许多Fresco使用和问题的反馈及解答,往往可能会解决你的一些基本疑惑,甚至,你自己Open New Issue亦无不可。

中文文档

    http://www.fresco-cn.org/ ; 如果真心读不懂英文或者不想读, 那么这里,你可以去看看。

导入官方示例

    http://www.cnblogs.com/stay/p/4398432.html; 简单看了下,还不错, 讲的相对详细,我并未细看。因为其实导入和编译项目该是开发的基本功吧,(*^__^*) 嘻嘻……

简单使用

    http://blog.csdn.net/y1scp/article/details/49245535; 非常详细的使用教程了吧,作者还是比较有心的,点个赞。

 

 下面简单使用:

1、配置环境

        使用Fresco需要先在build.gradle中导入依赖:

  // Fresco所需依赖
    compile 'com.facebook.fresco:fresco:0.12.0'
    // 在 API < 14 上的机器支持 WebP 时,需要添加
    compile 'com.facebook.fresco:animated-base-support:0.12.0'
    // 支持 GIF 动图,需要添加
    compile 'com.facebook.fresco:animated-gif:0.12.0'
    // 支持 WebP (静态图+动图),需要添加
    compile 'com.facebook.fresco:animated-webp:0.12.0'
    compile 'com.facebook.fresco:webps

      2、在项目的Application中初始化Fresco:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        // 初始化Fresco
        Fresco.initialize(this);
    }
}

 

      最后别忘了在AndroidManifest.xml文件中为Application添加name属性:

    <application
        android:name=".MyApplication"
        ......
    </application>

2、SimpleDraweeView

2.1、XML属性

        Fresco为我们提供了一个SimpleDraweeView控件,我们可以直接在这个控件中加载图片。Fresco在这个控件中集成了很多属性,简单实用。以下是SimpleDraweeView中的常用属性:

总结:

XML属性    意义
fadeDuration    淡入淡出动画持续时间(单位:毫秒ms)
actualImageScaleType    实际图像的缩放类型
placeholderImage    占位图
placeholderImageScaleType    占位图的缩放类型
progressBarImage    进度图
progressBarImageScaleType    进度图的缩放类型
progressBarAutoRotateInterval    进度图自动旋转间隔时间(单位:毫秒ms)
failureImage    失败图
failureImageScaleType    失败图的缩放类型
retryImage    重试图
retryImageScaleType    重试图的缩放类型
backgroundImage    背景图
overlayImage    叠加图
pressedStateOverlayImage    按压状态下所显示的叠加图
roundAsCircle    设置为圆形图
roundedCornerRadius    圆角半径
roundTopLeft    左上角是否为圆角
roundTopRight    右上角是否为圆角
roundBottomLeft    左下角是否为圆角
roundBottomRight    右下角是否为圆角
roundingBorderWidth    圆形或者圆角图边框的宽度
roundingBorderColor    圆形或者圆角图边框的颜色
roundWithOverlayColor    圆形或者圆角图底下的叠加颜色(只能设置颜色)
viewAspectRatio    控件纵横比

XML 例如:

  <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/id_main_sdv_sdv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        fresco:actualImageScaleType="focusCrop"
        fresco:fadeDuration="3000"
        fresco:failureImage="@mipmap/ic_launcher"
        fresco:failureImageScaleType="centerInside"
        fresco:placeholderImage="@mipmap/ic_launcher"
        fresco:placeholderImageScaleType="fitCenter"
        fresco:progressBarAutoRotateInterval="1000"
        fresco:progressBarImage="@mipmap/ic_launcher"
        fresco:progressBarImageScaleType="centerInside"
        fresco:retryImage="@mipmap/ic_launcher"
        fresco:retryImageScaleType="centerCrop"
        fresco:roundAsCircle="false"
        fresco:viewAspectRatio="1.6" />

 

2.2、注意事项

(1)SimpleDraweeView控件的宽高不能设置为wrap_content,只能是match_parent、具体值或使用viewAspectRatio属性设置宽高比。

(2)如果在一个页面中加载多张图片,不要将SimpleDraweeView直接放在ScollView中,建议使用RecyclerView、ListView或GridView,因为ScrollView中的SimpleDraweeView会持有内存直到Activity或Fragment销毁。

(3)使用SimpleDraweeView时,不要使用imageView中有、View中没有的属性。
 

3、框架使用

3.1、简单使用

        像TextView、Button等其他控件一样后去到SimpleDraweeView,然后调用如下代码即可加载网络图片:

SimpleDraweeView sdv = (SimpleDraweeView) findViewById(R.id.id_main_sdv_sdv);
sdv.setImageURI("http://xxxx.jpg");

 

3.2、JAVA代码设置属性

        我们可以通过GenericDraweeHierarchy,在JAVA代码中动态的设置SimpleDraweeView控件的属性。需要注意的是,如果在JAVA代码中设置了属性,那么XML文件中设置的属性就都无效了。一个实例代码如下:

        // 代码设置SimpleDraweeView的属性(会覆盖XML设置的所有属性,即在XML中有在这里没有的属性都会失效)
        // 注意:一个GenericDraweeHierarchy是不能被多个SimpleDraweeView共用的
        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(getResources())
                .setFadeDuration(3000)
                .setPlaceholderImage(R.mipmap.ic_launcher)
                .setPlaceholderImageScaleType(ScalingUtils.ScaleType.FIT_XY)
                .setProgressBarImage(new ProgressBarDrawable()) // 显示进度条(Fresco自带的进度条)
                .build();
        // 设置图片圆角
        RoundingParams roundingParams = new RoundingParams();
        roundingParams.setRoundAsCircle(false); // 不将图片剪切成圆形
        roundingParams.setCornersRadius(200);
        hierarchy.setRoundingParams(roundingParams);
        // 为SimpleDraweeView设置属性
        sdv.setHierarchy(hierarchy);

        注意:不能把同一个GenericDraweeHierarchy对象设置给多个SimpleDraweeView!

 

3.3、设置下载事件

 

        sdv.setController(Fresco.newDraweeControllerBuilder()
                .setControllerListener(new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                        // 所有图片都加载成功时触发的方法
                        int width = imageInfo.getWidth();
                        int height = imageInfo.getHeight();
                        QualityInfo qualityInfo = imageInfo.getQualityInfo();
                        int quality = qualityInfo.getQuality();
                        boolean isOfFullQuality = qualityInfo.isOfFullQuality();
                        boolean isOfGoodEnoughQuality = qualityInfo.isOfGoodEnoughQuality();
                    }

                    @Override
                    public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
                        // 加载渐进式图片时回调的方法
                    }

                    @Override
                    public void onFailure(String id, Throwable throwable) {
                        // 加载图片失败时回调的方法
                    }
                })
                .setUri("http://xxx.jpg")
                .build());

3.4、渐进式图片

        渐进式图片是一种支持图片从模糊到清晰的加载模式。代码如下:

 

                sdv.setController(Fresco.newDraweeControllerBuilder()
                        .setImageRequest(
                                ImageRequestBuilder.newBuilderWithSource(
                                                  Uri.parse("http://XXXX .jpg"))
                                        .setProgressiveRenderingEnabled(true)
                                        .build())
                        .setOldController(sdv.getController())
                        .build());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值