관련 개념
1. Drawable은 그림을 그릴 수 있는 객체로, 비트맵(BitmapDrawable), 도형(ShapeDrawable), 또는 레이어(LayerDrawable)일 수 있습니다. 그리기 요구사항에 따라 적절한 그리기 객체를 생성합니다.
2. Canvas는 그림을 그릴 목적지 영역으로, 그리기 작업을 수행하는 데 사용됩니다.
3. Bitmap은 이미지 처리를 위한 비트맵 데이터입니다.
4. Matrix는 변환(회전, 크기 조정, 이동 등)을 위한 행렬입니다.
Bitmap 관련 작업
1. 리소스에서 Bitmap 가져오기
Resources resMgr = getResources();
Bitmap imageBitmap = BitmapFactory.decodeResource(resMgr, R.drawable.sample_image);
2. Bitmap → byte[] 변환
public byte[] convertBitmapToBytes(Bitmap bitmapImage) {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 90, byteStream);
return byteStream.toByteArray();
}
3. byte[] → Bitmap 변환
public Bitmap convertBytesToBitmap(byte[] byteData) {
if (byteData != null && byteData.length > 0) {
return BitmapFactory.decodeByteArray(byteData, 0, byteData.length);
}
return null;
}
4. Bitmap 크기 조정
public static Bitmap resizeBitmap(Bitmap originalBitmap, int newWidth, int newHeight) {
int currentWidth = originalBitmap.getWidth();
int currentHeight = originalBitmap.getHeight();
Matrix transformationMatrix = new Matrix();
float widthScale = ((float) newWidth / currentWidth);
float heightScale = ((float) newHeight / currentHeight);
transformationMatrix.postScale(widthScale, heightScale);
return Bitmap.createBitmap(originalBitmap, 0, 0, currentWidth, currentHeight, transformationMatrix, true);
}
5. Drawable을 Bitmap으로 변환
public static Bitmap convertDrawableToBitmap(Drawable drawableObject) {
int width = drawableObject.getIntrinsicWidth();
int height = drawableObject.getIntrinsicHeight();
Bitmap.Config imageConfig = drawableObject.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
Bitmap resultBitmap = Bitmap.createBitmap(width, height, imageConfig);
Canvas drawingCanvas = new Canvas(resultBitmap);
drawableObject.setBounds(0, 0, width, height);
drawableObject.draw(drawingCanvas);
return resultBitmap;
}
6. 둥근 모서리 이미지 생성
public static Bitmap createRoundedCornerBitmap(Bitmap sourceBitmap, float cornerRadius) {
int width = sourceBitmap.getWidth();
int height = sourceBitmap.getHeight();
Bitmap outputBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(outputBitmap);
final int backgroundColor = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, height);
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(backgroundColor);
canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sourceBitmap, rect, rect, paint);
return outputBitmap;
}
7. 반사 효과가 있는 이미지 생성
public static Bitmap createReflectionImage(Bitmap originalBitmap) {
final int reflectionGap = 4;
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
Matrix reflectionMatrix = new Matrix();
reflectionMatrix.preScale(1, -1);
Bitmap reflectionPart = Bitmap.createBitmap(originalBitmap, 0, height/2, width, height/2, reflectionMatrix, false);
Bitmap combinedBitmap = Bitmap.createBitmap(width, height + height/2, Config.ARGB_8888);
Canvas canvas = new Canvas(combinedBitmap);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
canvas.drawBitmap(reflectionPart, 0, height + reflectionGap, null);
Paint gradientPaint = new Paint();
LinearGradient shader = new LinearGradient(0, originalBitmap.getHeight(), 0,
combinedBitmap.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
gradientPaint.setShader(shader);
gradientPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, combinedBitmap.getHeight() + reflectionGap, gradientPaint);
return combinedBitmap;
}
Drawable 관련 작업
1. Bitmap을 Drawable로 변환
Bitmap bitmapImage = getImage(); // 실제 이미지 획득 방법에 따라 변경
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmapImage);
// BitmapDrawable은 Drawable의 하위 클래스이므로 bitmapDrawable 객체를 직접 사용
2. Drawable 크기 조정
public static Drawable resizeDrawable(Drawable drawableItem, int targetWidth, int targetHeight) {
int currentWidth = drawableItem.getIntrinsicWidth();
int currentHeight = drawableItem.getIntrinsicHeight();
Bitmap intermediateBitmap = convertDrawableToBitmap(drawableItem);
Matrix scaleMatrix = new Matrix();
float widthRatio = ((float) targetWidth / currentWidth);
float heightRatio = ((float) targetHeight / currentHeight);
scaleMatrix.postScale(widthRatio, heightRatio);
Bitmap resizedBitmap = Bitmap.createBitmap(intermediateBitmap, 0, 0, currentWidth, currentHeight, scaleMatrix, true);
return new BitmapDrawable(resizedBitmap);
}