Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an ImageSwitcher without a Gallery

Tags:

android

In looking at the ApiDemos example in the Android (1.5) SDK, there is a fine example of using an ImageSwitcher, with a Gallery object to provide the "change image" actions.

The application I'm looking to write, starting into Android development, has three images that I want to be able to pane/scroll through, so ImageSwitcher looks like a fine solution. However, I don't [necesarily] want to have thumbnails in a gallery. I want either a swipe action, and/or a button, to cause a scroll to the previous/next image in the set.

The example ImageSwitcher in ApiDemos uses a Gallery, and without that Gallery, doesn't do anything.

If someone has a suggestion of a way to bind some sort of button controller, or a U/I swipe object, I would appreciate the pointer.

Sorry to ask such a newbie level question.

Thanks.

like image 276
cross Avatar asked Nov 14 '22 12:11

cross


1 Answers

You can use it like this:

  public class GalleryActivity extends Activity implements ViewFactory{    
    /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);        

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

        iSwitcher.setImageDrawable(fetchImage(mImageURLS[0]));

        iSwitcher.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // CLICK HANDLER
                // Change image like: 
                // iSwitcher.setImageDrawable(fetchImage(mImageURLS[1]));
            }
        });
   }

    @Override
    public View makeView() {
        ImageView iView = new ImageView(this);
        iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        iView.setLayoutParams(new 
                ImageSwitcher.LayoutParams(
                        LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        iView.setBackgroundColor(0xFF000000);
        return iView;
    }
}

fetchImage(mImageURLS[0]) returns Drawable object

like image 199
Komi Avatar answered Dec 30 '22 15:12

Komi