Android 开发Picasso 设置圆形图片

释放双眼,带上耳机,听听看~!
Picasso.with(getContext()).load(getLoginInfo1.getString("headpicurl", "0")).resize(200, 200).transform(new CircleTransform()).into(mHeadImageView);
//ImageView 设置成圆形图片
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
    float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
mAccount.setHeadPic(bitmap);
squaredBitmap.recycle();
    return bitmap;
}

 


// Picasso.with(getContext()).load(mAccount.getHeadImageUrl()).transform(new CircleTransform()).into(mHeadImageView);
// 声明一个Picasso 裁剪图片的类,设置为圆形头像。
class CircleTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
        float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
        return bitmap;
}
@Override
public String key() {
return "circle";
}
}

 

人已赞赏
Android文章

Android Studio 插件汇总

2020-3-25 7:11:46

Android文章

android 接口 接口回调

2020-3-25 10:20:10

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索