Android:GestureDetector手势识别类

为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过 GestureDetector.OnGestureListener 来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:

boolean  onDoubleTap(MotionEvent e) 
解释:双击的第二下Touch down时触发 
boolean  onDoubleTapEvent(MotionEvent e) 
解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。 
boolean  onDown(MotionEvent e) 
解释:Touch down时触发 
boolean  onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
解释:Touch了滑动一点距离后,up时触发。 
void  onLongPress(MotionEvent e) 
解释:Touch了不移动一直Touch down时触发 
boolean  onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
解释:Touch了滑动时触发。 
void  onShowPress(MotionEvent e) 
解释:Touch了还没有滑动时触发 
(与onDown,onLongPress比较 
onDown只要Touch down一定立刻触发。 
而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。 
所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。


boolean  onSingleTapConfirmed(MotionEvent e) 
boolean  onSingleTapUp(MotionEvent e) 
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。

点击一下非常快的(不滑动)Touchup: 
onDown->onSingleTapUp->onSingleTapConfirmed 
点击一下稍微慢点的(不滑动)Touchup: 
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

使用GestureDetector需要在View中重写onTouchEvent事件,例如:

[java]  view plain copy
  1. GestureDetector mGesture = null;  
  2. @Override  
  3.     public boolean onTouch(View v, MotionEvent event)  
  4.     {  
  5.         // TODO Auto-generated method stub  
  6.         return mGesture.onTouchEvent(event);  
  7.     }  

 详细的测试例子如下:

[cpp]  view plain copy
  1. package com.jiubang.android.gesturetest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.GestureDetector;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.GestureDetector.SimpleOnGestureListener;  
  10. import android.view.View.OnTouchListener;  
  11. import android.widget.Button;  
  12.   
  13. public class GestureActivity extends Activity  
  14.             implements OnTouchListener  
  15. {  
  16.     private Button mButton = null;  
  17.     GestureDetector mGesture = null;  
  18.       
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.           
  25.         Log.i("TEST""onCreate");  
  26.         mButton = (Button)findViewById(R.id.button1);  
  27.         mButton.setOnTouchListener(this);  
  28.         mGesture = new GestureDetector(thisnew GestureListener());  
  29.     }  
  30.   
  31.     @Override  
  32.     public boolean onTouch(View v, MotionEvent event)  
  33.     {  
  34.         // TODO Auto-generated method stub  
  35.         return mGesture.onTouchEvent(event);  
  36.     }  
  37.       
  38.     class GestureListener extends SimpleOnGestureListener  
  39.     {  
  40.   
  41.         @Override  
  42.         public boolean onDoubleTap(MotionEvent e)  
  43.         {  
  44.             // TODO Auto-generated method stub  
  45.             Log.i("TEST""onDoubleTap");  
  46.             return super.onDoubleTap(e);  
  47.         }  
  48.   
  49.         @Override  
  50.         public boolean onDown(MotionEvent e)  
  51.         {  
  52.             // TODO Auto-generated method stub  
  53.             Log.i("TEST""onDown");  
  54.             return super.onDown(e);  
  55.         }  
  56.   
  57.         @Override  
  58.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  59.                 float velocityY)  
  60.         {  
  61.             // TODO Auto-generated method stub  
  62.             Log.i("TEST""onFling:velocityX = " + velocityX + " velocityY" + velocityY);  
  63.             return super.onFling(e1, e2, velocityX, velocityY);  
  64.         }  
  65.   
  66.         @Override  
  67.         public void onLongPress(MotionEvent e)  
  68.         {  
  69.             // TODO Auto-generated method stub  
  70.             Log.i("TEST""onLongPress");  
  71.             super.onLongPress(e);  
  72.         }  
  73.   
  74.         @Override  
  75.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  76.                 float distanceX, float distanceY)  
  77.         {  
  78.             // TODO Auto-generated method stub  
  79.             Log.i("TEST""onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);  
  80.             return super.onScroll(e1, e2, distanceX, distanceY);  
  81.         }  
  82.   
  83.         @Override  
  84.         public boolean onSingleTapUp(MotionEvent e)  
  85.         {  
  86.             // TODO Auto-generated method stub  
  87.             Log.i("TEST""onSingleTapUp");  
  88.             return super.onSingleTapUp(e);  
  89.         }  
  90.           
  91.     }  
  92. }  

//以上部分内容选自网络

视屏播放界面的手势控制界面:

[java]  view plain copy
  1. package com.azhibo.zhibo.view;  
  2. import java.util.HashMap;  
  3. import java.util.Map;  
  4. import io.vov.vitamio.LibsChecker;  
  5. import io.vov.vitamio.MediaPlayer;  
  6. import io.vov.vitamio.MediaPlayer.OnCompletionListener;  
  7. import io.vov.vitamio.MediaPlayer.OnErrorListener;  
  8. import io.vov.vitamio.MediaPlayer.OnPreparedListener;  
  9. import io.vov.vitamio.widget.MediaController;  
  10. import io.vov.vitamio.widget.VideoIntefaceClick;  
  11. import io.vov.vitamio.widget.VideoUtils;  
  12. import io.vov.vitamio.widget.VideoView;  
  13. import android.app.Activity;  
  14. import android.content.Context;  
  15. import android.content.res.Configuration;  
  16. import android.media.AudioManager;  
  17. import android.net.Uri;  
  18. import android.os.Bundle;  
  19. import android.os.Handler;  
  20. import android.os.Message;  
  21. import android.util.Log;  
  22. import android.view.Display;  
  23. import android.view.GestureDetector;  
  24. import android.view.MotionEvent;  
  25. import android.view.View;  
  26. import android.view.ViewGroup;  
  27. import android.view.Window;  
  28. import android.view.WindowManager;  
  29. import android.view.GestureDetector.SimpleOnGestureListener;  
  30. import android.widget.ImageButton;  
  31. import android.widget.ImageView;  
  32. import android.widget.ProgressBar;  
  33. import android.widget.Toast;  
  34.   
  35. import com.apple.down.assit.DownLoadInfo;  
  36. import com.azhibo.zhibo.R;  
  37. import com.umeng.analytics.MobclickAgent;  
  38.   
  39. public class VideoViewActivity extends Activity {  
  40.     private static int n=0;  
  41.     //private String path = "";  
  42.     private VideoView mVideoView;  
  43.     public VideoIntefaceClick listItemCall;  
  44.     private Uri pathUri;  
  45.     private ProgressBar probar;  
  46.     private String type=null;  
  47.     private int mLayout = VideoView.VIDEO_LAYOUT_SCALE;  
  48.     private View mVolumeBrightnessLayout;  
  49.     private ImageView mOperationBg;  
  50.     private ImageView mOperationPercent;  
  51.     private AudioManager mAudioManager;  
  52.     /** 最大声音 */  
  53.     private int mMaxVolume;  
  54.     /** 当前声音 */  
  55.     private int mVolume = -1;  
  56.     /** 当前亮度 */  
  57.     private float mBrightness = -1f;      
  58.     private GestureDetector mGestureDetector;  
  59.     private String path="";  
  60.     private String title="";  
  61.     private DownLoadInfo info=null;  
  62.   
  63.     //xulongheng 播放界面的视频监听事件  
  64.     private class ListItemViewListener implements VideoIntefaceClick {  
  65.         @Override  
  66.         public void onExitVideo() {  
  67.             if(type!=null&&type.equals("1")){  
  68.                 Toast.makeText(VideoViewActivity.this"请耐心等待视频下载完毕!", Toast.LENGTH_SHORT).show();  
  69.             }else{  
  70.                 Toast.makeText(VideoViewActivity.this"本直播源需要休息一下,换一个试试吧!", Toast.LENGTH_SHORT).show();  
  71.             }  
  72.               
  73.             Log.v("xulongheng*VideoViewActivity*onExitVideo:""--->onExitVideo "+type);  
  74.             VideoViewActivity.this.finish();  
  75.         }  
  76.   
  77.         @Override  
  78.         public void onCheckVagueVide(int statue, String url) {  
  79.             // TODO Auto-generated method stub  
  80.             Log.v("xulongheng*VideoViewActivity*onCheckVagueVide:""--->onCheckVagueVide statue"+statue);  
  81.             Message msg=new Message();  
  82.             msg.obj=url;  
  83.             msg.what=statue;  
  84.             mHandler.sendMessage(msg);  
  85.         }  
  86.     }  
  87.       
  88.     @Override  
  89.     public void onCreate(Bundle icicle) {  
  90.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  91.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  92.           
  93.         super.onCreate(icicle);  
  94.           
  95.         if (!LibsChecker.checkVitamioLibs(this, getClass().getName(), R.string.init_decoders, R.raw.libarm))  
  96.         {  
  97.             return;  
  98.         }  
  99.           
  100.         setContentView(R.layout.videoview);  
  101.         mVideoView = (VideoView) findViewById(R.id.surface_view);  
  102.         probar=(ProgressBar) findViewById(R.id.probar);  
  103.         mVolumeBrightnessLayout = findViewById(R.id.operation_volume_brightness);  
  104.         mOperationBg = (ImageView) findViewById(R.id.operation_bg);  
  105.         mOperationPercent = (ImageView) findViewById(R.id.operation_percent);  
  106.         mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);  
  107.         mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);  
  108.           
  109.         listItemCall = new ListItemViewListener();  
  110.         mVideoView.setVideoIntefaceClick(listItemCall);  
  111.         ImageButton showLeft=(ImageButton) findViewById(R.id.showLeft);  
  112.         path=this.getIntent().getStringExtra("url");  
  113.         title=getIntent().getStringExtra("title");  
  114. //      info=(DownLoadInfo) getIntent().getSerializableExtra("info");  
  115.           
  116.           
  117. //      path="http://218.26.231.201/youku/69722EE89093676C988636194/030020010052A91FBF7A9A036E0AF8E178E089-4698-FC9B-45D5-E65EA8462AF1.mp4";  
  118.         Log.v("xulongheng*VideoView*uri***"":"+path);  
  119.         pathUri = Uri.parse(path);  
  120.         type=this.getIntent().getStringExtra("type");         
  121.           
  122.         if(type!=null&&type.equals("1")){  
  123.             probar.setVisibility(View.GONE);  
  124.             StringBuilder builder=new StringBuilder();  
  125.             for(int i=0;i"/").length;i++){  
  126.                 if(i!=0){  
  127.                     builder.append(path.split("/")[i]);  
  128.                     if(i!=path.split("/").length-1)  
  129.                         builder.append("/");  
  130.                 }  
  131.             }  
  132.             mVideoView.setVideoPath(builder.toString(),this,this.getString(R.string.app_name),this.getString(R.string.load_now));  
  133.         }else{  
  134.             VideoUtils videoUtls=new VideoUtils(this,pathUri);  
  135.             if(videoUtls.checkVagueVide()){  
  136.                 Log.i("HU""--->pathpath"+pathUri);  
  137.                 videoUtls.setVideoIntefaceClick(listItemCall);  
  138.                 videoUtls.doWith(pathUri.toString());  
  139.             }else{  
  140.                 probar.setVisibility(View.GONE);  
  141.                 mVideoView.setVideoURI(pathUri,this,this.getString(R.string.app_name),this.getString(R.string.load_now));  
  142.             }  
  143.         }         
  144.         mVideoView.start();  
  145.           
  146.         //xlh_add 添加播放器监听  
  147.         initVideoListen();  
  148.         mGestureDetector = new GestureDetector(thisnew MyGestureListener());  
  149.               
  150.     }  
  151.     public void initVideoListen(){  
  152.           
  153.         mVideoView.setVideoQuality(MediaPlayer.VIDEOQUALITY_HIGH);  
  154.         mVideoView.setOnPreparedListener(new OnPreparedListener() {  
  155.             @Override  
  156.             public void onPrepared(MediaPlayer mp) {  
  157.                 mVideoView.setSubShown(true);  
  158.                   
  159.                 Log.v("xulongheng*VideoActivity*播放器播放准备监听:""onPrepared");  
  160.             }  
  161.         });  
  162.         mVideoView.setOnCompletionListener(new OnCompletionListener() {  
  163.             @Override  
  164.             public void onCompletion(MediaPlayer arg0) {  
  165.                 n++;  
  166.               
  167.                 Toast.makeText(VideoViewActivity.this"播放即将结束!", Toast.LENGTH_SHORT).show();             
  168.                 VideoViewActivity.this.finish();  
  169.                 Log.v("xulongheng*VideoActivity*播放器播放完成监听:""onCompletion"+n);  
  170.             }  
  171.   
  172.         });  
  173.           
  174.         mVideoView.setOnErrorListener(new OnErrorListener() {  
  175.               
  176.             @Override  
  177.             public boolean onError(MediaPlayer arg0, int arg1, int arg2) {  
  178.                 //播放源错误统计分析 未完成  
  179.                 Map source = new HashMap();  
  180.                 source.put("url", path);  
  181.                 if(title!=null&&!title.equals(""))  
  182.                     source.put("title", title);  
  183.                   
  184.                   
  185.                 MobclickAgent.onEvent(VideoViewActivity.this"ErrorSource",source);      
  186.                   
  187.                 Toast.makeText(VideoViewActivity.this"本视频无法播放", Toast.LENGTH_SHORT).show();             
  188.                 VideoViewActivity.this.finish();  
  189.                   
  190.                 Log.v("xulongheng*VideoActivity*播放器播放错误监听:""onError:"+arg1+"/"+arg2);  
  191.                 return false;  
  192.             }  
  193.         });  
  194.         mVideoView.setMediaController(new MediaController(this));  
  195.           
  196.           
  197.           
  198.     }  
  199.       
  200.   
  201.     @Override  
  202.     public void onConfigurationChanged(Configuration newConfig) {  
  203.         if (mVideoView != null)  
  204.         {  
  205.             mVideoView.setVideoLayout(mLayout, 0);  
  206.         }  
  207.           
  208.         super.onConfigurationChanged(newConfig);  
  209.     }  
  210.       
  211.     public Handler mHandler = new Handler() {  
  212.         @SuppressWarnings("unchecked")  
  213.         public void handleMessage(Message msg) {  
  214.             probar.setVisibility(View.GONE);  
  215.               
  216.             switch(msg.what){  
  217.             case 0:  
  218.                 mVideoView.setVideoURI(Uri.parse(msg.obj.toString()),VideoViewActivity.this,VideoViewActivity.this.getString(R.string.app_name),VideoViewActivity.this.getString(R.string.load_now));  
  219.                 break;  
  220.             case 1:  
  221.                 mVideoView.setVideoURI(Uri.parse(msg.obj.toString()),VideoViewActivity.this,VideoViewActivity.this.getString(R.string.app_name),VideoViewActivity.this.getString(R.string.load_now));  
  222.                 break;  
  223.             }  
  224.         }  
  225.     };  
  226.       
  227.     @Override  
  228.     public boolean onTouchEvent(MotionEvent event) {  
  229.         if (mGestureDetector.onTouchEvent(event))  
  230.             return true;  
  231.   
  232.         // 处理手势结束 多点触控之MotionEvent.ACTION_MASK作用  
  233.         switch (event.getAction() & MotionEvent.ACTION_MASK) {  
  234.         case MotionEvent.ACTION_UP:  
  235.             endGesture();  
  236.             break;  
  237.         }  
  238.   
  239.         return super.onTouchEvent(event);  
  240.     }  
  241.   
  242.     /** 手势结束 */  
  243.     private void endGesture() {  
  244.         mVolume = -1;  
  245.         mBrightness = -1f;  
  246.   
  247.         // 隐藏  
  248.         mDismissHandler.removeMessages(0);  
  249.         mDismissHandler.sendEmptyMessageDelayed(0500);  
  250.     }  
  251.   
  252.     //xulongheng 触屏横向调音量  
  253.     private class MyGestureListener extends SimpleOnGestureListener {  
  254.   
  255.         /** 双击 */  
  256.         @Override  
  257.         public boolean onDoubleTap(MotionEvent e) {  
  258.             if (mLayout == VideoView.VIDEO_LAYOUT_SCALE)  
  259.                 mLayout = VideoView.VIDEO_LAYOUT_ZOOM;  
  260.             else  
  261.                 mLayout = VideoView.VIDEO_LAYOUT_SCALE;  
  262.               
  263.             Log.d("Scale = ", Integer.toString(mLayout));  
  264.               
  265.             if (mVideoView != null)  
  266.             {  
  267.                 mVideoView.setVideoLayout(mLayout, 0);  
  268.             }  
  269.               
  270.             return true;  
  271.         }  
  272.   
  273.         /** 左右两边滑动调节音量两度 */  
  274.         @Override  
  275.         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  276.             float mOldX = e1.getX(), mOldY = e1.getY();  
  277.             int y = (int) e2.getRawY();  
  278.             Display disp = getWindowManager().getDefaultDisplay();  
  279.             int windowWidth = disp.getWidth();  
  280.             int windowHeight = disp.getHeight();  
  281.   
  282.             if (mOldX > windowWidth * 4.0 / 5)// 右边滑动  
  283.                 onVolumeSlide((mOldY - y) / windowHeight);  
  284.             else if (mOldX < windowWidth / 5.0)// 左边滑动  
  285.                 onBrightnessSlide((mOldY - y) / windowHeight);  
  286.   
  287.             return super.onScroll(e1, e2, distanceX, distanceY);  
  288.         }  
  289.     }  
  290.       
  291.     /** xulongheng定时隐藏 */  
  292.     private Handler mDismissHandler = new Handler() {  
  293.         @Override  
  294.         public void handleMessage(Message msg) {  
  295.             mVolumeBrightnessLayout.setVisibility(View.GONE);  
  296.         }  
  297.     };  
  298.   
  299.     /** 
  300.      * 滑动改变声音大小 
  301.      *  
  302.      * @param percent 
  303.      */  
  304.     private void onVolumeSlide(float percent) {  
  305.         if (mVolume == -1) {  
  306.             mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);  
  307.             if (mVolume < 0)  
  308.                 mVolume = 0;  
  309.   
  310.             // 显示  
  311.             mOperationBg.setImageResource(R.drawable.video_volumn_bg);  
  312.             mVolumeBrightnessLayout.setVisibility(View.VISIBLE);  
  313.         }  
  314.   
  315.         int index = (int) (percent * mMaxVolume) + mVolume;  
  316.         if (index > mMaxVolume)  
  317.             index = mMaxVolume;  
  318.         else if (index < 0)  
  319.             index = 0;  
  320.   
  321.         // 变更声音  
  322.         mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);  
  323.   
  324.         // 变更进度条  
  325.         ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();  
  326.         lp.width = findViewById(R.id.operation_full).getLayoutParams().width * index / mMaxVolume;  
  327.         mOperationPercent.setLayoutParams(lp);  
  328.     }  
  329.   
  330.     /** 
  331.      * xulongheng 滑动改变亮度 
  332.      *  
  333.      * @param percent 
  334.      */  
  335.     private void onBrightnessSlide(float percent) {  
  336.         if (mBrightness < 0) {  
  337.             mBrightness = getWindow().getAttributes().screenBrightness;  
  338.             if (mBrightness <= 0.00f)  
  339.                 mBrightness = 0.50f;  
  340.             if (mBrightness < 0.01f)  
  341.                 mBrightness = 0.01f;  
  342.   
  343.             // 显示  
  344.             mOperationBg.setImageResource(R.drawable.video_brightness_bg);  
  345.             mVolumeBrightnessLayout.setVisibility(View.VISIBLE);  
  346.         }  
  347.         WindowManager.LayoutParams lpa = getWindow().getAttributes();  
  348.         lpa.screenBrightness = mBrightness + percent;  
  349.         if (lpa.screenBrightness > 1.0f)  
  350.             lpa.screenBrightness = 1.0f;  
  351.         else if (lpa.screenBrightness < 0.01f)  
  352.             lpa.screenBrightness = 0.01f;  
  353.         getWindow().setAttributes(lpa);  
  354.   
  355.         ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();  
  356.         lp.width = (int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness);  
  357.         mOperationPercent.setLayoutParams(lp);  
  358.     }     
  359. }  

你可能感兴趣的:(Android,Android)