[Android] BitmapHelper Class

개요

BitmapHelper 클래스는 인자로 넘겨받은 비트맵 이미지의 모서리를 둥글게 처리한 비트맵으로 변경하여 리턴하는 클래스이다.

소스

[code lang=”java”] import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class BitmapHelper {

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap , int roundLevel) {

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.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, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = convertDipsToPixels(context, roundLevel);

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(bitmap, rect, rect, paint);

return output;
}

public static int convertDipsToPixels(Context context, int dips) {

final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dips * scale + 0.5f);
}

}
[/code]

You may also like...

1 Response

  1. December 10, 2011

    […] 클래스는 해당 포스트를 […]

Leave a Reply

Your email address will not be published. Required fields are marked *