带圆角的位图&透明的bg

时间:2012-11-27 17:26:28

标签: java android bitmap

我正在尝试使用下面的代码对位图的角进行圆角处理。问题在于,无论我将涂料的颜色设置为什么,例如Color.TRANSPARENT,它总是黑色的。我如何实际剪切位图的角落,而不仅仅是将它们涂成黑色?

谢谢!

    public static Bitmap roundCorners(Bitmap src, int radius) {
    //Create a *mutable* location, and a canvas to draw into it
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    RectF rect = new RectF(0, 0, width, height);
    Shader bitmapShader = new BitmapShader(src, TileMode.CLAMP, TileMode.CLAMP);
    paint.setColor(0xFF000000);
    paint.setShader(bitmapShader);
    canvas.drawRoundRect(rect, radius, radius, paint);
    return result;
}

1 个答案:

答案 0 :(得分:4)

有一种更简单的方法可以一次通过。只需绘制一个圆角矩形,但在Paint上设置一个BitmapShader。这将使用您的位图填充圆角矩形。无需更改xfermode或调用drawBitmap。