Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缩小ImageButtons中的位图以避免内存不足_Java_Android_Button_Bitmap_Out Of Memory - Fatal编程技术网

Java 缩小ImageButtons中的位图以避免内存不足

Java 缩小ImageButtons中的位图以避免内存不足,java,android,button,bitmap,out-of-memory,Java,Android,Button,Bitmap,Out Of Memory,我这里有一些按钮 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); 并

我这里有一些按钮

 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(我的MainScreen.java)结束:

更新

我的主要活动:

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

如果我希望小屏幕具有小分辨率,而大屏幕具有大分辨率,如何设置比例?您需要使用insample大小。是的,在那之后,我必须告诉位图它有多大,正如你在第一条评论中提到的。请你看一下我更新的问题,我不知道,我是否必须在每一张新图片上使用insample大小,或者我必须做什么,因为我认为我不能像现在这样复制样本..?我认为你使用选项。inSampleSize=calculateInSampleSize(高度、宽度);因此,我在代码中添加了这一行,正如您在我现在更新的问题中所看到的那样:options.inSampleSize=calculateInSampleSize(options,imageHeight,imageWidth);但我还是得到了错误。。?
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;
}