我使用 ViewPager 显示资源文件夹中的一组图像,如果我的图像尺寸小,一切正常,
但是当我用我需要它在我的应用程序中的高清图像替换它时,它给了我这个错误:
java.lang.OutOfMemoryError:位图大小超出 VM 预算
注1:
我的代码中现在有 5 张图片用于测试,但最后我将有大约 30 张高清图片,
笔记2 :
我想知道为什么会发生这种情况,我是 android 新手,第一次使用 viewpager 类,然后我在另一个应用程序中使用了画廊类,其中包含 30 多张高清图像并且没有例外。
任何建议将不胜感激,非常感谢
我的代码:
logcat 堆栈:
FATAL EXCEPTION: main
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:563)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:462)
at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:488)
at com.test.demo.MyPagerAdapter.<init>(MyPagerAdapter.java:42)
at com.test.demo.MainActivity.onCreate(MainActivity.java:15)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
主要活动
public class MainActivity extends Activity {
private ViewPager mMyPager;
private MyPagerAdapter mMyPagerAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMyPager = (ViewPager) findViewById(R.id.mypages);
mMyPagerAdapter = new MyPagerAdapter(this);
mMyPager.setAdapter(mMyPagerAdapter); }}
我的页面适配器
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<ImageView> mViewsList;
private Context mContext = null;
public MyPagerAdapter(Context context) {
mContext = context;
mViewsList = new ArrayList<ImageView>();
Resources resource = mContext.getResources();
Bitmap bMap1 = BitmapFactory.decodeResource(resource,
R.drawable.one);
ImageView image1 = new ImageView(mContext);
image1.setImageBitmap(bMap1);
mViewsList.add(image1);
Bitmap bMap2 = BitmapFactory.decodeResource(resource,
R.drawable.two );
ImageView image2 = new ImageView(mContext);
image2.setImageBitmap(bMap2);
mViewsList.add(image2);
Bitmap bMap3 = BitmapFactory.decodeResource(resource,
R.drawable.three);
ImageView image3 = new ImageView(mContext);
image3.setImageBitmap(bMap3);
mViewsList.add(image3);
Bitmap bMap4 = BitmapFactory.decodeResource(resource,
R.drawable.four);
ImageView image4 = new ImageView(mContext);
image4.setImageBitmap(bMap4);
mViewsList.add(image4);
Bitmap bMap5 = BitmapFactory.decodeResource(resource,
R.drawable.five);
ImageView image5 = new ImageView(mContext);
image5.setImageBitmap(bMap5);
mViewsList.add(image5);
}
@Override
public int getCount() {
return mViewsList.size();
}
@Override
public Object instantiateItem(View view, int position) {
View myView = mViewsList.get(position);
((ViewPager) view).addView(myView);
return myView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(View view, int arg1, Object object) {
((ViewPager) view).removeView((ImageView) object);
}
}