图片缓存框架

来源:互联网 发布:北控软件 怎么样 编辑:程序博客网 时间:2024/06/05 04:46

图片缓存框架主要有Picasso(Square 开源),Glide(Google 开源),Fresco(Facebook 开源)。它们都采用多级缓存(至少有两级缓存)来提高图片加载速度。都支持多种数据源,网络(http和https)、本地、资源、Assets等。

{}Picasso

()Picasso默认Bitmap格式是ARGB_888,图片质量相对要好些,但是占用的内存很大。

()Picasso加载全尺寸的图片到内存,然后让GPU来实时重绘大小。

//Picasso指定加载的图片大小(resize)Picasso.with(this)       .resize(int targetWidth, int targetHeight)       .into(""); 

()Picasso磁盘缓存只有一个全尺寸的的图片。

()不支持Gif图片

()获取加载图片,进行一系列的操作(裁剪,压缩,圆角)

Picasso.with(this)       .load("")       .transform(new CropSquareTransformation())       .into("");public class CropSquareTransformation implements Transformation {    @Override    public Bitmap transform(Bitmap source) {        //进行裁剪        int size = Math.min(source.getWidth(), source.getHeight());        int x = (source.getWidth() - size) / 2;        int y = (source.getHeight() - size) / 2;        Bitmap result = Bitmap.createBitmap(source, x, y, size, size);        if (result != source) {            source.recycle();        }        return result;    }    @Override    public String key() {        return "square()";    }}

{}Glide

()Glide支持Gif、WebP、缩略图。甚至是Video,可以看成是一个媒体缓存。

()Glide加载图片会和Activity/Fragment的生命周期保持一致。比如Paused状态时暂停加载,Resumed状态时又自动重新加载。所以可以把Activity 和 Fragment传递给Glide,而不是Context。

()Glide默认Bitmap格式是RGB_565,图片质量相对要差些,但是占用的内存很少。

可以创建一个GlideModule将Bitmap格式转换到ARGB_8888,但是占用的内存会增大。

public class MyGlideModule implements GlideModule {    @Override     public void applyOptions(Context context, GlideBuilder builder) {        // Apply options to the builder here.      builder.setDecodeFormat(DecodeFormat.ALWAYS_ARGB_8888);    }  ......   }<manifest ...>    <application ...>        <!-- ... permissions -->        <meta-data            android:name="com.mypackage.MyGlideModule"            android:value="GlideModule" />        <!-- ... activities and other components -->    </application></manifest>      

()Glide加载图片的大小和ImageView大小是一致的。

()Glide磁盘缓存的是跟ImageView大小相同的图片,

()Glide会为每种大小的ImageView磁盘缓存一次图片。也就说一张图片已经缓存过一次,但是你要在另一个不同尺寸的ImageView显示,则需要重新下载,调整成新尺寸的大小,再将这个尺寸图片也缓存起来(加载比Picasso快)。

//Glide既缓存全尺寸又缓存其他尺寸Glide.with(this)     .diskCacheStrategy(DiskCacheStrategy.ALL)     .into(""); 

这样下次在任何ImageView中加载图片的时候,全尺寸的图片将从缓存中取出,重新调整大小,然后缓存.

()获取加载图片,进行一系列的操作(压缩,圆角)

Glide.with(this)     .load("")     .transform(new CropGlideTransformation(this))     .into("");public class CropGlideTransformation extends BitmapTransformation {    public CropGlideTransformation(Context context) {        super(context);    }    @Override    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {        //进行裁剪        if (toTransform == null) {            return null;        }        int size = Math.min(outWidth, outHeight);        int x = (outWidth - size) / 2;        int y = (outHeight - size) / 2;        Bitmap result = Bitmap.createBitmap(toTransform, x, y, size, size);        if (result != toTransform) {            toTransform.recycle();        }        return result;    }    @Override    public String getId() {        return getClass().getName();    }}

{}Fresco

Picasso框架
https://github.com/square/picasso

Glide框架
https://github.com/bumptech/glide

Fresco框架
https://www.fresco-cn.org

0 0
原创粉丝点击