【问题标题】:How to scaled down images used in ViewPager Android?如何缩小 ViewPager Android 中使用的图像?
【发布时间】:2026-01-28 23:40:01
【问题描述】:

我按照这个教程http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/

我有一个看起来像这样的寻呼机适配器(代码如下),我收​​到一条错误消息,提示我的图像太大。 (java.lang.OutOfMemoryError:位图大小超出 VM 预算。

我知道很多人问过这个问题,我用谷歌搜索并找到了很多解决方案。但是,我似乎无法正确运行它。我是新手,希望在这里得到一些答案,因为我不知道我还能做什么。

位图解码适用于图像,但如果我的图像采用如下布局怎么办。如何缩小图像?

public class TutorialPagerAdapter extends PagerAdapter {

Activity activity;
int imageArray[];
private Resources resource;  


    private class MyPagerAdapter extends PagerAdapter {
    public int getCount() {
        return 5;
    }
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.farleft;
        try{
            mImageView = (ImageView) collection.findViewById(R.id.tutorial);
            mImageView.setImageBitmap(decodeSampledBitmapFromResource(activity.getResources(), R.id.tutorial, 100, 100));
            }
            catch (NullPointerException e)
            {
                e.printStackTrace();
            }

            break;
        case 1:
            resId = R.layout.left;
            break;
        case 2:
            resId = R.layout.middle;
            break;
        case 3:
            resId = R.layout.right;
            break;
        case 4:
            resId = R.layout.farright;
            break;
        }
        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);
        return view;
    }
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }
    @Override
    public Parcelable saveState() {
        return null;
    }


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

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

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

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(res, 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) {
    if (width > height) {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    } else {
        inSampleSize = Math.round((float)width / (float)reqWidth);
                  }
                      }
return inSampleSize;}

}

编辑

我尝试了mImageView.setImageBitmap(decodeSampledBitmapFromResource...),但在这一行出现空指针异常。

【问题讨论】:

    标签: android android-viewpager


    【解决方案1】:

    试试这个代码可能对你有帮助它在视图寻呼机How to do pinching zoom and swipe on multiple imageview in android?中绑定了两个imageview。

    【讨论】:

    • 我试过那个人用的方法,但是好像不行。我尝试了mImageView.setImageBitmap(decodeSampledBitmapFromResource...),但我得到了 NullPointerException。我实际上在每个布局中有 2 个图像。我总共有 9 个布局。
    • mImageView.setImageBitmap(decodeSampledBitmapFromResource(activity.getResources(), R.drawable.ic_launcher, 100, 100));替换它