android之ViewFlipper滑屏切換效果
設置了三個頁面,布局文件如下:
<?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 接口,具體如下:
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;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.zd); pre1Button = (ImageButton)findViewById(R.id.preButton1); pre1Button.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(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(){ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(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; }
@Override public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
@Override public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub return false;
}
@Override public 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;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}</pre>測試時,鼠標點擊模擬器快速移動松開(就是在屏幕上模擬手指滑動),結果如下:
轉自:http://blog.csdn.net/shang_515/article/details/6782133
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!