要求触摸滑动屏幕实现翻页,从左往右,从右往左的动画效果。
界面main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper">
<!-- 第一页 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一屏"/>
</LinearLayout>
<!-- 第2页 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二屏"/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
在res目录下新建文件夹anim,里面四个XML文件如下:
in_lefttoright.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 从左向右移进动画效果 -->
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="1000"/>
</set>
in_righttoleft.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 从右向左移进动画效果 -->
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="1000"/>
</set>
out_lefttoright.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 从左向右移出动画效果 -->
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="1000"/>
</set>
out_righttoleft.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 从右向左移出动画效果 -->
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="1000"/>
</set>
MainActivity.java
public class MainActivity extends Activity {
private ViewFlipper flipper;
private float startX;
private Animation in_lefttorightAnimation;
private Animation out_lefttorightAnimation;
private Animation in_righttoleftAnimation;
private Animation out_righttoleftAnimation;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
in_lefttorightAnimation = AnimationUtils.loadAnimation(this, R.anim.in_lefttoright);//从左向右进入屏幕动画效果
out_lefttorightAnimation = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);//从左向右移出屏幕动画效果
in_righttoleftAnimation = AnimationUtils.loadAnimation(this, R.anim.in_righttoleft);//从右向左进入屏幕动画效果
out_righttoleftAnimation = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);//从右向左移出屏幕动画效果
}
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://手指按下屏幕
startX = event.getX();//取得按下时刻的点
break;
case MotionEvent.ACTION_UP://手指抬起
float endX = event.getX();//取得抬起时刻的点
if(endX > startX){//向右
flipper.setInAnimation(in_lefttorightAnimation);//设置从左向右进入屏幕动画效果
flipper.setOutAnimation(out_lefttorightAnimation);//设置从左向右移出屏幕动画效果
flipper.showNext();//显示下一页
}else if(endX < startX){//向左
flipper.setInAnimation(in_righttoleftAnimation);//设置从右向左进入屏幕动画效果
flipper.setOutAnimation(out_righttoleftAnimation);//设置从右向左移出屏幕动画效果
flipper.showPrevious();//显示上一页
}
break;
}
return super.onTouchEvent(event);
}
}