今天總結下有關Android的圖片開源框架UIL、Glide、Picasso、當然不止這些還有okhttp、xutlis、afinal、andbase、volley等等,今天主要是對於Glide使用進行總結。
Glide是谷歌推薦使用的加載圖片的框架,它相對於其他的框架有更多的有點,說到Glide我們不得不談談Picasso,為什么呢?這是因為Picasso的使用與Glide的使用上非常的相似,但是細細看,有明顯不同,首先我們看下Picasso與Glide的基本用法?
Picasso:
1 Picasso.with(this) 2 .load(url)//加載圖片 3 .placeholder(R.mipmap.ic_launcher)//正在加載時的圖片 4 .error(R.mipmap.ic_launcher)//加載錯誤是的圖片 5 .into(glide_image2);
Glide:
1 Glide.with(this) 2 .load(url)//加載圖片 3 .placeholder(R.mipmap.ic_launcher)//正在加載時的圖片 4 .error(R.mipmap.ic_launcher)//加載錯誤是的圖片 5 .into(glide_image);
1、看到沒有,是不是一樣呢,基本上它們的用法一直,但是我們在使用Glide時需要注意,Glide.with(this),我們在傳入的時候,我建議傳入Actitiy,Fragment對應得context,而不是全局的context,為什么呢,這是因為這樣我們可以讓Gilde加載圖片與我們的Activity,Fragment的生命周期一直,創建時去加載,銷毀時停止加載,
2、Glide的加載速度比Picasso的加載速度要快,但是消耗的內存要比Picasso的內存高,為什么呢這是因為Gilde他是根據你傳入的尺寸進行緩存,如果倆個地方需要 全尺寸緩存,另一個地方按照比例緩存,那么Glide需要緩存倆次,而Picsso是全尺寸的緩存,每當重新加載時,需要重新繪制
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 }
4、Glide的setTag方法不同之處?
我們可以通過在我們的values下建立一個ids的xml
<?xml version="1.0" encoding="utf-8"?> <resources> <item name="image_tag" type="id"/> </resources>
通過Image.setTag(R.id.image_tag,url)的形式來進行設置
5、Glide如何設置圓形圖片
對於如何制作圓形的方法,有很多可以通過自定義ImageView,當然Glide也給我們提供了很多的方法來時圓角圖片。一般有一下方法
1、自定一個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 }
2、我們可以通過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
3、我們可以通過自定義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的使用花了一個下午對於他的一些基本使用就總結導致,過段時間深入研究后在總結了、一下附上加載的圖片效果圖。