Mask an image in layout in Android
I need to be able to mask an image in my layout to have rounded corners. Is there a simple way to specify an alpha mask in the layout? I would like to avoid having to manually mask each bitmap I'm using.
EDIT
Here is my code, as referenced to the other answer:
HttpURLConnection connection = (HttpURLConnection) this.items.get(position).getIcon().toURL().openConnection();
connection.setDoInput(true);
connection.connect();
Bitmap bitmap = BitmapFactory.decodeStream(connection.getInputStream());
Integer dimension = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getWidth() :
bitmap.getHeight();
Bitmap rounder = Bitmap.createBitmap(dimension, dimension, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(rounder);
Paint painter = new Paint(Paint.ANTI_ALIAS_FLAG);
painter.setColor(Color.RED);
canvas.drawRoundRect(new RectF开发者_运维技巧(0, 0, dimension, dimension),
20.0F, 20.0F, painter);
painter.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.drawBitmap(rounder, 0, 0, painter);
holder.image.setImageBitmap(bitmap);
However, this doesn't work. I don't see see rounded corners, I see the regular image as-is. Is there a step at the end I'm missing that involves the canvas?
Looks like your question was answered before, perhaps this might help?
There are a few ways you could acomplish this . I would make a nine patch that is transparent except for the corners and draw that on top of the images.
精彩评论