美文网首页
Fresco的使用

Fresco的使用

作者: NullPoint3Exce | 来源:发表于2019-02-14 15:33 被阅读0次

    1.简介

    2.依赖--网络权限(省略)

    //必须添加
      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:webpsupport:0.12.0'
    
      // 仅支持 WebP 静态图,需要添加(选装)
      compile 'com.facebook.fresco:webpsupport:0.12.0'
    

    3.Application 声明,同时在清单文件里配置Application

    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            Fresco.initialize(this);
        }
    }
    

    4.在xml布局文件中, 加入命名空间:

     
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:fresco="http://schemas.android.com/apk/res-auto"  // 加入命名空间
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    

    5.布局里添加图片组件

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/my_image_view"
        android:layout_width="130dp"
        android:layout_height="130dp"
        fresco:placeholderImage="@drawable/my_drawable"
      />
    

    6.使用

    Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/logo.png");
    SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view);
    draweeView.setImageURI(uri);
    

    7.Image Pipeline介绍
    Image pipeline 负责完成加载图像,变成Android设备可呈现的形式所要做的每个事情。(简单讲就是可以支持更多功能)

    • Pipeline配置-在Application里初始化config
    ImagePipelineConfig config = ImagePipelineConfig.newBuilder(context)
        .setBitmapMemoryCacheParamsSupplier(bitmapCacheParamsSupplier)
        .setCacheKeyFactory(cacheKeyFactory)
        .setDownsampleEnabled(true)
        .setWebpSupportEnabled(true)
        .setEncodedMemoryCacheParamsSupplier(encodedCacheParamsSupplier)
        .setExecutorSupplier(executorSupplier)
        .setImageCacheStatsTracker(imageCacheStatsTracker)
        .setMainDiskCacheConfig(mainDiskCacheConfig)
        .setMemoryTrimmableRegistry(memoryTrimmableRegistry)
        .setNetworkFetchProducer(networkFetchProducer)
        .setPoolFactory(poolFactory)
        .setProgressiveJpegConfig(progressiveJpegConfig)
        .setRequestListeners(requestListeners)
        .setSmallImageDiskCacheConfig(smallImageDiskCacheConfig)
        .build();
    Fresco.initialize(context, config);
    

    8.缓存:

    • Bitmap 缓存
      Bitmap缓存存储Bitmap对象,这些Bitmap对象可以立刻用来显示或者用于后处理

    • 未解码图片的内存缓存
      这个缓存存储的是原始压缩格式的图片。从该缓存取到的图片在使用之前,需要先进行解码。

    • 磁盘缓存
      和未解码的内存缓存相似,磁盘缓存存储的是未解码的原始压缩格式的图片,在使用之前同样需要经过解码等处理。

    相关文章

      网友评论

          本文标题:Fresco的使用

          本文链接:https://www.haomeiwen.com/subject/mypeeqtx.html