안녕하세요
현재 화면 이동을 intent로 하고 있습니다. startActivity();
그런데 화면상에 FrameLayout으로 탭구현을 하여 (TabActivity 아님) 이미지버튼 약 60개 배경 6개 탭 버튼 6개 등 많은 이미지를
소요 하고 있습니다.
그래서 아웃오브 메모리가 나는거 같은데
이럴때는 메모리 처리를 어떻게 해야하는지 궁금합니다.
고수님들의 답변 부탁드립니다.
그냥 바로 불러서 이미지뷰에 셋팅하셨나요? 아니면 적절한 크기로 리사이징해서 셋팅하셨나요?
bitmap options 를 이용한 리사이징해서 불러오는 소스 투척합니다.
maxResolution은 800*480 이미지로 셋팅 할거라면 800*480의 루트인 619 넣어주시면 됩니다.
public static Bitmap safeDecodeBitmapFromFile(String filePath, final int maxResolution) {
// File이 null이면 null리턴
if (!new File(filePath).exists()) {
Log.e(TAG, "File 이 존재하지 않습니다.");
return null;
}
// 비트맵팩토리 옵션을 디코드시 크기만 반환하도록 설정.
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
// 크기를 구한다.
BitmapFactory.decodeFile(filePath, bitmapOptions);
getSampleSize(bitmapOptions, maxResolution);
// 다시 비트맵팩토리 옵션을 복원
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPurgeable = true;
bitmapOptions.inDither = true;
final Bitmap bitmap = BitmapFactory.decodeFile(filePath, bitmapOptions);
return bitmap;
public static Bitmap safeDecodeBitmapFromResources(Resources res, int id, final int maxResolution) {
BitmapFactory.decodeResource(res, id, bitmapOptions);
final Bitmap bitmap = BitmapFactory.decodeResource(res, id, bitmapOptions);
private static void getSampleSize(BitmapFactory.Options bitmapOptions, int maxResolution) {
// 비트맵팩토리 옵션으로부터 원본 사이즈를 구한다.
final int bitmapWidth = bitmapOptions.outWidth;
final int bitmapHeight = bitmapOptions.outHeight;
// 원본 비트맵의 픽셀수가 maxPixels보다 크면 샘플 사이즈로 조절한다.
int maxPixels = maxResolution * maxResolution;
if (bitmapWidth * bitmapHeight > maxPixels) {
// 밑수를 0.5로 하는 LOG값
final Double value1 = Math.log(maxResolution / (double) Math.max(bitmapHeight, bitmapWidth)) / Math.log(0.5);
double sampleSize = Math.pow(2, (int) Math.round(value1));
bitmapOptions.inSampleSize = (int) sampleSize;
/**
* Bitmap 가로, 세로 사이즈를 리사이징 한다.
*
* @param srcBitmap
* 원본 Bitmap 객체
* @param maxResolution
* 제한 해상도
* @return 리사이즈된 이미지 Bitmap 객체
*/
static public Bitmap resizeBitmapImage(Bitmap srcBitmap, int maxResolution) {
// 원본 너비
int width = srcBitmap.getWidth();
// 원본 높이
int height = srcBitmap.getHeight();
// 리사이징 될 이미지의 너비
int newWidth = width;
// 리사이징 될 이미지의 높이
int newHeight = height;
// 리사이징 비율
float resizingRate = 0.0f;
/*
* 가로형 이미지
if (width > height) {
if (maxResolution < width) {
resizingRate = maxResolution / (float) width;
newHeight = (int) (height * resizingRate);
newWidth = maxResolution;
* 새로형 이미지
else {
if (maxResolution < height) {
resizingRate = maxResolution / (float) height;
newWidth = (int) (width * resizingRate);
newHeight = maxResolution;
return Bitmap.createScaledBitmap(srcBitmap, newWidth, newHeight, true);
* 해당 가로크기로 종횡비를 유지한 비트맵을 반환합니다.
* @param horizontalResolution
* @return
static public Bitmap resizeBitmapByHorizontal(Bitmap srcBitmap, int horizontalResolution) {
* 리사이즈
resizingRate = horizontalResolution / (float) width;
newWidth = horizontalResolution;
* 해당 세로크기로 종횡비를 유지한 비트맵을 반환합니다.
* @param verticalResolution
static public Bitmap resizeBitmapByVertical(Bitmap srcBitmap, int verticalResolution) {
resizingRate = verticalResolution / (float) height;
newHeight = verticalResolution;
로그인 유지
그냥 바로 불러서 이미지뷰에 셋팅하셨나요? 아니면 적절한 크기로 리사이징해서 셋팅하셨나요?
bitmap options 를 이용한 리사이징해서 불러오는 소스 투척합니다.
maxResolution은 800*480 이미지로 셋팅 할거라면 800*480의 루트인 619 넣어주시면 됩니다.
public static Bitmap safeDecodeBitmapFromFile(String filePath, final int maxResolution) {
// File이 null이면 null리턴
if (!new File(filePath).exists()) {
Log.e(TAG, "File 이 존재하지 않습니다.");
return null;
}
// 비트맵팩토리 옵션을 디코드시 크기만 반환하도록 설정.
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
// 크기를 구한다.
BitmapFactory.decodeFile(filePath, bitmapOptions);
getSampleSize(bitmapOptions, maxResolution);
// 다시 비트맵팩토리 옵션을 복원
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPurgeable = true;
bitmapOptions.inDither = true;
final Bitmap bitmap = BitmapFactory.decodeFile(filePath, bitmapOptions);
return bitmap;
}
public static Bitmap safeDecodeBitmapFromResources(Resources res, int id, final int maxResolution) {
// 비트맵팩토리 옵션을 디코드시 크기만 반환하도록 설정.
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
// 크기를 구한다.
BitmapFactory.decodeResource(res, id, bitmapOptions);
getSampleSize(bitmapOptions, maxResolution);
// 다시 비트맵팩토리 옵션을 복원
bitmapOptions.inJustDecodeBounds = false;
bitmapOptions.inPurgeable = true;
bitmapOptions.inDither = true;
final Bitmap bitmap = BitmapFactory.decodeResource(res, id, bitmapOptions);
return bitmap;
}
private static void getSampleSize(BitmapFactory.Options bitmapOptions, int maxResolution) {
// 비트맵팩토리 옵션으로부터 원본 사이즈를 구한다.
final int bitmapWidth = bitmapOptions.outWidth;
final int bitmapHeight = bitmapOptions.outHeight;
// 원본 비트맵의 픽셀수가 maxPixels보다 크면 샘플 사이즈로 조절한다.
int maxPixels = maxResolution * maxResolution;
if (bitmapWidth * bitmapHeight > maxPixels) {
// 밑수를 0.5로 하는 LOG값
final Double value1 = Math.log(maxResolution / (double) Math.max(bitmapHeight, bitmapWidth)) / Math.log(0.5);
double sampleSize = Math.pow(2, (int) Math.round(value1));
bitmapOptions.inSampleSize = (int) sampleSize;
}
}
/**
* Bitmap 가로, 세로 사이즈를 리사이징 한다.
*
* @param srcBitmap
* 원본 Bitmap 객체
* @param maxResolution
* 제한 해상도
* @return 리사이즈된 이미지 Bitmap 객체
*/
static public Bitmap resizeBitmapImage(Bitmap srcBitmap, int maxResolution) {
// 원본 너비
int width = srcBitmap.getWidth();
// 원본 높이
int height = srcBitmap.getHeight();
// 리사이징 될 이미지의 너비
int newWidth = width;
// 리사이징 될 이미지의 높이
int newHeight = height;
// 리사이징 비율
float resizingRate = 0.0f;
/*
* 가로형 이미지
*/
if (width > height) {
if (maxResolution < width) {
resizingRate = maxResolution / (float) width;
newHeight = (int) (height * resizingRate);
newWidth = maxResolution;
}
}
/*
* 새로형 이미지
*/
else {
if (maxResolution < height) {
resizingRate = maxResolution / (float) height;
newWidth = (int) (width * resizingRate);
newHeight = maxResolution;
}
}
return Bitmap.createScaledBitmap(srcBitmap, newWidth, newHeight, true);
}
/**
* 해당 가로크기로 종횡비를 유지한 비트맵을 반환합니다.
*
* @param srcBitmap
* @param horizontalResolution
* @return
*/
static public Bitmap resizeBitmapByHorizontal(Bitmap srcBitmap, int horizontalResolution) {
// 원본 너비
int width = srcBitmap.getWidth();
// 원본 높이
int height = srcBitmap.getHeight();
// 리사이징 될 이미지의 너비
int newWidth = width;
// 리사이징 될 이미지의 높이
int newHeight = height;
// 리사이징 비율
float resizingRate = 0.0f;
/*
* 리사이즈
*/
resizingRate = horizontalResolution / (float) width;
newWidth = horizontalResolution;
newHeight = (int) (height * resizingRate);
return Bitmap.createScaledBitmap(srcBitmap, newWidth, newHeight, true);
}
/**
* 해당 세로크기로 종횡비를 유지한 비트맵을 반환합니다.
*
* @param srcBitmap
* @param verticalResolution
* @return
*/
static public Bitmap resizeBitmapByVertical(Bitmap srcBitmap, int verticalResolution) {
// 원본 너비
int width = srcBitmap.getWidth();
// 원본 높이
int height = srcBitmap.getHeight();
// 리사이징 될 이미지의 너비
int newWidth = width;
// 리사이징 될 이미지의 높이
int newHeight = height;
// 리사이징 비율
float resizingRate = 0.0f;
/*
* 리사이즈
*/
resizingRate = verticalResolution / (float) height;
newHeight = verticalResolution;
newWidth = (int) (width * resizingRate);
return Bitmap.createScaledBitmap(srcBitmap, newWidth, newHeight, true);
}