我正在创建一个幻灯片应用程序,它通过使用动画事件在同一个 ImageView 中的图像之间切换,在 ViewFlipper 中一次加载一个图像,但是当我按下后退按钮离开应用程序时,我总是收到此错误:
E/AndroidRuntime( 729): FATAL EXCEPTION: main
E/AndroidRuntime( 729): java.lang.OutOfMemoryError
E/AndroidRuntime( 729): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime( 729): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
E/AndroidRuntime( 729): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
E/AndroidRuntime( 729): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:449)
E/AndroidRuntime( 729): at br.com.vodia.mobilephotoframe.ConfigFragment.onActivityCreated(ConfigFragment.java:167)
E/AndroidRuntime( 729): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1486)
E/AndroidRuntime( 729): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
E/AndroidRuntime( 729): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
E/AndroidRuntime( 729): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
E/AndroidRuntime( 729): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
E/AndroidRuntime( 729): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
E/AndroidRuntime( 729): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1167)
E/AndroidRuntime( 729): at android.app.Activity.performStart(Activity.java:5216)
E/AndroidRuntime( 729): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
E/AndroidRuntime( 729): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
E/AndroidRuntime( 729): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3553)
E/AndroidRuntime( 729): at android.app.ActivityThread.access$700(ActivityThread.java:140)
E/AndroidRuntime( 729): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
E/AndroidRuntime( 729): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 729): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 729): at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime( 729): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 729): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 729): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime( 729): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime( 729): at dalvik.system.NativeStart.main(Native Method)
这是我的代码的一部分:
mSlideshow = (ViewFlipper) findViewById(R.id.slideshow);
if (currentImages.size() > 0) {
mSlideshow.setFlipInterval(interval);
mSlideshow.setInAnimation(this, android.R.anim.fade_in);
mSlideshow.setOutAnimation(this, android.R.anim.fade_out);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
mWidth = size.x;
mHeight = size.y;
mSlideshowImage = (ImageView) findViewById(R.id.slideshow_image);
mImageList = new ArrayList<String>(currentImages);
mCurrentImage = 0;
mCurrentBitmap = decodeSampledBitmapFromPath(mImageList.get(mCurrentImage), mWidth, mHeight);
mSlideshowImage.setImageBitmap(mCurrentBitmap);
mSlideshow.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if (mCurrentImage + 1 < mImageList.size()) {
mCurrentImage = mCurrentImage + 1;
} else {
mCurrentImage = 0;
}
if (mCurrentBitmap != null)
mCurrentBitmap.recycle();
mCurrentBitmap = decodeSampledBitmapFromPath(mImageList.get(mCurrentImage), mWidth, mHeight);
mSlideshowImage.setImageBitmap(mCurrentBitmap);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
//To change body of implemented methods use File | Settings | File Templates.
}
});
mSlideshow.startFlipping();
}
此代码已使用 inSampleSize 将位图的宽度和高度限制为显示分辨率。