Search code examples
androidimageswitcher

i want the image swither in my app


I want to insert the image swicther in my app so what should I do now? this is aking for imageadapter so how can i implement it? please help me.. thank u....

this is my code...

Integer pics[] = { R.drawable.amrapali1, R.drawable.defic1, R.drawable.hnsafal1, R.drawable.leela1, R.drawable.mitashi1, R.drawable.magnanimous1, R.drawable.moon1, R.drawable.netpeckers1, R.drawable.nggroup1, R.drawable.platinum1, R.drawable.shivalik1, R.drawable.trikon1 };

ImageSwitcher iSwitcher;


   iSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher);
    iSwitcher.setFactory(this);
    iSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in));
    iSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out));

    Gallery gallery = (Gallery) findViewById(R.id.Gallery);
    gallery.setAdapter(new ImageAdapter(this));
    gallery.setOnItemClickListener(new OnItemClickListener() {
    });

Solution

  • Try out with the below ImageAdapter class

    public class ImageAdapter extends BaseAdapter {
    
            private Context ctx;
    
            public ImageAdapter(Context c) {
                ctx = c; 
            }
    
            public int getCount() {
    
                return pics.length;
            }
    
            public Object getItem(int arg0) {
    
                return arg0;
            }
    
            public long getItemId(int arg0) {
    
                return arg0;
            }
    
            public View getView(int arg0, View arg1, ViewGroup arg2) {
    
                ImageView iView = new ImageView(ctx);
                iView.setImageResource(pics[arg0]);
                iView.setScaleType(ImageView.ScaleType.FIT_XY);
                iView.setLayoutParams(new Gallery.LayoutParams(200, 150));
                return iView;
            }
        }
    

    For more detailed understanding Go HERE