缩小 ImageButtons 中的位图以避免 OutOfMemory

Scaling down Bitmaps in ImageButtons to avoid OutOfMemory

我这里有一些 ImageButtons

 ImageButton btn_start = (ImageButton) findViewById(R.id.btn_start);
 ImageButton btn_start.setOnClickListener(this);
 ImageButton btn_start2 = (ImageButton) findViewById(R.id.btn_start2);
 ImageButton btn_start2.setOnClickListener(this);

并想在其中缩放他们的图片,以避免内存不足。

我已经尝试过,使用 THIS,但它不起作用,意味着应用程序以 OutOfMemory(我的 MainScreen.java)结束:

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

BitmapFactory.decodeResource(getResources(), R.id.imageView1, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;


BitmapFactory.decodeResource(getResources(), R.drawable.btn_start, options);
BitmapFactory.decodeResource(getResources(), R.drawable.btn_start2, options);

btn_start.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.id.btn_start2));

btn_start2.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.id.btn_start3));

更新

我的主要活动:

public class MainScreen extends Activity  implements OnClickListener {
@Override
    public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

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

    BitmapFactory.decodeResource(getResources(), R.id.imageView1, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    String imageType = options.outMimeType;
     //options.inSampleSize=calculateInSampleSize(options,imageHeight,imageWidth);


    BitmapFactory.decodeResource(getResources(), R.drawable.start2, options);
    BitmapFactory.decodeResource(getResources(), R.drawable.start1, options);

     btn_start.setImageBitmap( BitmapFactory.decodeResource(getResources(),
            R.drawable.start1, options)); 
    btn_start2.setImageBitmap(
            BitmapFactory.decodeResource(getResources(), R.drawable.start2, options)); 
}


    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
 }

我已尝试将所有需要的内容都放入我的代码中,但目前还没有结果。

public void setupLogo(){
  Bitmap bmp = createBitmap();
  //YOU CAN CHANGE THIS TO A BUTTON, AND SET IMAGE FOR THE BUTTON
  ImageView imageView = (ImageView) R.id.imageViewFromLayout;
  imageView.setImageBitmap(bmp);
}

public Bitmap createBitmap(){

 BitmapFactory.Options options2 = new BitmapFactory.Options();                      
 options2.inJustDecodeBounds = true;
 //YOU CAN CHANGE THIS TO DECODE RESOURCE
 BitmapFactory.decodeFile(Main.this.getExternalFilesDir(filepath) +"/companylogo.png",options2);
 options2.inSampleSize = calculateInSampleSize();//=32
 options2.inJustDecodeBounds = false;
 //YOU CAN CHANGE THIS TO DECODE RESOURCE
 return BitmapFactory.decodeFile(Main.this.getExternalFilesDir(filepath) +"/companylogo.png",options2);
}

public int calculateInSampleSize() {

 DisplayMetrics displayMetrics = Main.this.getResources().getDisplayMetrics();
 int reqWidth = displayMetrics.widthPixels;
 //WILL OUTPUT THE IMAGE AT 100x100 PIXELS                      
 final int height = 100;
 final int width = 100;

 double devCal2 =  (height*1000)/width;
 int reqHeight = (int) ((devCal2/1000)*reqWidth); 

 int inSampleSize = 1;

 if (height > reqHeight || width > reqWidth) {

  final int halfHeight = height / 2;
  final int halfWidth = width / 2;

  // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  // height and width larger than the requested height and width.
  while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
   inSampleSize *= 2;
  }
 }
 return inSampleSize;
}

调用setupLogo();设置 bmp,然后将其分配给 ImageView 或按钮