Android Picasso是一个用于管理图片加载和缓存的开源库。它提供了简单易用的API来加载、展示和处理图片,使开发者能够轻松地在Android应用中实现圆形图片和圆角图片效果。
// 定义一个自定义的Transformation
public 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";
}
}
// 使用Picasso加载并显示圆形图片
Picasso.get()
.load(imageUrl)
.transform(new CircleTransform())
.into(imageView);
// 定义一个自定义的Transformation
public class RoundedCornersTransform implements Transformation {
private final int radius;
public RoundedCornersTransform(int radius) {
this.radius = radius;
}
@Override
public Bitmap transform(Bitmap source) {
Bitmap result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
RectF rectF = new RectF(0, 0, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(source, 0, 0, paint);
if (result != source) {
source.recycle();
}
return result;
}
@Override
public String key() {
return "rounded_corners";
}
}
// 使用Picasso加载并显示圆角图片
Picasso.get()
.load(imageUrl)
.transform(new RoundedCornersTransform(radius))
.into(imageView);
优势:
应用场景:
推荐的腾讯云相关产品和产品介绍链接地址: 腾讯云提供了丰富的云计算和存储产品,以下是一些相关产品的介绍:
领取专属 10元无门槛券
手把手带您无忧上云