1

このチュートリアルに従いましたhttp://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizo​​ntal-view-paging/

このようなページャー アダプター (以下のコード) がありますが、イメージが大きすぎるというエラーが表示されます。( java.lang.OutOfMemoryError: ビットマップ サイズが VM の予算を超えています。 )

多くの人がこの質問をしたことを知っています.Googleで検索して多くの解決策を見つけました. しかし、私はそれを正しく実行できなかったようです。私は新しく、他に何をすべきかわからないので、ここでいくつかの答えを得たいと思っています。

ビットマップ デコードは画像用ですが、私の画像が以下のようなレイアウトになっている場合はどうでしょうか。画像を縮小するにはどうすればよいですか?

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...)ましたが、この行で Null Pointer Exception が発生します。

4

1 に答える 1

1

このコードを試してみてください。ビューページャーで2つのイメージビューにバインドされているので、役立つかもしれませんAndroidで複数のイメージビューでピンチズームとスワイプを行う方法は? .

于 2012-12-19T06:23:04.927 に答える