main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView" android:layout_width="240dip" android:layout_height="300dip" android:layout_centerInParent="true" /> </RelativeLayout>
MainActivity如下:
package cn.com.testgesture2; import android.os.Bundle; import android.view.GestureDetector; import android.view.View; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; /** * Demo描述: * 在ImageView控件上滑动 * 依据滑动速度的不同显示不同的照片 * * 重要参考: * http://www.cnblogs.com/zfrr/archive/2012/07/19/2599591.html * 介绍了onFling中速度参数正负的坐标 */ public class MainActivity extends Activity { private GestureDetector mGestureDetector; private ImageView mImageView; private Bitmap mBitmapA; private Bitmap mBitmapB; private Bitmap mBitmapC; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init() { mBitmapA=BitmapFactory.decodeResource(getResources(), R.drawable.a); mBitmapB=BitmapFactory.decodeResource(getResources(), R.drawable.b); mBitmapC=BitmapFactory.decodeResource(getResources(), R.drawable.c); mGestureDetector = new GestureDetector(MainActivity.this,new GestureListenerImpl()); mImageView=(ImageView) findViewById(R.id.imageView); mImageView.setImageBitmap(mBitmapA); mImageView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent event) { return mGestureDetector.onTouchEvent(event); } }); } //覆写Activity的onTouchEvent方法 //将Touch事件交给GestureDetector处理 @Override public boolean onTouchEvent(MotionEvent event) { return mGestureDetector.onTouchEvent(event); } //以下为OnGestureListener的实现 private class GestureListenerImpl implements OnGestureListener { //触摸屏幕时均会调用该方法 @Override public boolean onDown(MotionEvent e) { return false; } //手指在屏幕上拖动时会调用该方法 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) { System.out.println("---> 手势中的 onFling 方法"); System.out.println("velocityX="+velocityX+",velocityY="+velocityY); if (2000f<velocityX&&velocityX>3000f){ mImageView.setImageBitmap(mBitmapA); } if (3000f<velocityX&&velocityX>4000f){ mImageView.setImageBitmap(mBitmapB); } if (4000f<velocityX&&velocityX>5000f){ mImageView.setImageBitmap(mBitmapC); } return false; } //长按触摸屏幕时均会调用该方法 @Override public void onLongPress(MotionEvent e) { } //手指在屏幕上滚动时会调用该方法 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) { return false; } //在触摸屏上按下,且未移动和松开时调用该方法 @Override public void onShowPress(MotionEvent e) { } //轻击屏幕时调用该方法 @Override public boolean onSingleTapUp(MotionEvent e) { return false; } } }
时间: 2024-10-23 21:29:19