这些其实很简单,在项目里调用这两个代码就行了。但是圆角图片还是有点小坑(有的图片设置过后不是圆角图片,可能是每个人上传的图片尺寸长宽比例不是合适的值),不过我已经解决了。下面就来看看怎么用的吧
先上一个效果图:
首先要下载glide jar包
下载链接:glide jia包 密码:pk8n
1.圆角图片
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
/**
* Created by tongshoujun on 2017/9/12.
* 圆角形图
*/
public classGlideRoundTransformextendsBitmapTransformation {
private static floatradius=0f;
publicGlideRoundTransform(Context context) {
this(context,4);
}
publicGlideRoundTransform(Context context, intdp) {
super(context);
this.radius= Resources.getSystem().getDisplayMetrics().density* dp;
}
@Override
protected Bitmaptransform(BitmapPool pool,Bitmap toTransform, intoutWidth, intoutHeight) {
returnroundCrop(pool,toTransform);
}
private staticBitmaproundCrop(BitmapPool pool,Bitmap source) {
if(source ==null)return null;
Bitmap result = pool.get(source.getWidth(),source.getHeight(),Bitmap.Config.ARGB_8888);
if(result ==null) {
result = Bitmap.createBitmap(source.getWidth(),source.getHeight(),Bitmap.Config.ARGB_8888);
}
Canvas canvas =newCanvas(result);
Paint paint =newPaint();
paint.setShader(newBitmapShader(source,BitmapShader.TileMode.CLAMP,BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF =newRectF(0f,0f,source.getWidth(),source.getHeight());
//设置圆角弧度
canvas.drawRoundRect(rectF,radius,radius,paint);
returnresult;
}
@Override
public StringgetId() {
returngetClass().getName() + Math.round(radius);
}
}
这是我的使用,我就不过多解释(10就是你要设置圆角弧度的度数),如果你要使用,请改成你想要的。
2.圆形图片
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 tongshoujun on 2017/9/12.
* 圆形图
*/
public class GlideCircleTransformextendsBitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override
protected Bitmaptransform(BitmapPool pool,Bitmap toTransform, intoutWidth, intoutHeight) {
returncircleCrop(pool,toTransform);
}
private static BitmapcircleCrop(BitmapPool pool,Bitmap source) {
if(source ==null)return null;
intsize = Math.min(source.getWidth(),source.getHeight());
intx = (source.getWidth() - size) /2;
inty = (source.getHeight() - size) /2;
//TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source,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 =newCanvas(result);
Paint paint =newPaint();
paint.setShader(newBitmapShader(squared,BitmapShader.TileMode.CLAMP,BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
floatr = size /2f;
canvas.drawCircle(r,r,r,paint);
returnresult;
}
@Override
public StringgetId() {
returngetClass().getName();
}
}
使用结果: