Glide使用
1.
compile 'com.github.bumptech.glide:glide:3.7.0'
//图形处理框架,可以不加
compile 'jp.wasabeef:glide-transformations:2.0.1'
// If you want to use the GPU Filters
compile 'jp.co.cyberagent.android.gpuimage:gpuimage-library:1.3.0'
2.使用
final ImageView imageview= (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("http://attach.bbs.miui.com/forum/201502/08/021410qwvwbpdkiasdhwha.jpg")
// .asGif()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
// .placeholder(R.mipmap.ic_launcher)
// .transform(new GlideCircleTransform(MainActivity.this))
.bitmapTransform(new CropCircleTransformation(MainActivity.this))
// .transform(new RoundedCornersTransformation(this,30,0, RoundedCornersTransformation.CornerType.ALL))
.into(imageview);
asgif()和不asgif()区别:
asgif()就不可以调用.bitmapTransform 方法。但是有一个transfrom方法。也就是说你加上asgif你就不可以使用上面我提到的那个图形处理框架。而且不加gif也可以显示gif,而且可以用上面的框架圆角显示
3.当然你可以自己写处理图片的逻辑如下:
//这会让图片圆角显示
package cn.xinyu.com.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
/**
* Created by Administrator on 2016/6/2.
*/
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private Bitmap circleCrop(BitmapPool pool, Bitmap toTransform) {
if (toTransform == null) return null;
int size = Math.min(toTransform.getWidth(), toTransform.getHeight());
int x = (toTransform.getWidth() - size) / 2;
int y = (toTransform.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(toTransform, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}
然后使用的时候:
Glide.with(this).load("http://attach.bbs.miui.com/forum/201502/08/021410qwvwbpdkiasdhwha.jpg")
.transform(new GlideCircleTransform(MainActivity.this))
4.gif加载慢解决方法
.asGif()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
泰山崩于前而色不变的曹新雨,抱歉