picasso与glide

Glied是谷歌推荐使用的加载图片的框架,它相对于其他的框架有更多的优点;

Glied,加载快但消耗内存高,按照比例缓存和全尺寸缓存,所加载的Bitmap格式ARGB_565


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

picasso加载速度慢一些,但消耗内存小一点, 全尺寸缓存,每次重新加载时,需要重新绘制.所加载的Bitmap格式ARGB_8888

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



/**
 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     }

Glide设置圆形图片


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 }
 
 
 
 
  /**
 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     }



发布了5 篇原创文章 · 获赞 7 · 访问量 7070

猜你喜欢

转载自blog.csdn.net/jokeYJW/article/details/78538259