2012-09-01 4 views
0

Android 게임을 만들고 있지만 비트 맵을로드 할 때 메모리 오류가 발생합니다. 이 문제는 매우 큰 비트 맵 (게임 배경 임)으로 인해 발생하지만, 어떻게하면 "비트 맵 크기가 VM 예산을 늘리는"오류를 일으킬 수 있는지 알 수 없습니다. 나는 배경을 더 작게 만들 수 없기 때문에 비트 맵을 작게 만들기 위해 비트 맵을 재조정 할 수 없다. 어떤 제안? 당신은 이미지를 다운 샘플링해야 할거야비트 맵 메모리 오류 Android

space = BitmapFactory.decodeResource(context.getResources(), 
      R.drawable.background); 
    space = Bitmap.createScaledBitmap(space, 
      (int) (space.getWidth() * widthRatio), 
      (int) (space.getHeight() * heightRatio), false); 
+0

SampleSize를 사용해 보셨습니까? http://developer.android.com/training/displaying-bitmaps/load-bitmap.html –

답변

0

:

오, 그래

, 여기가 오류를 발생시키는 코드입니다. 화면보다 작게 "축소"할 수는 없지만 작은 화면의 경우에는 큰 화면처럼 높은 해상도 일 필요는 없습니다.

간단히 말해서 downSample하려면 inSampleSize 옵션을 사용해야합니다. 이미지가 화면에 맞으면 실제로는 꽤 쉽습니다.

final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    final Display display = wm.getDefaultDisplay(); 

    final int dimension = Math.max(display.getHeight(), display.getWidth()); 

    final Options opt = new BitmapFactory.Options(); 
    opt.inJustDecodeBounds = true; 

    InputStream bitmapStream = /* input stream for bitmap */; 
    BitmapFactory.decodeStream(bitmapStream, null, opt); 
    try 
    { 
     bitmapStream.close(); 
    } 
    catch (final IOException e) 
    { 
     // ignore 
    } 

    final int imageHeight = opt.outHeight; 
    final int imageWidth = opt.outWidth; 

    int exactSampleSize = 1; 
    if (imageHeight > dimension || imageWidth > dimension) 
    { 
     if (imageWidth > imageHeight) 
     { 
      exactSampleSize = Math.round((float) imageHeight/(float) dimension); 
     } 
     else 
     { 
      exactSampleSize = Math.round((float) imageWidth/(float) dimension); 
     } 
    } 

    opt.inSampleSize = exactSampleSize; // if you find a nearest power of 2, the sampling will be more efficient... on the other hand math is hard. 
    opt.inJustDecodeBounds = false; 

    bitmapStream = /* new input stream for bitmap, make sure not to re-use the stream from above or this won't work */; 
    final Bitmap img = BitmapFactory.decodeStream(bitmapStream, null, opt); 

    /* Now go clean up your open streams... :) */ 

희망이 있습니다.

+0

감사합니다! 어떻게하면 비트 맵에서 inputStream을 얻을 수 있습니까? – user1404512

+0

비트 맵 리소스입니까? 실제로, 당신은 리소스를 잡거나 다른 것을 잡을 수있는 다른 BitmapFactory.decodeStream 메소드를 사용할 수 있습니다.) – xbakesx

0
  • 나는 왜 당신이 ImageBitmap을 사용하고 있는지 이해하지 못합니까? 배경에 대 한. 필요한 경우, 알았어. 그렇지 않으면 Layout을 사용하고 배경 이미지를 사용하고 있기 때문에 배경을 설정하십시오. 이것은 중요합니다. (안드로이드 문서를 확인하십시오. 그들은 명확하게이 문제를 지적했다.)

당신은이 작업을 수행 할 수 텐트 크기가 16메가바이트 같습니다 대부분의 안드로이드 기기에서 방법

Drawable d = getResources().getDrawable(R.drawable.your_background); 
backgroundRelativeLayout.setBackgroundDrawable(d);