缩小ImageButtons中的位图以避免OutOfMemory

时间:2015-04-10 17:52:07

标签: java android button bitmap out-of-memory

我这里有一些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);

并希望在其中缩放图片,以避免OutOfMemory。

我已尝试使用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);
    }
 }

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

1 个答案:

答案 0 :(得分:2)

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或按钮