How to write dynamic slideshow in Android

I am trying to write a dynamic slideshow with ViewFlipper, but I cannot put all Bitmaps (ImageViews) in the ViewFlipper at the same time because it will throw an OutOfMemoryException.

At the moment I have this code and it only shows the last snapshot and then nothing else.

mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(3000);
mViewFlipper.startFlipping();
setPictures(im);
mViewFlipper.stopFlipping();



    private void setPictures(IMI im) {
            Bitmap bmp = setView(im);
            setFlipperImage(bmp);
            bmp = null;

            mViewFlipper.setInAnimation(null);
            mViewFlipper.setOutAnimation(null);

            while (mViewFlipper.getChildCount() > 1) {
                mViewFlipper.removeViewAt(0);
            }

        }

private void setFlipperImage(Bitmap bm) {
        ImageView image = new ImageView(getApplicationContext());
        image.setImageBitmap(bm);
        mViewFlipper.addView(image);
    }

    private Bitmap setView(IMI im) {
        Bitmap bmp = null;
        try {
            bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse("file://" + im.getUri()));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return bmp;

    }

      

Thanks for your help in advance.

+3


source to share


2 answers


Don't use ViewFlipper for this use case (for the reason you gave). Use the ViewPager in conjunction with the FragmentPagerAdapter where each fragment shows one image. Thus, the max. 3 bitmaps are loaded at a time.



+2


source


try it

SlideViewPager.java

public class SlideViewPager extends Activity {
ViewPager pager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpagerlayout);
    pager=(ViewPager) findViewById(R.id.view_pager);
    MyAdapter adapter=new MyAdapter(this);
    pager.setAdapter(adapter);
}   
private boolean pagerMoved = false;
private static final long ANIM_VIEWPAGER_DELAY = 3000;

private Handler h = new Handler();
    private Runnable animateViewPager = new Runnable() {
        @Override
        public void run() {

            if (!pagerMoved) {

                if(pager.getCurrentItem()==pager.getChildCount()){
                    pager.setCurrentItem(0,true);
                }else {
                    pager.setCurrentItem(pager.getCurrentItem()+1, true);
                }

                h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);                  
            }               
        }
    };

@Override
public void onPause() {
    super.onPause();
    if (h != null) {
        h.removeCallbacks(animateViewPager);
    }
}

@Override
public void onResume() {
    super.onResume();
    h.postDelayed(animateViewPager, ANIM_VIEWPAGER_DELAY);
}
}   
class MyAdapter extends PagerAdapter{
    Context mContext;
    public TnCAdapter(Context context){
        mContext=context;
    }
    @Override
    public int getCount() {
        return noOfSlides;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==((View)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View rootview=inflater.inflate(R.layout.viewpager_view, container,false);

        ImageView iv=(ImageView) rootview.findViewById(R.id.imageView1);
        //Here set your image for every slide           
        switch (position) {
        case 0:
            iv.setBackgroundResource(R.drawable.scr0);
            break;
        case 1:
            iv.setBackgroundResource(R.drawable.scr1);
            break;
        case 2:
            iv.setBackgroundResource(R.drawable.scr2);
            break;

        }

        ((ViewPager)container).addView(rootview,0);

        return rootview;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        ((ViewPager)container).removeView((View) object);

    }
}   
}    

      

and viewpagerlayout.xml

<?xml version="1.0" encoding="utf-8"?>    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_above="@+id/btn_tnc"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/indicator"
    android:layout_marginTop="5dp"
    android:isScrollContainer="true" >
</android.support.v4.view.ViewPager>

      



and viewpager_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"   
android:orientation="vertical" >
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:background="@drawable/ic_launcher" />
</LinearLayout>    

      

Try setting the bitmap image to instantiateItem

in the MyAdapter class.

0


source







All Articles