Android uses ViewFlipper to automatically rotate images up and down

First look at the effect:

1. The XML code:

                      <ViewFlipper
                            android:id="@+id/viewFlipper"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:flipInterval="3000"
                            android:inAnimation="@anim/anim_marquee_in"
                            android:outAnimation="@anim/anim_marquee_out" />
Copy the code

2. Concrete implementation:

(1) Key codes:

// picList <String> picList; / /... / /... Initializing picList...... is omitted / /... viewFlipper.removeAllViews(); for (int i = 0; i < picList.size(); i++) { final String pic = picList.get(i); ImageView iv = new ImageView(context); iv.setImageResource(R.mipmap.bg); Iv. setOnClickListener(listener); viewFlipper.addView(iv); viewFlipper.setAutoStart(true); } viewFlipper.setFlipInterval(3 * 1000); viewFlipper.startFlipping();Copy the code

(2) Rotation animation: Android :inAnimation + Android :outAnimation

anim_marquee_in

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromYDelta="100%p" android:toYDelta="0"/> </set>Copy the code

anim_marquee_out

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromYDelta="0" android:toYDelta="-100%p"/> </set>Copy the code