效果图
往左滑动屏幕,显示第一页,往右滑显示第二页
xml布局如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 滑动屏幕专用的ViewFlipper -->
<ViewFlipper
android:id="@+id/viewfilp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- 第一页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一页" />
</LinearLayout>
<!-- 第二页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#339966" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二页" />
</LinearLayout>
</ViewFlipper>
</LinearLayout>
package ll.e.animation;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private float startx;
private ViewFlipper vf;
private Animation inAnimation;
private Animation outAnimation;
private Animation yioutAnimation;
private Animation yienterAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vf = (ViewFlipper) findViewById(R.id.viewfilp);
inAnimation = AnimationUtils.loadAnimation(this, R.anim.enteralpha);
outAnimation = AnimationUtils.loadAnimation(this, R.anim.chutranslar);
yienterAnimation = AnimationUtils.loadAnimation(this,
R.anim.yientrtranslar);
yioutAnimation = AnimationUtils.loadAnimation(this,
R.anim.yiouttranslar);
}
// public void click(View v) {
// Intent intent = new Intent(this, OtherActivity.class);
// startActivity(intent);
// this.overridePendingTransition(R.anim.enteralpha, R.anim.exitalpha);
// }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* 用来判断是想左滑,还是想右滑
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
startx = event.getX();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
float endx = event.getX();
if (endx > startx) {
vf.setInAnimation(inAnimation);//设置进入布局的风格
vf.setOutAnimation(outAnimation);//设置出去布局的风格
vf.showNext();//显示下一个控件
} else if (endx < startx) {
vf.setInAnimation(yienterAnimation);
vf.setOutAnimation(yioutAnimation);
vf.showPrevious();//显示上一个布局
}
return true;
}
return super.onTouchEvent(event);
}
}
源码链接如下
http://download.youkuaiyun.com/detail/u014469691/9463703