重复显示多张图片于imageView中,采用从左到右翻转效果。

7

假设我有多个图片在drawable文件夹中(例如8张图片)。 我想使用从左到右翻转的效果将所有这些图像一个接一个地显示在imageView中,并反复显示(ex-img[0],img[1],......img[8],img[0],img[1],..........)。 我该怎么做呢?

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);         
}
5个回答

7

使用自定义函数通过处理程序来旋转图像以更改图像,这里我更改了图像的反向方向:

    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);

    }

谢谢你的帮助,兄弟...但是我想要在更改图像时有翻转动画效果...你能给我你的动画XML代码吗?请。 - Sritam Jagadev
请查看此链接:http://androidtutorials60.blogspot.in/2013/09/simple-rotate-animation-in-android.html - Haresh Chhelana

7

使用ViewFlipper,并将ImageView添加到ViewFlipper中。

布局文件:

<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>

然后在你的活动中添加以下代码。
ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.flipperid);

现在,您可以通过调用 viewFlipper.startFlipping(); 来自动翻转图像。

希望这能帮助到您。


非常感谢您提供这么简单的答案。我们能否为其添加某种动画效果? - Shirish Herwade
看这个 https://stackoverflow.com/a/25781173/4608334 祝编码愉快! - Vinoth Vino
1
@VinothVino,简单易懂的答案。感谢你的出色工作。 - Gomathimeena Subburayan

0

只需使用

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;
}
}

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();


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接