概述:

  图片加载的工作流(task flow)都是3级缓存的流程;图片的内存缓存一定是LruCache实现;图片下载和读取线程的调度一定是通过线程池管理

画图说明图片加载原理

Glide的使用

详情查看https://github.com/bumptech/glide

  • 介绍:

    • 专注于处理平滑滑动的图片类库
    • 默认使用HttpUrlConnection下载图片
    • 支持设置渐渐显示的动画
    • 支持设置加载中的图片
  • 添加依赖

    compile 'com.github.bumptech.glide:glide:3.7.0'
    
  • 使用Glide加载图片

     Glide.with(this)
            .load("")
            .centerCrop()//设置从中间剪切
            .placeholder(R.mipmap.ic_launcher)//设置加载过程中显示的图片
            .error(R.mipmap.ic_launcher)//设置加载失败显示的图片
            .crossFade()//设置渐渐显示的效果
            .into(image);
    

Picasso的使用

详情查看https://github.com/square/picasso

  • 介绍:

    • Square开源的比较早的图片加载类库
    • 自动处理adapter中的ImageView的回收时取消下载图片
    • 支持加载多种来源的图片,比如网络,sd卡,res资源
    • 支持设置占位图片
    • 支持对图片的自定义处理
  • 添加依赖

    compile 'com.squareup.picasso:picasso:2.5.2'
    
  • 使用Picasso加载图片

    Picasso.with(this)
            .load("url")
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .centerCrop()
            .noFade()//设置不需要渐渐显示的动画效果
            .resize(120,120)//指定压缩参考的宽高比
            .into(image);
    
  • 加载其他资源路径的图片

    Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
    Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
    Picasso.with(context).load(new File(...)).into(imageView3);
    
  • 注意:如果不设置resize(120,120),则Picasso会加载整个图片,显然这样消耗的内存比较大,一般都需要指定一下,而Glide内部已经默认参考了控件的宽高来进行缩放了。

Fresco的使用

详情查看https://github.com/facebook/fresco

  • 介绍:

    • Facebook开源的专注于优化java堆内存,最大程度减少OOM
    • 在Android4.4以及以下,将图片存储在Android的一块特殊的内存区域,这会让图片处理更加快速
    • 支持Gif和WebP格式的图片
  • 添加依赖

    compile 'com.facebook.fresco:fresco:0.11.0'
    
  • 首先初始化Fresco,一般在Application的onCreate中初始化

    //先初始化Fresco
    Fresco.initialize(this);
    
  • 使用Fresco提供的SimpleDraweeView显示图片

      draweeView.setImageURI("url");
    
  • 由于使用的是自定义控件加载图片,那么通过定义属性来进行设置:

    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/image_view"
        android:layout_width="300dp"
        android:layout_height="300dp"
        fresco:fadeDuration="300"
        fresco:actualImageScaleType="focusCrop"
        fresco:placeholderImage="@color/wait_color"
        fresco:placeholderImageScaleType="fitCenter"
        fresco:failureImage="@drawable/error"
        fresco:failureImageScaleType="centerInside"
        fresco:retryImage="@drawable/retrying"
        fresco:retryImageScaleType="centerCrop"
        fresco:progressBarImage="@drawable/progress_bar"
        fresco:progressBarImageScaleType="centerInside"
        fresco:progressBarAutoRotateInterval="1000"
        fresco:backgroundImage="@color/blue"
        fresco:overlayImage="@drawable/watermark"
        fresco:pressedStateOverlayImage="@color/red"
        fresco:roundAsCircle="false"
        fresco:roundedCornerRadius="1dp"
        fresco:roundTopLeft="true"
        fresco:roundTopRight="false"
        fresco:roundBottomLeft="false"
        fresco:roundBottomRight="true"
        fresco:roundWithOverlayColor="@color/corner_color"
        fresco:roundingBorderWidth="2dp"
        fresco:roundingBorderColor="@color/border_color"
    />
    
    属性解释:
    placeholderImage就是所谓的展位图啦,在图片没有加载出来之前你看到的就是它
    
    failureIamge看到名字就知道是什么了,图片加载失败时显示的图片就是它了
    
    retryImage图片加载失败时显示,提示用户点击重新加载,重复加载4次还是没有加载出来的时候才会显示failureImage的图片
    
    progressBarImage进度条图片
    backgroundImage背景图片,这里的背景图片首先被绘制
    
    overlayImage设置叠加图,在xml中只能设置一张叠加图片,如果需要多张图片的话,需要在java代码中设置哦
    pressedStateOverlayImage设置点击状态下的叠加图,此叠加图不能缩放
    
    ImageScaleType这个就是各种各样的图片缩放样式了,center,centerCrop,fouseCrop,centerInside,fitCenter,fitStart,fitEnd,fitXY

图片加载总结

  • UniversalImageLoader:老牌优秀的图片加载类库,特点是配置项丰富,支持圆形图片效果显示以及添加图片加载动画
  • Picasso : Square公司出品。也是很早期出现的图片加载库。默认加载图片不会压缩,并且图片渲染模式是ARGB_8888,占用内存相比Glide稍微高一点,但是可以指定图片加载的宽高,便会依据图片的宽高进行缩放
  • Glide:专门为优化Picasso而生,所以API和Picasso简直一模一样。内部会自动根据图片的宽高来压缩图片,并且图片渲染模式为RGB_565,内存占用会减少一半,专门针对滑动中的图片加载有优化。和Picasso相比,推荐使用Glide。
  • Fresco : Facebook公司开源的。特点是在android4.4以及以下,将图片的放入Android native的C++内存中,而不是Java堆内存,所以占用的Java堆内存很小,大大减小了程序出现OOM的几率;支持WebP和Gif显示;支持多种图片的显示配置,比如圆形。
posted on 2017-02-14 18:35  从前有個人  阅读(235)  评论(0编辑  收藏  举报