見てて便利だなあと思いつつ.
Y.A.M の 雑記帳: Android ImageViewの領域を角丸にする方法
Paint#setXfermode を使ってできないか, などと思い始め.
角丸であろうが, 丸であろうが, 三角であろうが, 星型であろうが,
描画ができればくり抜けるはず.
こんなの.
public Bitmap getCroppedBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
final Rect rect = new Rect(0, 0, width, height);
final RectF rectf = new RectF(0, 0, width, height);
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawRoundRect(rectf, width / 5, height / 5, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
レイアウト.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/image0"
android:layout_width="96dp"
android:layout_height="96dp"/>
<ImageView
android:id="@+id/image1"
android:layout_width="96dp"
android:layout_height="96dp"/>
</LinearLayout>
使い方.
ImageView imageView0 = (ImageView) view.findViewById(R.id.image0);
ImageView imageView1 = (ImageView) view.findViewById(R.id.image1);
imageView0.setImageBitmap(
getCroppedBitmap(
BitmapFactory.decodeResource(
this.getResources(),
R.drawable.funasi0,
null
)
)
);
imageView1.setImageBitmap(
getCroppedBitmap(
BitmapFactory.decodeResource(
this.getResources(),
R.drawable.funasi1,
null
)
)
);

あなたの Utils にもぜひ.