activity切换直接通过overridePendingTransition()方法,直接输入动画资源就可以实现。
view之间的切换需要设置setInAnimation和setOutAnimation,需要编码实现。
package com.zskx;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;
public class AnimationActivity extends Activity {
ViewFlipper viewFlipper;
Animation anim_in_left2right ; //从左到右进入动画
Animation anim_out_left2right ; //从左到右出去动画
Animation anim_in_right2left ; //从右到左进入动画
Animation anim_out_right2left ; //从右到左出去动画
int windowWidth;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
windowWidth = getWindowManager().getDefaultDisplay().getWidth(); //屏幕宽度
setAnimation(); //加载动画资源
}
private void setAnimation() {
anim_in_left2right = AnimationUtils.loadAnimation(this, R.anim.in_left2right);
anim_out_left2right = AnimationUtils.loadAnimation(this, R.anim.out_left2right);
anim_in_right2left = AnimationUtils.loadAnimation(this, R.anim.in_right2left);
anim_out_right2left = AnimationUtils.loadAnimation(this, R.anim.out_right2left);
}
/**
* 启动第二个activity
* @param v
*/
public void startSecond(View v){
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
AnimationActivity.this.overridePendingTransition(R.anim.activity_in,
R.anim.activity_out);//切换activity的动画,直接输入动画R资源
}
float lastX;
float curX;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = event.getX();
break;
case MotionEvent.ACTION_UP:
curX = event.getX();
if(curX - lastX > windowWidth * 0.75){ //拉动超过屏幕3/4, 从左到右切换page。
viewFlipper.setInAnimation(anim_in_left2right); //设置动画必须在showNext()之前
viewFlipper.setOutAnimation(anim_out_left2right);
viewFlipper.showNext(); // 切换到下一页
}else if(lastX - curX > windowWidth * 0.75){ //拉动超过屏幕3/4,从右到左切换page
viewFlipper.setInAnimation(anim_in_right2left); //设置动画必须在showPrevious()之前
viewFlipper.setOutAnimation(anim_out_right2left);
viewFlipper.showPrevious(); // 切换到上一页
}
break;
case MotionEvent.ACTION_MOVE:
break;
}
return super.onTouchEvent(event);
}
}
淡入淡出动画的xml:
in
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="3000"
/>
</set>
out
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0"
android:duration="3000"
/>
</set>
左右互换动画的xml:
in:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="3000"
/>
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="100%p" <!--窗口宽度-->
android:toXDelta="0"
android:duration="3000"
/>
</set>
out
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="3000"
/>
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="3000"
/>
</set>