1

我按照本教程http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizo​​ntal-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...)了,但我在这一行得到了空指针异常。

4

1 回答 1

1

试试这个代码可能对你有帮助它在视图寻呼机中绑定了两个imageview如何在android中的多个imageview上进行捏缩放和滑动?.

于 2012-12-19T06:23:04.927 回答