검색결과 리스트
글
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
이번에 프로젝트를 하면서 비트맵을 다룰 일이 생겼는데, 프로젝트를 진행하면서 사용하게 된 함수들을 정리해둔다.
아마 앞으로 비트맵에 관련된 함수를 더 사용하게 된다면 이 글에 추가하도록 하겠다.
1. View의 비트맵 얻기
public static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
v.clearFocus();
v.setPressed(false);
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
2. 두 비트맵 합성
public static Bitmap getOverlayBitmap(Bitmap baseBmp, Bitmap overlayBmp, int distanceLeft, int distanceTop) {
Bitmap resultBmp = Bitmap.createBitmap(baseBmp.getWidth(), baseBmp.getHeight(), baseBmp.getConfig());
Canvas canvas = new Canvas(resultBmp);
canvas.drawBitmap(baseBmp, 0, 0, null);
canvas.drawBitmap(overlayBmp, distanceLeft, distanceTop, null);
return resultBmp;
}
Bitmap resultBmp = Bitmap.createBitmap(baseBmp.getWidth(), baseBmp.getHeight(), baseBmp.getConfig());
Canvas canvas = new Canvas(resultBmp);
canvas.drawBitmap(baseBmp, 0, 0, null);
canvas.drawBitmap(overlayBmp, distanceLeft, distanceTop, null);
return resultBmp;
}
3. 비트맵 회전
public static Bitmap getRotationBitmap(Bitmap bitmap, int degrees) {
if (degrees != 0 && bitmap != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
if (degrees != 0 && bitmap != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);
try {
Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
if (bitmap != converted) {
bitmap.recycle();
bitmap = converted;
}
} catch (OutOfMemoryError ex) {
}
}
return bitmap;
}
Bitmap converted = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
if (bitmap != converted) {
bitmap.recycle();
bitmap = converted;
}
} catch (OutOfMemoryError ex) {
}
}
return bitmap;
}
4. 비트맵을 원형으로 변경
public static Bitmap getRoundedBitmap(Bitmap bitmap) {
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.GRAY;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawOval(rectF, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
}
5. 비트맵에 라운드 코너를 지정
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
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 = pixels;
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 = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
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);
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
6. 비디오파일에서 썸네일이미지 생성
String moviePath = "<경로>/test.mp4";
Bitmap bm = ThumbnailUtils.createVideoThumbnail(moviePath, MediaStore.Images.Thumbnails.MINI_KIND);
'-- Android' 카테고리의 다른 글
BITMAP To BASE64 (0) | 2015.07.21 |
---|---|
Touch후 Drag 시 자신 영역 밖으로 Drag되었는지 체크 (0) | 2015.07.20 |
숫자로 된 문자열에 천단위로 콤마를 표시 (0) | 2015.07.16 |
네트워크 사용가능 여부 확인 (0) | 2015.07.16 |
PiXEL to DP, DP to PIXEL (0) | 2015.07.16 |
RECENT COMMENT