的Android圆形图片

的Android圆形图片

我想用5px在图像的拐角处用毕加索在imageview上显示。我创建了一个简单的类ImageRoundCorners,在这个类中,我使用simple方法来圆化图像的角点,但是我的代码不起作用,角点也不圆。下面是我的代码:

   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.

最佳答案

错误信息非常清楚。你只是忘记回收原始位图。

....
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return output;

你的代码只缺一行!(我很惊讶所有这些答案告诉你做各种无关的,连根拔起的解决方案。)

关于android - 带有 picasso 的Android圆形图片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41436999/

10-10 11:39