private viewflipper flipper;
private float startx;
private animation in_lefttoright;
private animation outlefttoright;
private animation in_righttoleft;
private animation out_righttoleft;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
flipper = (viewflipper) this.findviewbyid(r.id.viewflipper);
in_lefttoright = animationutils.loadanimation(this, r.anim.in_lefttoright);
outlefttoright = animationutils.loadanimation(this, r.anim.out_lefttoright);
in_righttoleft = animationutils.loadanimation(this, r.anim.in_righttoleft);
out_righttoleft = animationutils.loadanimation(this, r.anim.out_righttoleft);
}
@override
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_lefttoright);
flipper.setoutanimation(outlefttoright);
flipper.shownext();
//顯示下一頁
}else if(endx < startx){
//向左
flipper.setinanimation(in_righttoleft);
flipper.setoutanimation(out_righttoleft);
flipper.showprevious();
}
break;
}
return super.ontouchevent(event);
}
main.xml主布局檔案:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout
xmlns:android=""
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<viewflipper
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>
<!-- 第二頁 -->
android:background="#ffffff"
android:text="第二屏"
</viewflipper>
</linearlayout>
in_lefttoright.xml檔案:
<set xmlns:android=""
android:shareinterpolator="false">
<translate
android:fromxdelta="-100%p"
android:toxdelta="0"
android:duration="3000"
/>
</set>
in_righttoleft.xml檔案:
android:fromxdelta="100%p"
android:toxdelta="0"
out_lefttoright.xml檔案:
android:shareinterpolator="false">
<translate
android:fromxdelta="0"
android:toxdelta="100%p"
android:duration="3000"
out_righttoleft.xml檔案:
android:fromxdelta="0"
android:toxdelta="-100%p"
android:duration="3000"