2017-10-08 45 views
0

我在加載大圖像時出現問題。 我必須製作一個尺寸爲3556 x 2000像素的地圖/背景。大滾動圖像android studio

我試試這個: https://developer.android.com/topic/performance/graphics/load-bitmap.html 但它看起來不適合我。 (例外:內存不足)

這是我的背景: scr.hu/8p0mdz - Screenshooter

我打上黑色方塊是手機用戶上可見的區域。當然,用戶可以放大或縮小可見區域。

我不能使用libgdx。我只想使用android庫。我不知道我該如何開始我的工作。我不要求一個代碼(我會很樂意接受這個代碼),但是我想知道我應該從哪裏開始。

在此背景下,將繪製其他圖像(建築物)。當遊戲開始時,需要將資源加載到內存中。在libgdx中,我可以使用AssetManager。在當我使用

https://developer.android.com/reference/android/content/res/AssetManager.html

我的情況下,它應該是足夠了嗎?

我希望你能理解我的問題。

+0

你可以嘗試滑翔?看看它是否有效 –

+0

不,我甚至都沒聽說過。感謝幫助。如果我找不到解決方案,我會嘗試。 :) – Kielek

回答

0

這不是一個微不足道的問題,所以我認爲沒有簡單的解決方案。但是,是幾件事情,可以幫助:

  1. 您可以控制分配的內存量爲位圖:

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    options.inPreferredConfig = Bitmap.Config.RGB_565; 
    //RGB_565 color format requires way less memory than ARGB_8888 
    
  2. BitmapRegionDecoder類加載只是一個位圖的一部分。

    BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance("path", false); 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.RGB_565; 
    Bitmap partOfBitmap = decoder.decodeRegion(new Rect(0, 0, 100, 100), options); 
    

使用BitmapRegionDecoder可以開發定製View負責處理用戶滾動事件,並加載到內存中唯一可見的圖像區域,破壞不可見。

+0

我認爲這可以工作。但我不知道爲什麼位圖大小9335 x 5250,當我把3556 x 2000px。 – Kielek

0

我試圖解決這個如上:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_planet); 
    int WIDTH = 0; 
    int HEIGHT = 0; 
    backgroundJungle = (ImageView)findViewById(R.id.backgroundJungle); 

    InputStream is = null; 
    try { 
     Drawable drawable = getResources().getDrawable(R.drawable.junglemap); 
     BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; 
     Bitmap bitmap = bitmapDrawable.getBitmap(); 
     WIDTH = bitmap.getWidth(); 
     HEIGHT = bitmap.getHeight(); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
     is = new ByteArrayInputStream(stream.toByteArray()); 
    } catch (Exception ex) { 
     ManagerException(ex); 
    } 

    try { 
     if (is != null) { 
      BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inPreferredConfig = Bitmap.Config.RGB_565; 
      Bitmap partOfBitmap = decoder.decodeRegion(new Rect(0,0,2500,2500),options); 
      backgroundJungle.setImageBitmap(partOfBitmap); 
      Toast.makeText(getApplicationContext(), WIDTH + " " + HEIGHT, Toast.LENGTH_SHORT).show(); 
     } 
    } catch (IOException ex) { 
     ManagerException(ex); 
    } 
} 

但是位圖的大小爲9335 X 5250,但我的形象具有3556 X 2000