2012-03-19 29 views
0

私はこの活動のためにこの背景イメージを手に入れました。その480x800 pngです。 それに勾配があるので、バンディングの危険があります。そのため、私は99%不透明にして、最良のカラーモードを強制しました。背景画像メモリサイズ

私のデバイスでは、HTCマジックでさえ、これは問題ありません。

しかし、デフォルトの1.6エミュレータでは、メモリエラーが発生します。何をすべきか? 背景はとのコードに設定されます。解決策ではないよう256に192とデバイスRAMサイズに最大VMヒープの設定

bgView.setImageResource(R.drawable.baby_pink_solid); 

+0

は、バックグラウンドでのイメージビューではなく、バックグラウンドでのイメージです。なぜあなたはイメージビューを使用しますか? – njzk2

答えて

0

は、コード内のビットマップにアクセスしてみてください、その後、setImageBitmap()経由で設定します。コードでビットマップをデコードしているときにOOMを取得した場合、これはsetImageResource()から取得する理由です。

私は、ビットマップは、Android上で扱うためのトリッキーなものであることを発見した、とあなたはそれらを使用する際に注意する必要があります!

また、@Sadeshkumar Periyasamyの答えをチェックし、これはビットマップまたは今日のデバイスと同じくらいのパワーを持っていないデバイスのために、より大きなサイズをデコードするために有用です。

0

は、任意のビットマップを拡大縮小するために、このコードを試してみてください。

public class ImageScale 
{ 
/** 
* Decodes the path of the image to Bitmap Image. 
* @param imagePath : path of the image. 
* @return Bitmap image. 
*/ 
public Bitmap decodeImage(String imagePath) 
{ 
    Bitmap bitmap=null; 
    try 
    { 

     File file=new File(imagePath); 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 

     BitmapFactory.decodeStream(new FileInputStream(file),null,o); 
     final int REQUIRED_SIZE=200; 
     int width_tmp=o.outWidth, height_tmp=o.outHeight; 

     int scale=1; 
     while(true) 
     { 
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
      break; 
      width_tmp/=2; 
      height_tmp/=2; 
      scale*=2; 
     } 

     BitmapFactory.Options options=new BitmapFactory.Options(); 

     options.inSampleSize=scale; 
     bitmap=BitmapFactory.decodeStream(new FileInputStream(file), null, options); 

    } 
    catch(Exception e) 
    { 
     bitmap = null; 
    }  
    return bitmap; 
} 

/** 
    * Resizes the given Bitmap to Given size. 
    * @param bm : Bitmap to resize. 
    * @param newHeight : Height to resize. 
    * @param newWidth : Width to resize. 
    * @return Resized Bitmap. 
    */ 
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) 
{ 

    Bitmap resizedBitmap = null; 
    try 
    { 
     if(bm!=null) 
     { 
      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 
resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,  true); 
// resizedBitmap = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true); 
     } 
    } 
    catch(Exception e) 
    { 
     resizedBitmap = null; 
    } 

    return resizedBitmap; 
} 

}