【发布时间】:2017-05-17 03:51:43
【问题描述】:
我想用 5px 圆角图像以显示在毕加索的图像视图上。我创建了简单的类ImageRoundCorners,在其中我使用简单的方法来圆角图像,但我的代码不起作用,角落没有圆角。下面是我的代码:
file = new File(APP.DIR_APP + APP.IMAGE + "/ok.jpg");
if (file.isFile() && file.exists()) {
Uri uri = Uri.fromFile(file);
Picasso.with(this).load(uri).transform(new ImageRoundCorners()).into(fiv_image_view);
}
和ImageRoundCorners 类:
import com.squareup.picasso.Transformation;
public class ImageRoundCorners implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
Bitmap output = Bitmap.createBitmap(source.getWidth(), source
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 50;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(source, rect, rect, paint);
return output;
}
@Override
public String key() {
return "RoundImage";
}
}
这段代码有什么问题,我该如何解决?
我收到此错误:
java.lang.IllegalStateException: Transformation RoundImage mutated input Bitmap but failed to recycle the original.
【问题讨论】:
-
你为什么不尝试自定义
Drawable并应用到你的 imageView。 -
@W4R10CK 我认为使用这段代码我有简单的实现
-
我必须说不要裁剪原始图像,而是使用自定义 Drawable 到您的 imageview 来使角落变圆。
-
如果您返回一个新的位图,那么您需要在原始位图上调用 recycle()。检查毕加索图书馆的创建者对此问题的看法,github.com/square/picasso/issues/489
-
我找到的最干净的方式是这样的:medium.com/@ookami.kb/…
标签: android android-layout picasso