7

Suppose I have multiple images in drawable folder(ex-8 images). I want to show all these images in a imageView one after another with left to right flip effect repeatedly(ex-img[0],img[1],……img[8],img[0],img[1],…………). How can I do this?

private void AnimateandSlideShow() {
    image1 = (ImageView)findViewById(R.id.imageView1);
    image1.setImageResource(img[currentimageindex1%img.length]);
    currentimageindex1++;
    Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
    image1.startAnimation(rotateimage);         
}
Farid Nouri Neshat
  • 28,138
  • 6
  • 71
  • 113
Sritam Jagadev
  • 931
  • 3
  • 16
  • 39

6 Answers6

7

Use custom function rotate image using handler for interval to change image,here i change image vice verse direction :

    private ImageView image1;
    private int[] imageArray;
    private int currentIndex;
    private int startIndex;
    private int endIndex;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        image1 = (ImageView)findViewById(R.id.imageView1);
        imageArray = new int[8];
        imageArray[0] = R.drawable.one;
        imageArray[1] = R.drawable.two;
        imageArray[2] = R.drawable.three;
        imageArray[3] = R.drawable.four;
        imageArray[4] = R.drawable.five;
        imageArray[5] = R.drawable.six;
        imageArray[6] = R.drawable.seven;
        imageArray[7] = R.drawable.eight;

        startIndex = 0;
        endIndex = 7;
        nextImage();


    }

    public void nextImage(){
        image1.setImageResource(imageArray[currentIndex]);
        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
        image1.startAnimation(rotateimage);
        currentIndex++;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(currentIndex>endIndex){
                    currentIndex--;
                    previousImage();
                }else{
                    nextImage();
                }

            }
        },1000); // here 1000(1 second) interval to change from current  to next image  

    }
    public void previousImage(){
        image1.setImageResource(imageArray[currentIndex]);
        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
        image1.startAnimation(rotateimage);
        currentIndex--;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(currentIndex<startIndex){
                    currentIndex++;
                    nextImage();
                }else{
                    previousImage(); // here 1000(1 second) interval to change from current  to previous image 
                }
            }
        },1000);

    }
Haresh Chhelana
  • 24,394
  • 5
  • 55
  • 67
7

Use ViewFlipper and add your ImageView inside the ViewFlipper.

Layout file:

<ViewFlipper
    android:layout_width="match_parent"
    android:layout_height="330dp"
    android:layout_below="@id/textViewid"
    android:layout_marginTop="20dp"
    android:id="@+id/flipperid"
    android:layout_centerInParent="true"
    android:flipInterval="4000"
    android:elevation="5dp">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/pic1"
        android:scaleType="fitCenter"
        android:layout_gravity="center_horizontal"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/pic2"
        android:scaleType="fitCenter"
        android:layout_gravity="center_horizontal"/>

</ViewFlipper>

Then add below code in your activity.

ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.flipperid);

and now you can start flipping the images automatically by calling viewFlipper.startFlipping();

Hope this will help you..

Vinoth Vino
  • 8,283
  • 3
  • 63
  • 64
0

Use Gallery View in your app . Follow this tutorial

http://www.androidinterview.com/android-gallery-view-example-displaying-a-list-of-images/

hope this will help you friend :)

Prince Thomas
  • 990
  • 7
  • 10
  • Bro,I am dynamically changing different images in single image View .I want to display these images one after another with a flip animation effect.means suppose a image is displaying in the image view now.but when a new image is coming it should be displayed with a flip effect. – Sritam Jagadev Nov 27 '14 at 10:29
0
<ViewFlipper
            android:id="@+id/imageCarouselContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/progressSlideshow"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="0dp"
            android:animateFirstView="true"
            android:background="@color/black"
            android:flipInterval="2000"
            android:inAnimation="@anim/fade_in"
            android:orientation="vertical"
            android:outAnimation="@anim/fade_out" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
             />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
             />

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="5dp"
            />
        </ViewFlipper>

yourViewFlipper.startFlipping();

Murtaza Khursheed Hussain
  • 14,976
  • 7
  • 55
  • 82
0

use ViewFlipper or ViewPager

i hope this will help you...

Sarath Kumar
  • 1,754
  • 2
  • 18
  • 38
0

Just use

public class CustomGallery extends Gallery {

private Handler handler;

public CustomGallery(Context context) {
    super(context);
    handler = new Handler();
    postDelayedScrollNext();
}

public CustomGallery(Context context, AttributeSet attrs) {
    super(context, attrs);
    handler = new Handler();
    postDelayedScrollNext();
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    handler = new Handler();
    postDelayedScrollNext();
}

protected void postDelayedScrollNext() {
    handler.postDelayed(new Runnable() {
        public void run() {
            postDelayedScrollNext();
            Log.d("CustomGallery", "dpad RIGHT");
            onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
        }
    }, 1000);
}

protected boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
    return e2.getX() > e1.getX();
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    int kEvent;
    if (isScrollingLeft(e1, e2)) {
        Log.d("CustomGallery", "fling LEFT");
        kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
    } else {
        Log.d("CustomGallery", "fling LEFT");
        kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
    }
    onKeyDown(kEvent, null);
    return true;
}
}
Sritam Jagadev
  • 931
  • 3
  • 16
  • 39