将可绘制列表传递给decodeSampledBitmapFromResource(getResources(),drawable,50,50);

时间:2016-02-19 08:37:04

标签: java android datepicker

我正在制作一个包含多个ImageButtons的布局但是当我运行它时由于java.lang.OutOfMemoryError而崩溃:我试图使堆大小=大,它工作但应用程序变慢,因为它占用大量内存尺寸现在我想使用下面的方法重新调整所有这些图像的大小,我不知道如何给它所有的资源我

decodeSampledBitmapFromResource(getResources(),R.drawable(), 50, 50);

但我不知道如何在

中更改第二个参数
for (int i = 0; i < 10; i++) {
   decodeSampledBitmapFromResource(getResources(),R.drawable(), 50, 50);
}

所以我可以调整所有图像的大小

这是我的代码资源(家庭,浪漫,预算,奢侈品,娱乐,娱乐,积雪,文化):

公共类Main3Activity扩展了AppCompatActivity {

int year ;
int month ;
int day ;

int [] drawable = new int [] {R.drawable.family,R.drawable.romance,R.drawable.beach,         R.drawable.budget,R.drawable.snow_activity,R.drawable.amusement,         R.drawable.luxury,R.drawable.entertainment,R.drawable.culture};

ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    Bundle extras = getIntent().getExtras();

    year = extras.getInt("year");
    day = extras.getInt("day");
    month = extras.getInt("month");



    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    BitmapFactory.decodeResource(getResources(), R.id.family, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    String imageType = options.outMimeType;

    BitmapFactory.Options options1 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.romance, options);
    int imageHeight1 = options.outHeight;
    int imageWidth1 = options.outWidth;
    String imageType1 = options.outMimeType;

    BitmapFactory.Options options2 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.culture, options);
    int imageHeight2 = options.outHeight;
    int imageWidth2 = options.outWidth;
    String imageType2 = options.outMimeType;

    BitmapFactory.Options options3 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight3 = options.outHeight;
    int imageWidth3 = options.outWidth;
    String imageType3 = options.outMimeType;

    BitmapFactory.Options options4 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight4 = options.outHeight;
    int imageWidth4 = options.outWidth;
    String imageType4 = options.outMimeType;

    BitmapFactory.Options options5 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight5 = options.outHeight;
    int imageWidth5 = options.outWidth;
    String imageType5  = options.outMimeType;

    BitmapFactory.Options options6 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight6 = options.outHeight;
    int imageWidth6 = options.outWidth;
    String imageType6 = options.outMimeType;

    BitmapFactory.Options options7 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight7 = options.outHeight;
    int imageWidth7 = options.outWidth;
    String imageType7 = options.outMimeType;

    BitmapFactory.Options options8 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight8 = options.outHeight;
    int imageWidth8 = options.outWidth;
    String imageType8 = options.outMimeType;

    for (int i = 0; i < 10; i++) {



        imageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), drawable[i], 50, 50));


    }

    calculateInSampleSize( options , imageWidth , imageHeight);
    calculateInSampleSize( options1 , imageWidth1 , imageHeight1);
    calculateInSampleSize( options2 , imageWidth2 , imageHeight2);
    calculateInSampleSize( options3 , imageWidth3 , imageHeight3);
    calculateInSampleSize( options4 , imageWidth4 , imageHeight4);
    calculateInSampleSize( options5 , imageWidth5 , imageHeight5);
    calculateInSampleSize( options6 , imageWidth6 , imageHeight6);
    calculateInSampleSize(options7, imageWidth7, imageHeight7);
    calculateInSampleSize(options8, imageWidth8, imageHeight8);







}




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 = 4;

    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 :(得分:0)

这是正确的方法:

公共类Main3Activity扩展了AppCompatActivity {

int year ;
int month ;
int day ;












@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    Bundle extras = getIntent().getExtras();

    year = extras.getInt("year");
    day = extras.getInt("day");
    month = extras.getInt("month");
    ImageButton imageButton= (ImageButton) findViewById(R.id.family)  ; ;
    ImageButton imageButton1 = (ImageButton)findViewById(R.id.romance);
    ImageButton imageButton2 = (ImageButton)findViewById(R.id.culture);
    ImageButton imageButton3 = (ImageButton)findViewById(R.id.snow);
    ImageButton imageButton4 = (ImageButton)findViewById(R.id.beach);
    ImageButton imageButton5 = (ImageButton)findViewById(R.id.budget);
    ImageButton imageButton6 = (ImageButton)findViewById(R.id.entertament);
    ImageButton imageButton7 = (ImageButton)findViewById(R.id.amusement);
    ImageButton imageButton8 = (ImageButton)findViewById(R.id.luxury);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    BitmapFactory.decodeResource(getResources(), R.id.family, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    String imageType = options.outMimeType;

    BitmapFactory.Options options1 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.romance, options);
    int imageHeight1 = options.outHeight;
    int imageWidth1 = options.outWidth;
    String imageType1 = options.outMimeType;

    BitmapFactory.Options options2 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.culture, options);
    int imageHeight2 = options.outHeight;
    int imageWidth2 = options.outWidth;
    String imageType2 = options.outMimeType;

    BitmapFactory.Options options3 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight3 = options.outHeight;
    int imageWidth3 = options.outWidth;
    String imageType3 = options.outMimeType;

    BitmapFactory.Options options4 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight4 = options.outHeight;
    int imageWidth4 = options.outWidth;
    String imageType4 = options.outMimeType;

    BitmapFactory.Options options5 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight5 = options.outHeight;
    int imageWidth5 = options.outWidth;
    String imageType5  = options.outMimeType;

    BitmapFactory.Options options6 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight6 = options.outHeight;
    int imageWidth6 = options.outWidth;
    String imageType6 = options.outMimeType;

    BitmapFactory.Options options7 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight7 = options.outHeight;
    int imageWidth7 = options.outWidth;
    String imageType7 = options.outMimeType;

    BitmapFactory.Options options8 = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.id.beach, options);
    int imageHeight8 = options.outHeight;
    int imageWidth8 = options.outWidth;
    String imageType8 = options.outMimeType;





    imageButton.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.family, 100, 100));
    imageButton1.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.romance, 100, 100));
    imageButton2.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.culture,100, 100));
    imageButton3.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.budget, 100, 100));
    imageButton4.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.budget, 100, 100));
    imageButton5.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.entertainment, 100, 100));
    imageButton6.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.snow_activity, 100, 100));
    imageButton7.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.amusement, 100, 100));
    imageButton8.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.luxury, 100, 100));





    calculateInSampleSize( options , imageWidth , imageHeight);
    calculateInSampleSize( options1 , imageWidth1 , imageHeight1);
    calculateInSampleSize( options2 , imageWidth2 , imageHeight2);
    calculateInSampleSize( options3 , imageWidth3 , imageHeight3);
    calculateInSampleSize( options4 , imageWidth4 , imageHeight4);
    calculateInSampleSize( options5 , imageWidth5 , imageHeight5);
    calculateInSampleSize( options6 , imageWidth6 , imageHeight6);
    calculateInSampleSize(options7, imageWidth7, imageHeight7);
    calculateInSampleSize(options8, imageWidth8, imageHeight8);







}




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 = 4;

    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);
}

}