in_leftright.xml //从左往右进入
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="100%p"/>
</set>
out_leftright.xml //从左往右出去
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="-100%p"
android:toXDelta="0"/>
</set>
activity_main.xml //布局文件
<LinearLayout 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:orientation="vertical">
<ViewFlipper android:id="@+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:background="#ffff0000"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center">
</LinearLayout>
<LinearLayout
android:background="#ff00ff00"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center">
</LinearLayout>
<LinearLayout
android:background="#ff0000ff"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center">
</LinearLayout>
</ViewFlipper>
</LinearLayout>
MainActivity.java文件
package com.example.test3;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private ViewFlipper mViewFlipper;
Handler mHandler = new Handler();
Runnable mRunnable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewFlipper = (ViewFlipper)this.findViewById(R.id.viewFlipper);
mViewFlipper.setInAnimation(this, R.anim.in_leftright);
mViewFlipper.setOutAnimation(this, R.anim.out_leftright);
mRunnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mViewFlipper.showNext();
mHandler.postDelayed(this, 2000);
}
};
mHandler.postDelayed(mRunnable, 2000);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
}