gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:52:16 26 4
gpt4 key购买 nike

我遵循了本教程 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...) 但是我在这一行得到了 Null Pointer Exception。

最佳答案

尝试这段代码可能对你有帮助它在 View 寻呼机中绑定(bind)了两个 ImageView How to do pinching zoom and swipe on multiple imageview in android? .

关于android - 如何缩小 ViewPager Android 中使用的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com