user3139159
user3139159

Reputation: 23

How to create an image slider?

How to create an image slider in eclipse?

i want, when a finger moves from left to right or from right to left on imageView, I want to change the photo.

Upvotes: 1

Views: 2373

Answers (2)

Kailash Dabhi
Kailash Dabhi

Reputation: 3513

No need to do any custom thing.As Android provides the facility like this in ViewFlipper class.It is actually made for it.

View Flipper:-

Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

Its very nice and also built in support for animation so just use this for this purpose. To implement the ViewFlipper in your application I am sharing some good tutorials..

  • http://javatechig.com/android/android-viewflipper-example
  • http://www.yogeshblogspot.com/android-viewflipper-example/
  • http://learnandroideasily.blogspot.in/2013/06/android-viewflipper-example.html

    You can also get more info at here. http://developer.android.com/reference/android/widget/ViewFlipper.html.

    Enjoy..!

    Upvotes: 1

  • nikvs
    nikvs

    Reputation: 1090

    View Pager refer this is one option, otherwise you can add a simple gesture listener to your image view.

    //Adding gesture listener to image view.

    final GestureDetector gdt = new GestureDetector(new GestureListener());
            final ImageView imageView  = (ImageView) findViewById(R.id.image_view);
            imageView.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(final View view, final MotionEvent event) {
                    gdt.onTouchEvent(event);
                    return true;
                }
            });
    

    //The gesture listener class

      private class GestureListener extends SimpleOnGestureListener {
                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    //load previous image here
                        return false; // Right to left
                    }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    //load next image here
                        return false; // Left to right
                    }
    
                    if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                        return false; // Bottom to top
                    }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                        return false; // Top to bottom
                    }
                    return false;
                }
            }
    

    Upvotes: 0

    Related Questions