//首先是anim的left_in.xml的动画设置
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <translate android:duration="2000" android:fromXDelta="-100%p" android:toXDelta="0" /> <alpha android:fromAlpha="0.5" android:toAlpha="1" android:duration="2000" /> </set>
//anim的left_out.xml的动画设置
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <translate android:duration="2000" android:fromXDelta="0" android:toXDelta="100%p" /> <alpha android:fromAlpha="1" android:toAlpha="0.5" android:duration="2000" /> </set>
然后设置activity_main.xml文件设置
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <ViewFlipper android:id="@+id/viewflipper" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
接着是drawable的图片设置
最后是MainActivity的设置
package com.example.viewflipper; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import android.widget.ViewFlipper; public class MainActivity extends AppCompatActivity { private ViewFlipper flipper; private int[] resID = {R.mipmap.ic_launcher,R.drawable.pic3}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); flipper = (ViewFlipper) findViewById(R.id.viewflipper); //动态导入的方式为ViewFlipper加入到子View for(int i=0; i<resID.length;i++){ flipper.addView(getImageView(resID[i])); } //为flipper去添加动画效果 flipper.setInAnimation(this,R.anim.left_in); flipper.setOutAnimation(this,R.anim.left_out); //为flipper视图切换的时间间隔 flipper.setFlipInterval(3000); //开始播放 flipper.startFlipping(); } private ImageView getImageView(int resID){ ImageView image = new ImageView(this); image.setBackgroundResource(resID); return image; } }