메신저.장치에 비트 맵을 만드는 동안 메모리가 부족합니다. 고해상도 이미지에 문제가있는
1280x720 이미지의 경우 nodpi-drawable 폴더를 사용하고이 코드를 사용하여 크기를 조정하십시오.
public static Drawable scaleDrawable(Drawable d, int width, Activity cxt)
{
BitmapDrawable bd = (BitmapDrawable)d;
double oldWidth = bd.getBitmap().getWidth();
double scaleFactor = width/oldWidth;
int newHeight = (int) (d.getIntrinsicHeight() * scaleFactor);
int newWidth = (int) (oldWidth * scaleFactor);
Drawable drawable = new BitmapDrawable(cxt.getResources(),MainScreen.getResizedBitmap(bd.getBitmap(),newHeight,newWidth));
BitmapDrawable bd2 = (BitmapDrawable)drawable;
return drawable;
}
public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
내가 화면이 320 × 480 인 경우 이미지가 320로 확장 및 비율을 유지할 수 있도록 witdh 화면에 이미지를 확장하는 코드를 사용, 난 이미지가 바닥에서 화면 밖으로 이동하면 걱정 말아.
잘 작동하지만 xhdpi 장치에서 정확히 720x1280의 화면이있는 삼성 Galaxy Note 2를 사용하려고 할 때 유용합니다. 라인에서 메모리 부족 예외와
그것은 충돌 :
이Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
내가 이미지가 720에서 720로 확장해야하지만 내 코드를 최적화하거나 뭔가 정말 나쁜해야하는 이유, 이해하지 못할.
나는 havent 1080x1920 장치에 시도했지만 너무 추락 것 같습니다.
누군가 코드를 보면 뭔가 나빠질 수 있습니다.
내 이미지는 drawable-nodpi에 저장된 드로어 블이며, 그 방법은 내게 맞는 경로에서 decodeFile을 사용합니다. ? 그리고 어떻게? – Nanoc
BitmapFactory.decodeResource (resources res, int id)를 사용하면 폴더에서 드로어 블에 액세스 할 수 있습니다. – Horschtele