聊聊Android优秀的图片加载缓存的开源框架?UIL、Glide、Picasso

今天总结下有关Android的图片开源框架UIL、Glide、Picasso、固然不止这些还有okhttp、xutlis、afinal、andbase、volley等等,今天主要是对于Glide使用进行总结。css

Glide是谷歌推荐使用的加载图片的框架,它相对于其余的框架有更多的有点,说到Glide咱们不得不谈谈Picasso,为何呢?这是由于Picasso的使用与Glide的使用上很是的类似,可是细细看,有明显不一样,首先咱们看下Picasso与Glide的基本用法?java

Picasso:            android

1   Picasso.with(this)
2                 .load(url)//加载图片
3                 .placeholder(R.mipmap.ic_launcher)//正在加载时的图片
4                 .error(R.mipmap.ic_launcher)//加载错误是的图片
5                 .into(glide_image2);

Glide:canvas

1  Glide.with(this)
2                 .load(url)//加载图片
3                 .placeholder(R.mipmap.ic_launcher)//正在加载时的图片
4                 .error(R.mipmap.ic_launcher)//加载错误是的图片
5                 .into(glide_image);

一、看到没有,是否是同样呢,基本上它们的用法一直,可是咱们在使用Glide时须要注意,Glide.with(this),咱们在传入的时候,我建议传入Actitiy,Fragment对应得context,而不是全局的context,为何呢,这是由于这样咱们可让Gilde加载图片与咱们的Activity,Fragment的生命周期一直,建立时去加载,销毁时中止加载,缓存

二、Glide的加载速度比Picasso的加载速度要快,可是消耗的内存要比Picasso的内存高,为何呢这是由于Gilde他是根据你传入的尺寸进行缓存,若是俩个地方须要      全尺寸缓存,另外一个地方按照比例缓存,那么Glide须要缓存俩次,而Picsso是全尺寸的缓存,每当从新加载时,须要从新绘制app

 1  /**
 2      * Glide的全尺寸缓存
 3      */
 4     public void GlideImage3(String url) {
 5         Glide.with(this)
 6                 .load(url)//加载图片
 7                 .placeholder(R.mipmap.ic_launcher)//正在加载时的图片
 8                 .error(R.mipmap.ic_launcher)//加载错误是的图片
 9                 .diskCacheStrategy(DiskCacheStrategy.ALL)
10                 .into(glide_image);
11     }

 

3,Picasso加载的bitmap格式ARGB_8888而Glide所加载的bitmap格式ARGB_565固然咱们能够经过实现GlideMenu来实现框架

 1 /**
 2  * 更改Glide的bitmap的格式为ARGB_8888
 3  * Created by joe.xiang on 2016/6/9.
 4  */
 5 public class GlideConfigration implements GlideModule {
 6 
 7 
 8     @Override
 9     public void applyOptions(Context context, GlideBuilder builder) {
10         builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
11     }
12 
13     @Override
14     public void registerComponents(Context context, Glide glide) {
15 
16     }
17 }

 

四、Glide的setTag方法不一样之处?ide

   咱们能够经过在咱们的values下创建一个ids的xmlui

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="image_tag" type="id"/>
</resources>

经过Image.setTag(R.id.image_tag,url)的形式来进行设置this

 

 

五、Glide如何设置圆形图片

    对于如何制做圆形的方法,有不少能够经过自定义ImageView,固然Glide也给咱们提供了不少的方法来时圆角图片。通常有一下方法

    一、自定一个Transform 继承 BitmapTransformation

 1 /**
 2  * Created by joe.xiang on 2016/6/9.
 3  */
 4 public  class CircleTransform extends BitmapTransformation {
 5 
 6     public CircleTransform(Context context) {
 7         super(context);
 8     }
 9 
10     @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
11         return circleCrop(pool, toTransform);
12     }
13 
14     private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
15         if (source == null) return null;
16         int size = Math.min(source.getWidth(), source.getHeight());
17         int x = (source.getWidth() - size) / 2;
18         int y = (source.getHeight() - size) / 2;
19 
20         // TODO this could be acquired from the pool too
21         Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
22         Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
23         if (result == null) {
24             result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
25         }
26         Canvas canvas = new Canvas(result);
27         Paint paint = new Paint();
28         paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
29         paint.setAntiAlias(true);
30         float r = size / 2f;
31         canvas.drawCircle(r, r, r, paint);
32         return result;
33     }
34 
35     @Override public String getId() {
36         return getClass().getName();
37     }
38 }
 1   /**
 2      * 经过Glide的TransForMation 自定义圆形图片的bitmap
 3      */
 4     public void RoundImage(String url) {
 5         Glide.with(this)
 6                 .load(url)
 7                 .asBitmap()
 8                 .transform(new CircleTransform(this))
 9                 .into(glide_image5);
10     }

 

 二、咱们能够经过BitmapImageVieTarget,来获得一个带圆角的RoundBitmapDrawable;

 1  /**
 2      * 经过RoundBitmapDrawable
 3      */
 4     public void RoundImage2(String url) {
 5         Glide.with(this)
 6                 .load(url)
 7                 .asBitmap()
 8                 .into(new BitmapImageViewTarget(glide_image6) {
 9                     @Override
10                     protected void setResource(Bitmap resource) {
11                         RoundedBitmapDrawable RoundedBitmapDrawable = RoundedBitmapDrawableFactory.create(Glide_1.this.getResources(), resource);
12                         RoundedBitmapDrawable.setCircular(true);
13                         glide_image6.setImageDrawable(RoundedBitmapDrawable);
14                     }
15                 });
16     


三、咱们能够经过自定义RoundedCornerLayout 继承RelavityLayout来实现圆角图片效果
 1 **
 2  * Created by joe.xiang on 2016/6/9.
 3  */
 4 public class RoundedCornerLayout  extends RelativeLayout {
 5     private Bitmap maskBitmap;
 6     private Paint paint;
 7     private float cornerRadius;
 8 
 9     public RoundedCornerLayout(Context context) {
10         super(context);
11         init(context, null, 0);
12     }
13 
14     public RoundedCornerLayout(Context context, AttributeSet attrs) {
15         super(context, attrs);
16         init(context, attrs, 0);
17     }
18 
19     public RoundedCornerLayout(Context context, AttributeSet attrs, int defStyle) {
20         super(context, attrs, defStyle);
21         init(context, attrs, defStyle);
22     }
23 
24     private void init(Context context, AttributeSet attrs, int defStyle) {
25         paint = new Paint(Paint.ANTI_ALIAS_FLAG);
26 
27         setWillNotDraw(false);
28     }
29 
30     @Override
31     public void draw(Canvas canvas) {
32         super.draw(canvas);
33 
34         if (maskBitmap == null) {
35             // This corner radius assumes the image width == height and you want it to be circular
36             // Otherwise, customize the radius as needed
37             cornerRadius = canvas.getWidth() / 2;
38             maskBitmap = createMask(canvas.getWidth(), canvas.getHeight());
39         }
40 
41         canvas.drawBitmap(maskBitmap, 0f, 0f, paint);
42     }
43 
44     private Bitmap createMask(int width, int height) {
45         Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
46         Canvas canvas = new Canvas(mask);
47 
48         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
49         paint.setColor(Color.WHITE); // TODO set your background color as needed
50 
51         canvas.drawRect(0, 0, width, height, paint);
52 
53         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
54         canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);
55         return mask;
56     }
57 }
 1   <huanxin.exmaple.com.android_glidedemo.RoundedCornerLayout
 2                    android:layout_width="200dp"
 3                    android:layout_height="200dp">
 4                        <ImageView
 5                            android:id="@+id/glide_image7"
 6                            android:layout_width="200dp"
 7                            android:layout_height="200dp"
 8                            android:scaleType="centerCrop"
 9                            />
10  </huanxin.exmaple.com.android_glidedemo.RoundedCornerLayout>

使用上跟通常使用没什么区别。。。。。

4 、固然咱们亦可使用开源的圆角图片的自定义控件?

1   Glide.with(this).load(url).into(new SimpleTarget<GlideDrawable>() {
2              @Override
3              public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
4                  //使用自定义的圆角图片
5              }
6          });

对于Glide加载图片还有不少能够去研究的地方,它还能够加载gif的动态图,不过这个方法要谨慎使用,由于这个很是耗内存,对于Glide的使用花了一个下午对于他的一些基本使用就总结致使,过段时间深刻研究后在总结了、一下附上加载的图片效果图。

相关文章
相关标签/搜索