如何缩小ViewPager Android中使用的图像?

如何缩小ViewPager Android中使用的图像?,android,android-viewpager,Android,Android Viewpager,我遵循了这个教程 我有一个寻呼机适配器,看起来像这样(下面的代码),我得到一个错误,说我的图像太大。(java.lang.OutOfMemoryError:位图大小超出VM预算。) 我知道很多人问过这个问题,我在谷歌上搜索并找到了很多解决方案。然而,我似乎无法正确运行它。我是新来的,希望在这里得到一些答案,因为我不知道我还应该做什么 位图解码是针对图像的,但是如果我的图像在下面这样的布局中会怎么样呢。如何缩小图像的比例 public class TutorialPagerAdapter exte

我遵循了这个教程

我有一个寻呼机适配器,看起来像这样(下面的代码),我得到一个错误,说我的图像太大。(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(decodesampledbitmapfromrource…
,但在这一行出现了空指针异常

尝试此代码可能对您有帮助它在查看页面中绑定了两个imageview。

尝试此代码可能对您有帮助它在查看页面中绑定了两个imageview。

我尝试了此人使用的方法,但似乎不起作用。我尝试了
mImageView.setImageBitmap(decodeSampledBitmapFromResource…
,但是我得到了NullPointerException。实际上,我在每个布局中有两个图像。我总共有9个布局.mImageView.setImageBitmap(decodeSampledBitmapFromResource(activity.getResources(),R.drawable.ic_launcher,100100));替换itI我尝试了此人使用的方法,但似乎不起作用。我尝试了
mImageView.setImageBitmap(decodeSampledBitmapFromResource…
,但是我得到了NullPointerException。实际上,我在每个布局中有两个图像。我总共有9个布局.mImageView.setImageBitmap(decodeSampledBitmapFromResource(activity.getResources(),R.drawable.ic_launcher,100100));替换它