package com.nd.activitydemo;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.GestureDetector.OnGestureListener;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.view.animation.AnimationUtils;import android.widget.ImageView;import android.widget.ViewFlipper;public class ActivityDemoActivity extends Activity implements OnGestureListener {/** Called when the activity is first created. */GestureDetector mGestureDetector;// private static final int SCROLL_MIN_DISTANCE = 160;// private static final int SCROLL_MIN_VELOCITY = 10;private ViewFlipper vf;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setTheme(R.style.Transparent);setContentView(R.layout.transparent);vf = (ViewFlipper) findViewById(R.id.vf);vf.addView(addImageById(R.drawable.guardpic1), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));vf.addView(addImageById(R.drawable.guardpic2), new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));mGestureDetector = new GestureDetector(ActivityDemoActivity.this);}// 開發時對手勢的測試@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubboolean retValue = mGestureDetector.onTouchEvent(event);int action = event.getAction();if (action == MotionEvent.ACTION_UP) {// Helper method for lifted finger} else if (action == MotionEvent.ACTION_CANCEL) {}return retValue;}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubLog.i("Fling", "Fling Happened!");if (e1.getX() - e2.getX() > 120) {this.vf.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out));this.vf.showNext();return true;} else if (e1.getX() - e2.getX() < -120) {this.vf.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_in));this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_right_out));this.vf.showPrevious();return true;}return true;}@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 stubLog.i("MotionEvent", "onScroll.........");// 參數解釋:// e1:第1個ACTION_DOWN MotionEvent// e2:最後一個ACTION_MOVE MotionEvent// velocityX:X軸上的移動速度,像素/秒// velocityY:Y軸上的移動速度,像素/秒// 觸發條件 :// X軸的座標位移大於SCROLL_MIN_DISTANCE,且移動速度大於SCROLL_MIN_VELOCITY個像素/秒// if (e1.getX() - e2.getX() > SCROLL_MIN_DISTANCE// && Math.abs(distanceX) > SCROLL_MIN_VELOCITY) {// // Fling left// iv.setBackgroundResource(R.drawable.guardpic2);// } else if (e2.getX() - e1.getX() > SCROLL_MIN_DISTANCE// && Math.abs(distanceX) > SCROLL_MIN_VELOCITY) {// // Fling right// iv.setBackgroundResource(R.drawable.detailpageguideinfo);// }return false;}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubActivityDemoActivity.this.finish();return false;}public View addImageById(int id) {ImageView iv = new ImageView(this);iv.setImageResource(id);return iv;}}
布局檔案:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><ViewFlipper android:id="@+id/vf" android:layout_width="fill_parent" android:layout_height="fill_parent" /></LinearLayout>
參考資料:
ViewFlipper Animation 使用 http://www.iteye.com/topic/569493
ViewFlipper結合手勢OnGestureListener製作的滑動轉場效果 http://gundumw100.iteye.com/blog/905033【Android】滑動螢幕效果GestureDetector、OnGestureListener、ViewFlipper http://blog.csdn.net/feng88724/article/details/6170694android手勢翻頁效果 http://www.iteye.com/topic/712644
Android 投影片效果自訂 http://www.apkbus.com/android-13210-1-1.html
[經驗分享] Android 淺談Flipper的使用 http://wap.devdiv.com/thread-84867-1-1.html
完整原始碼:http://download.csdn.net/detail/sxsj333/3615788