Android imageview / addview outofmemory帮助?

时间:2011-03-18 06:30:54

标签: android


我正在开发一个简单的漫画书类型的应用程序 我是创建Android应用程序的新手,所以请帮帮我 我编码的可能是这样做的错误方法,但我希望能够用手指向左和向右推进 屏幕前进页面或后退页面。我遇到的问题是,在大约80-85张图片后,该应用程序将会 不再跑了,我猜是因为内存不足?非常感激任何的帮助! 谢谢 (正在这样做的部分我认为是我正在使用的事实:

viewFlipper = new ViewFlipper(this);
ImageView i1 = new ImageView(this);
i1.setImageResource(R.drawable.s1);
viewFlipper.addView(i1);

我的res / drawable文件夹中有120张图片......我做错了!

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

      private static final int SWIPE_MIN_DISTANCE = 120;
       private static final int SWIPE_MAX_OFF_PATH = 250;
       private static final int SWIPE_THRESHOLD_VELOCITY = 200;
       private GestureDetector gestureDetector;
       View.OnTouchListener gestureListener;

       private Animation slideLeftIn;
       private Animation slideLeftOut;
       private Animation slideRightIn;
       private Animation slideRightOut;
       private ViewFlipper viewFlipper;

    @Override
       public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);

           viewFlipper = new ViewFlipper(this);
           ImageView i1 = new ImageView(this);
           i1.setImageResource(R.drawable.s1);
           ImageView i2 = new ImageView(this);
           i2.setImageResource(R.drawable.s2);
           ImageView i3 = new ImageView(this);
           i3.setImageResource(R.drawable.s3);
           ImageView i4 = new ImageView(this);
           i4.setImageResource(R.drawable.s4);
           ImageView i5 = new ImageView(this);
           i5.setImageResource(R.drawable.s5);
           ImageView i6 = new ImageView(this);
           i6.setImageResource(R.drawable.s6);
           ImageView i7 = new ImageView(this);
           i7.setImageResource(R.drawable.s7);
           ImageView i8 = new ImageView(this);
           i8.setImageResource(R.drawable.s8);
           ImageView i9 = new ImageView(this);
           i9.setImageResource(R.drawable.s9);
           ImageView i10 = new ImageView(this);
           i10.setImageResource(R.drawable.s10);
           ImageView i11 = new ImageView(this);
           i11.setImageResource(R.drawable.s11);
           ImageView i12 = new ImageView(this);
           i12.setImageResource(R.drawable.s12);
           ImageView i13 = new ImageView(this);
           i13.setImageResource(R.drawable.s13);
           ImageView i14 = new ImageView(this);
           i14.setImageResource(R.drawable.s14);
           ImageView i15 = new ImageView(this);
           i15.setImageResource(R.drawable.s15);
           ImageView i16 = new ImageView(this);
           i16.setImageResource(R.drawable.s16);
           ImageView i17 = new ImageView(this);
           i17.setImageResource(R.drawable.s17);
           ImageView i18 = new ImageView(this);
           i18.setImageResource(R.drawable.s18);
           ImageView i19 = new ImageView(this);
           i19.setImageResource(R.drawable.s19);
           ImageView i20 = new ImageView(this);
           i20.setImageResource(R.drawable.s20);
           ImageView i21 = new ImageView(this);
           i21.setImageResource(R.drawable.s21);
           ImageView i22 = new ImageView(this);
           i22.setImageResource(R.drawable.s22);
           ImageView i23 = new ImageView(this);
           i23.setImageResource(R.drawable.s23);
           ImageView i24 = new ImageView(this);
           i24.setImageResource(R.drawable.s24);
           ImageView i25 = new ImageView(this);
           i25.setImageResource(R.drawable.s25);
           ImageView i26 = new ImageView(this);
           i26.setImageResource(R.drawable.s26);
           ImageView i27 = new ImageView(this);
           i27.setImageResource(R.drawable.s27);
           ImageView i28 = new ImageView(this);
           i28.setImageResource(R.drawable.s28);
           ImageView i29 = new ImageView(this);
           i29.setImageResource(R.drawable.s29);
           ImageView i30 = new ImageView(this);
           i30.setImageResource(R.drawable.s30);
           ImageView i31 = new ImageView(this);
           i31.setImageResource(R.drawable.s31);
           ImageView i32 = new ImageView(this);
           i32.setImageResource(R.drawable.s32);
           ImageView i33 = new ImageView(this);
           i33.setImageResource(R.drawable.s33);
           ImageView i34 = new ImageView(this);
           i34.setImageResource(R.drawable.s34);
           ImageView i35 = new ImageView(this);
           i35.setImageResource(R.drawable.s35);
           ImageView i36 = new ImageView(this);
           i36.setImageResource(R.drawable.s36);
           ImageView i37 = new ImageView(this);
           i37.setImageResource(R.drawable.s37);
           ImageView i38 = new ImageView(this);
           i38.setImageResource(R.drawable.s38);
           ImageView i39 = new ImageView(this);
           i39.setImageResource(R.drawable.s39);
           ImageView i40 = new ImageView(this);
           i40.setImageResource(R.drawable.s40);
           ImageView i41 = new ImageView(this);
           i41.setImageResource(R.drawable.s41);
           ImageView i42 = new ImageView(this);
           i42.setImageResource(R.drawable.s42);
           ImageView i43 = new ImageView(this);
           i43.setImageResource(R.drawable.s43);
           ImageView i44 = new ImageView(this);
           i44.setImageResource(R.drawable.s44);
           ImageView i45 = new ImageView(this);
           i45.setImageResource(R.drawable.s45);
           ImageView i46 = new ImageView(this);
           i46.setImageResource(R.drawable.s46);
           ImageView i47 = new ImageView(this);
           i47.setImageResource(R.drawable.s47);
           ImageView i48 = new ImageView(this);
           i48.setImageResource(R.drawable.s48);
           ImageView i49 = new ImageView(this);
           i49.setImageResource(R.drawable.s49);
           ImageView i50 = new ImageView(this);
           i50.setImageResource(R.drawable.s50);
           ImageView i51 = new ImageView(this);
           i51.setImageResource(R.drawable.s51);
           ImageView i52 = new ImageView(this);
           i52.setImageResource(R.drawable.s52);
           ImageView i53 = new ImageView(this);
           i53.setImageResource(R.drawable.s53);
           ImageView i54 = new ImageView(this);
           i54.setImageResource(R.drawable.s54);
           ImageView i55 = new ImageView(this);
           i55.setImageResource(R.drawable.s55);
           ImageView i56 = new ImageView(this);
           i56.setImageResource(R.drawable.s56);
           ImageView i57 = new ImageView(this);
           i57.setImageResource(R.drawable.s57);
           ImageView i58 = new ImageView(this);
           i58.setImageResource(R.drawable.s58);
           ImageView i59 = new ImageView(this);
           i59.setImageResource(R.drawable.s59);
           ImageView i60 = new ImageView(this);
           i60.setImageResource(R.drawable.s60);
           ImageView i61 = new ImageView(this);
           i61.setImageResource(R.drawable.s61);
           ImageView i62 = new ImageView(this);
           i62.setImageResource(R.drawable.s62);
           ImageView i63 = new ImageView(this);
           i63.setImageResource(R.drawable.s63);
           ImageView i64 = new ImageView(this);
           i64.setImageResource(R.drawable.s64);
           ImageView i65 = new ImageView(this);
           i65.setImageResource(R.drawable.s65);
           ImageView i66 = new ImageView(this);
           i66.setImageResource(R.drawable.s66);
           ImageView i67 = new ImageView(this);
           i67.setImageResource(R.drawable.s67);
           ImageView i68 = new ImageView(this);
           i68.setImageResource(R.drawable.s68);
           ImageView i69 = new ImageView(this);
           i69.setImageResource(R.drawable.s69);
           ImageView i70 = new ImageView(this);
           i70.setImageResource(R.drawable.s70);
           ImageView i71 = new ImageView(this);
           i71.setImageResource(R.drawable.s71);
           ImageView i72 = new ImageView(this);
           i72.setImageResource(R.drawable.s72);
           ImageView i73 = new ImageView(this);
           i73.setImageResource(R.drawable.s73);
           ImageView i74 = new ImageView(this);
           i74.setImageResource(R.drawable.s74);
           ImageView i75 = new ImageView(this);
           i75.setImageResource(R.drawable.s75);
           ImageView i76 = new ImageView(this);
           i76.setImageResource(R.drawable.s76);
           ImageView i77 = new ImageView(this);
           i77.setImageResource(R.drawable.s77);
           ImageView i78 = new ImageView(this);
           i78.setImageResource(R.drawable.s78);
           ImageView i79 = new ImageView(this);
           i79.setImageResource(R.drawable.s79);
           ImageView i80 = new ImageView(this);
           i80.setImageResource(R.drawable.s80);
           ImageView i81 = new ImageView(this);
           i81.setImageResource(R.drawable.s81);
           ImageView i82 = new ImageView(this);
           i82.setImageResource(R.drawable.s82);
           ImageView i83 = new ImageView(this);
           i83.setImageResource(R.drawable.s83);
           ImageView i84 = new ImageView(this);
           i84.setImageResource(R.drawable.s84);
           ImageView i85 = new ImageView(this);
           i85.setImageResource(R.drawable.s85);
           ImageView i86 = new ImageView(this);
           i86.setImageResource(R.drawable.s86);


           viewFlipper.addView(i1);

           viewFlipper.addView(i2);

           viewFlipper.addView(i3);

           viewFlipper.addView(i4);

           viewFlipper.addView(i5);

           viewFlipper.addView(i6);

           viewFlipper.addView(i7);

           viewFlipper.addView(i8);

           viewFlipper.addView(i9);

           viewFlipper.addView(i10);

           viewFlipper.addView(i11);

           viewFlipper.addView(i12);

           viewFlipper.addView(i13);

           viewFlipper.addView(i14);

           viewFlipper.addView(i15);

           viewFlipper.addView(i16);

           viewFlipper.addView(i17);

           viewFlipper.addView(i18);

           viewFlipper.addView(i19);

           viewFlipper.addView(i20);

           viewFlipper.addView(i21);

           viewFlipper.addView(i22);

           viewFlipper.addView(i23);

           viewFlipper.addView(i24);

           viewFlipper.addView(i25);

           viewFlipper.addView(i26);

           viewFlipper.addView(i27);

           viewFlipper.addView(i28);

           viewFlipper.addView(i29);

           viewFlipper.addView(i30);

           viewFlipper.addView(i31);

           viewFlipper.addView(i32);

           viewFlipper.addView(i33);

           viewFlipper.addView(i34);

           viewFlipper.addView(i35);

           viewFlipper.addView(i36);

           viewFlipper.addView(i37);

           viewFlipper.addView(i38);

           viewFlipper.addView(i39);

           viewFlipper.addView(i40);

           viewFlipper.addView(i41);

           viewFlipper.addView(i42);

           viewFlipper.addView(i43);

           viewFlipper.addView(i44);

           viewFlipper.addView(i45);

           viewFlipper.addView(i46);

           viewFlipper.addView(i47);

           viewFlipper.addView(i48);

           viewFlipper.addView(i49);

           viewFlipper.addView(i50);

           viewFlipper.addView(i51);

           viewFlipper.addView(i52);

           viewFlipper.addView(i53);

           viewFlipper.addView(i54);

           viewFlipper.addView(i55);

           viewFlipper.addView(i56);

           viewFlipper.addView(i57);

           viewFlipper.addView(i58);

           viewFlipper.addView(i59);

           viewFlipper.addView(i60);

           viewFlipper.addView(i61);

           viewFlipper.addView(i62);

           viewFlipper.addView(i63);

           viewFlipper.addView(i64);

           viewFlipper.addView(i65);

           viewFlipper.addView(i66);

           viewFlipper.addView(i67);

           viewFlipper.addView(i68);

           viewFlipper.addView(i69);

           viewFlipper.addView(i70);

           viewFlipper.addView(i71);

           viewFlipper.addView(i72);

           viewFlipper.addView(i73);

           viewFlipper.addView(i74);

           viewFlipper.addView(i75);

           viewFlipper.addView(i76);

           viewFlipper.addView(i77);

           viewFlipper.addView(i78);

           viewFlipper.addView(i79);

           viewFlipper.addView(i80);

           viewFlipper.addView(i81);

           viewFlipper.addView(i82);

           viewFlipper.addView(i83);

           viewFlipper.addView(i84);

           viewFlipper.addView(i85);

           viewFlipper.addView(i86);







           //set up animations
           slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
           slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
           slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
           slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
           //put up a brownie as a starter
           setContentView(viewFlipper);
           gestureDetector = new GestureDetector(new MyGestureDetector());
       }

       private void recycle() {
         // TODO Auto-generated method stub
      }

      public class MyGestureDetector extends SimpleOnGestureListener {
           @Override
           public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
               try {
                   if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                       return false;
                   // right to left swipe
                   if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                       viewFlipper.setInAnimation(slideLeftIn);
                       viewFlipper.setOutAnimation(slideLeftOut);
                       viewFlipper.showNext();
                       setContentView(viewFlipper);
                   } // left to right swipe
                   else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                       viewFlipper.setInAnimation(slideRightIn);
                       viewFlipper.setOutAnimation(slideRightOut);
                       viewFlipper.showPrevious();
                       setContentView(viewFlipper);

                   }
               } catch (Exception e) {
                   // nothing
               }
               return false;
           }


            // TODO Auto-generated method stub

         }



       @Override
       public boolean onTouchEvent(MotionEvent event) {
           if (gestureDetector.onTouchEvent(event)){

               return true;
           }
           else{
               return false;
           }
       }
         // TODO Auto-generated method stub

      }

1 个答案:

答案 0 :(得分:0)

viewFlipper可能不是您的最佳选择。问题是,所有图像都保留在内存中,没有任何东西被回收。还取决于图像的大小。

对于水平滚动..您最好的选择是使用图库。它是一个水平滚动列表。您可以使用适配器来设置内容,Android将负责为您回收视图。这是一个很好的例子:http://developer.android.com/resources/tutorials/views/hello-gallery.html

请记住,在实现适配器时,始终首先检查convertView是否为null ..并且只在必要时才膨胀新视图(即如果convertView == null)。否则表现会受到影响。