标签:android开发
interface
|
GestureDetector.OnDoubleTapListener | 响应双击屏幕事件 |
interface
|
GestureDetector.OnGestureListener | 响应手势事件 |
class
|
GestureDetector.SimpleOnGestureListener | 当仅需要监听所有手势的一个子集时使用 |
构造一个GestureDetector实例,传入一个事件监听器对象listener(作为参数)
|
Creates a GestureDetector with the supplied listener that runs deferred events on the thread associated with the supplied
Handler |
/*功能:根据用户的不同动作检测判定触发的是哪种手势动作*/ package com.example.android_gesture_1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.provider.ContactsContract; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.widget.Toast; public class GestureTest extends Activity implements OnGestureListener { //1.定义手势检测实例 GestureDetector detector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //2.创建手势检测器:this-表明该Activity本身会作为GestureDetector.OnGestureListener监听器 detector=new GestureDetector(this,this); } //3.指定把Activity的TouchEvent交给GestureDetector处理 @Override public boolean onTouchEvent(MotionEvent event) { return detector.onTouchEvent(event); } //4.手势检测判定--------------------------------------------/ @Override //a.当触碰事件按下时触发 public boolean onDown(MotionEvent e) { // Toast.makeText(this, "onDown:当触碰事件按下时触发", Toast.LENGTH_SHORT).show(); return false; } @Override //b.当用户在触摸屏上按下,而且还未移动和松开 public void onShowPress(MotionEvent e) { // Toast.makeText(this, "onShowPress:当用户在触摸屏上按下,而且还未移动和松开", Toast.LENGTH_SHORT).show(); } @Override //c.当用户在触摸屏上的轻击事件将会触发该方法 public boolean onSingleTapUp(MotionEvent e) { // Toast.makeText(this, "onSingleTapUp:当用户在触摸屏上的轻击事件将会触发该方法", Toast.LENGTH_SHORT).show(); return false; } @Override //d.当用户在屏幕上“滚动”时触发该方法 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // Toast.makeText(this, "onScroll:当用户在屏幕上“滚动”时触发该方法", Toast.LENGTH_SHORT).show(); return false; } @Override //e.当用户在屏幕长按时触发该方法 public void onLongPress(MotionEvent e) { Toast.makeText(this, "onLongPress:当用户在屏幕长按时触发该方法", Toast.LENGTH_SHORT).show(); } @Override //f.当用户在触摸屏上“拖过”时触发该方法 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Toast.makeText(this, "onFling:当用户在触摸屏上“拖过”时触发该方法", Toast.LENGTH_SHORT).show(); Intent intent=new Intent(Intent.ACTION_VIEW, ContactsContract.Contacts.CONTENT_URI); //启动联系人 startActivity(intent); return false; } }
package com.example.android_gesture_2; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.provider.ContactsContract; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.widget.ImageView; public class GestureZoom extends Activity implements OnGestureListener { //1.定义手势检测器实例 GestureDetector detector; ImageView imageview; //2.图片相关资源初始化 Bitmap bitmap; //初始的图片资源 int width,height; //定义图片的宽、高 float currentScale = 1; //记录当前的缩放比例 Matrix matrix; //控制图片缩放的Matrix对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mian); //3.创建手势检测器 detector=new GestureDetector(this,this); imageview=(ImageView)findViewById(R.id.image); matrix = new Matrix(); //4.获取被缩放的源图片 bitmap=BitmapFactory.decodeResource(this.getResources(),R.drawable.flower); width=bitmap.getWidth(); //获取位图宽 height=bitmap.getHeight(); //获取位图高 imageview.setImageBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.flower));//设置ImageView初始化时显示的图片 } //5.把Activity的OnTouch事件交给GestureDetector处理 @Override public boolean onTouchEvent(MotionEvent event) { return detector.onTouchEvent(event); } //6.手势判定并响应----------------------------------------------------// @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { velocityX=velocityX>4000?4000:velocityX; //横向上的拖动速度velocityX(放大) velocityX=velocityX<-4000?-4000:velocityX; //横向上的拖动速度velocityX(缩小) //a.根据手势的速度来计算缩放比例,如果velocityX>0,放大图像,否者缩小图像 currentScale +=currentScale*velocityX/4000.0f; currentScale=currentScale>0.01?currentScale:0.01f; //保证currentScale不会等于0 matrix.reset(); //重置Matrix matrix.setScale(currentScale, currentScale, 160, 200); //缩放Matrix BitmapDrawable tmp=(BitmapDrawable)imageview.getDrawable(); if(!tmp.getBitmap().isRecycled()) //如果图片还未收回,先强制收回该图片 { tmp.getBitmap().recycle(); } Bitmap bitmap2=Bitmap.createBitmap(bitmap, 0, 0, width,height,matrix,true);//根据原始图位和Matrix创建新图片 imageview.setImageBitmap(bitmap2);//显示新的位图 return true; } //-----------------------------------------------------------------// @Override public boolean onDown(MotionEvent e) { // 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; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } }
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.android_gesture_3.GestureFlip" > <!-- 定义ViewFlipper组件 --> <ViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
package com.example.android_gesture_3; 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.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.ViewFlipper; public class GestureFlip extends Activity implements OnGestureListener { //1.定义ViewFlipper、手势检测、动画数组实例 ViewFlipper flipper; GestureDetector detector; Animation[] animations=new Animation[4]; //定义一个动画数组,用于为ViewFlipper指定切换动画效果 final int FLIP_DISTANCE=50; //定义手势动作两点之间的最小距离 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //2.创建手势检测器 detector=new GestureDetector(this,this); //3.ViewFlipper、Animation初始化 flipper=(ViewFlipper)findViewById(R.id.flipper); //获取ViewFlipper组件 flipper.addView(addImageView(R.drawable.photo1)); //为ViewFlipper添加5个ImageView组件 flipper.addView(addImageView(R.drawable.photo2)); flipper.addView(addImageView(R.drawable.photo3)); flipper.addView(addImageView(R.drawable.photo4)); flipper.addView(addImageView(R.drawable.photo5)); animations[0] = AnimationUtils.loadAnimation(this,R.anim.abc_slide_out_top);//初始化Animation数组 animations[1] = AnimationUtils.loadAnimation(this,R.anim.abc_slide_in_top); animations[2] = AnimationUtils.loadAnimation(this,R.anim.abc_slide_out_bottom); animations[3] = AnimationUtils.loadAnimation(this,R.anim.abc_slide_in_bottom); } //4.定义添加ImageView的工具方法 private View addImageView(int resId) { ImageView imageView = new ImageView(this); imageView.setImageResource(resId); imageView.setScaleType(ImageView.ScaleType.CENTER); return imageView; } //5.将该Activity上的触碰事件交给GestureDetector处理 @Override public boolean onTouchEvent(MotionEvent event) { return detector.onTouchEvent(event); } //6.在触摸屏上“拖过”时触发该方法 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //如果第一个触点事件的X坐标大于第二触发事件的X坐标超过FLIP_DISTANCE //也就是手势从右向左滑 if(e1.getX()-e2.getX()>FLIP_DISTANCE) { //为flipper设置切换的动画效果 flipper.setAnimation(animations[0]); flipper.setAnimation(animations[1]); flipper.showPrevious(); return true; } //如果第二个触点事件的X坐标大于第一个触点事件的X坐标超过FLIP_DISTANCE else if(e2.getX()-e1.getX()>FLIP_DISTANCE) { //为flipper设置切换的动画效果 flipper.setAnimation(animations[2]); flipper.setAnimation(animations[3]); flipper.showPrevious(); return true; } return false; } //-------------------------------------------------------------------------// @Override public boolean onDown(MotionEvent e) { 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; } }
标签:android开发
原文地址:http://blog.csdn.net/u012637501/article/details/42505453