개발/개발 자료

(Android) ImageView 라운드(둥근 테두리) 처리

시원한물냉 2014. 1. 6. 10:58


원리는 간단하다.

원본 Bitmap 이미지와 동일한 크기의 Bitmap 객체를 생성한다.

그리고 생성한 Bitmap 객체에 캔버스를 연결한 후 캔버스에 둥근 테두리의 사각형을 그린다.

그리고 그 사각형 영역에 원본 Bitmap을 투과시키는 것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static Bitmap setRoundCorner(Bitmap bitmap, int pixel) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                            Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
         
    int color = 0xff424242;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);
         
    paint.setAntiAlias(true);
    paint.setColor(color);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, pixel, pixel, paint);
         
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
         
    return output;
}