android ViewGroup实现左右滑屏效果方法3

设置了三个页面,布局文件如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"android:background="@drawable/dbj">
  5. <ViewFlipperandroid:id="@+id/ViewFlipper"
  6. android:layout_width="fill_parent"android:layout_height="fill_parent">
  7. <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  8. android:orientation="vertical"android:layout_width="fill_parent"
  9. android:layout_height="fill_parent">
  10. <TextViewandroid:text="第1页"
  11. android:textSize="35dp"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_x="115dp"
  15. android:layout_y="20dp"/>
  16. </AbsoluteLayout>
  17. <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  18. android:orientation="vertical"android:layout_width="fill_parent"
  19. android:layout_height="fill_parent">
  20. <TextViewandroid:text="第2页"
  21. android:textSize="35dp"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_x="120dp"
  25. android:layout_y="20dp"/>
  26. </AbsoluteLayout>
  27. <AbsoluteLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  28. android:orientation="vertical"android:layout_width="fill_parent"
  29. android:layout_height="fill_parent">
  30. <TextViewandroid:text="第3页"
  31. android:textSize="35dp"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content"
  34. android:layout_x="120dp"
  35. android:layout_y="20dp"/>
  36. </AbsoluteLayout>
  37. </ViewFlipper>
  38. <ImageButtonandroid:layout_width="35dp"
  39. android:background="@drawable/pre_button"android:layout_height="40dp"
  40. android:id="@+id/preButton1"android:layout_x="101dp"
  41. android:layout_y="404dp">
  42. </ImageButton>
  43. <ImageButtonandroid:layout_width="40dp"
  44. android:background="@drawable/next_button"android:layout_height="40dp"
  45. android:id="@+id/nextButton1"android:layout_x="182dp"
  46. android:layout_y="405dp">
  47. </ImageButton>
  48. </AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent" android:background="@drawable/dbj"><ViewFlipper android:id="@+id/ViewFlipper"android:layout_width="fill_parent" android:layout_height="fill_parent"><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:text="第 1 页"android:textSize="35dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="115dp"android:layout_y="20dp"/></AbsoluteLayout><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:text="第 2 页"android:textSize="35dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="120dp"android:layout_y="20dp"/></AbsoluteLayout><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:text="第 3 页"android:textSize="35dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_x="120dp"android:layout_y="20dp"/></AbsoluteLayout></ViewFlipper><ImageButton android:layout_width="35dp"android:background="@drawable/pre_button" android:layout_height="40dp"android:id="@+id/preButton1" android:layout_x="101dp"android:layout_y="404dp"></ImageButton><ImageButton android:layout_width="40dp"android:background="@drawable/next_button" android:layout_height="40dp"android:id="@+id/nextButton1" android:layout_x="182dp"android:layout_y="405dp"></ImageButton></AbsoluteLayout>
主程序实现了OnGestureListener 接口,具体如下:

  1. importandroid.app.Activity;
  2. importandroid.os.Bundle;
  3. importandroid.view.GestureDetector;
  4. importandroid.view.GestureDetector.OnGestureListener;
  5. importandroid.view.MotionEvent;
  6. importandroid.view.View;
  7. importandroid.view.View.OnClickListener;
  8. importandroid.view.View.OnTouchListener;
  9. importandroid.view.animation.AnimationUtils;
  10. importandroid.widget.ImageButton;
  11. importandroid.widget.ViewFlipper;
  12. /**
  13. *ViewFlipperTest.java
  14. *@authorCloay
  15. *2011-6-24
  16. */
  17. publicclassViewFlipperTestextendsActivityimplementsOnGestureListener{
  18. privateViewFlipperflipper;
  19. privateGestureDetectordetector;
  20. privateImageButtonpre1Button;
  21. privateImageButtonnext1Button;
  22. @Override
  23. publicvoidonCreate(BundlesavedInstanceState){
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.zd);
  26. pre1Button=(ImageButton)findViewById(R.id.preButton1);
  27. pre1Button.setOnTouchListener(newOnTouchListener(){
  28. @Override
  29. publicbooleanonTouch(Viewv,MotionEventevent){
  30. //TODOAuto-generatedmethodstub
  31. if(event.getAction()==MotionEvent.ACTION_DOWN){//按钮按下背景图片
  32. pre1Button.setBackgroundResource(R.drawable.pre_button1);
  33. }
  34. //按钮up后设置背景图片,并滑动到前一页面
  35. elseif(event.getAction()==MotionEvent.ACTION_UP){
  36. pre1Button.setBackgroundResource(R.drawable.pre_button);
  37. flipper.setInAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_right_in));
  38. flipper.setOutAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_right_out));
  39. flipper.showPrevious();
  40. }
  41. returnfalse;
  42. }
  43. });
  44. next1Button=(ImageButton)findViewById(R.id.nextButton1);
  45. next1Button.setOnTouchListener(newOnTouchListener(){
  46. @Override
  47. publicbooleanonTouch(Viewv,MotionEventevent){
  48. //TODOAuto-generatedmethodstub
  49. if(event.getAction()==MotionEvent.ACTION_DOWN){
  50. next1Button.setBackgroundResource(R.drawable.next_button1);
  51. }
  52. //按钮up后设置背景图片,并滑动到后一页面
  53. elseif(event.getAction()==MotionEvent.ACTION_UP){
  54. next1Button.setBackgroundResource(R.drawable.next_button);
  55. flipper.setInAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_left_in));
  56. flipper.setOutAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_left_out));
  57. flipper.showNext();
  58. }
  59. returnfalse;
  60. }
  61. });
  62. detector=newGestureDetector(this);
  63. flipper=(ViewFlipper)this.findViewById(R.id.ViewFlipper);
  64. }
  65. publicbooleanonDoubleTap(MotionEvente){
  66. if(flipper.isFlipping()){
  67. flipper.stopFlipping();
  68. }else{
  69. flipper.startFlipping();
  70. }
  71. returntrue;
  72. }
  73. @Override
  74. publicbooleanonTouchEvent(MotionEventevent){
  75. returnthis.detector.onTouchEvent(event);
  76. }
  77. @Override
  78. publicbooleanonDown(MotionEvente){
  79. //TODOAuto-generatedmethodstub
  80. returnfalse;
  81. }
  82. @Override
  83. publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,floatvelocityY){
  84. //用户按下屏幕,快速移动后松开(就是在屏幕上滑动)
  85. //e1:第一个ACTION_DOWN事件(手指按下的那一点)
  86. //e2:最后一个ACTION_MOVE事件(手指松开的那一点)
  87. //velocityX:手指在x轴移动的速度单位:像素/秒
  88. //velocityY:手指在y轴移动的速度单位:像素/秒
  89. if(e1.getX()-e2.getX()>60){
  90. this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));
  91. this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));
  92. this.flipper.showNext();
  93. returntrue;
  94. }elseif(e1.getX()-e2.getX()<-60){
  95. this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_in));
  96. this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_out));
  97. this.flipper.showPrevious();
  98. returntrue;
  99. }
  100. returnfalse;
  101. }
  102. @Override
  103. publicvoidonLongPress(MotionEvente){
  104. //TODOAuto-generatedmethodstub
  105. }
  106. @Override
  107. publicbooleanonScroll(MotionEvente1,MotionEvente2,floatdistanceX,
  108. floatdistanceY){
  109. //TODOAuto-generatedmethodstub
  110. returnfalse;
  111. }
  112. @Override
  113. publicvoidonShowPress(MotionEvente){
  114. //TODOAuto-generatedmethodstub
  115. }
  116. @Override
  117. publicbooleanonSingleTapUp(MotionEvente){
  118. //TODOAuto-generatedmethodstub
  119. returnfalse;
  120. }
  121. }
import android.app.Activity;import android.os.Bundle;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.view.animation.AnimationUtils;import android.widget.ImageButton;import android.widget.ViewFlipper;/** * ViewFlipperTest.java * @author Cloay * 2011-6-24 */public class ViewFlipperTest extends Activity implements OnGestureListener {private ViewFlipper flipper;private GestureDetector detector;private ImageButton pre1Button;private ImageButton next1Button;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.zd);pre1Button = (ImageButton)findViewById(R.id.preButton1);pre1Button.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif(event.getAction()==MotionEvent.ACTION_DOWN){ //按钮按下背景图片pre1Button.setBackgroundResource(R.drawable.pre_button1);}//按钮up后设置背景图片,并滑动到前一页面else if(event.getAction()==MotionEvent.ACTION_UP){pre1Button.setBackgroundResource(R.drawable.pre_button);flipper.setInAnimation(AnimationUtils.loadAnimation(TestFlip.this, R.anim.push_right_in));flipper.setOutAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_right_out));flipper.showPrevious();}return false;}});next1Button = (ImageButton)findViewById(R.id.nextButton1);next1Button.setOnTouchListener(new OnTouchListener(){@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif(event.getAction()==MotionEvent.ACTION_DOWN){next1Button.setBackgroundResource(R.drawable.next_button1);}//按钮up后设置背景图片,并滑动到后一页面else if(event.getAction()==MotionEvent.ACTION_UP){next1Button.setBackgroundResource(R.drawable.next_button);flipper.setInAnimation(AnimationUtils.loadAnimation(TestFlip.this, R.anim.push_left_in));flipper.setOutAnimation(AnimationUtils.loadAnimation(TestFlip.this,R.anim.push_left_out));flipper.showNext();}return false;}});detector = new GestureDetector(this);flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper);}public boolean onDoubleTap(MotionEvent e) { if(flipper.isFlipping()) { flipper.stopFlipping(); }else { flipper.startFlipping(); } return true; } @Overridepublic boolean onTouchEvent(MotionEvent event) {return this.detector.onTouchEvent(event);}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { //用户按下屏幕,快速移动后松开(就是在屏幕上滑动)//e1:第一个ACTION_DOWN事件(手指按下的那一点)//e2:最后一个ACTION_MOVE事件 (手指松开的那一点)//velocityX:手指在x轴移动的速度 单位:像素/秒//velocityY:手指在y轴移动的速度 单位:像素/秒if (e1.getX() - e2.getX() > 60) {this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));this.flipper.showNext();return true;} else if (e1.getX() - e2.getX() < -60) {this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));this.flipper.showPrevious();return true;}return false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubreturn false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubreturn false;}}
测试时,鼠标点击模拟器快速移动松开(就是在屏幕上模拟手指滑动),结果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值