当前位置: 代码迷 >> Android >> android处理单击双击跟滑动事件
  详细解决方案

android处理单击双击跟滑动事件

热度:7   发布时间:2016-05-01 16:48:53.0
android处理单击双击和滑动事件

android有处理鼠标动作的GestureDetector,使用起来也挺方便,几个常用事件你都可以找到接口接入处理:

单击、双击(比如双击放大)、滑动(比如翻页)
下面是简化的例子,参考并整合所得,来源参考页尾参考内容:

package com.aslibra.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class touchGesture extends Activity implements OnGestureListener?{???????
??private LinearLayout main;???????
??private TextView viewA;
??private GestureDetector gestureScanner;

[email protected]
??public void onCreate(Bundle savedInstanceState) {???
????super.onCreate(savedInstanceState);???

????gestureScanner = new GestureDetector(this);???
????gestureScanner.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener(){??
??????public boolean onDoubleTap(MotionEvent e) {??
????????viewA.setText("-" + "onDoubleTap" + "-");??
????????//双击时产生一次
????????Log.v("test", "onDoubleTap");
????????return false;??
??????}
??????public boolean onDoubleTapEvent(MotionEvent e) {??
????????//双击时产生两次
????????Log.v("test", "onDoubleTapEvent");
????????return false;
??????}??
??????public boolean onSingleTapConfirmed(MotionEvent e) {??
????????viewA.setText("-" + "onSingleTapConfirmed" + "-");??
????????//短快的点击算一次单击
????????Log.v("test", "onSingleTapConfirmed");
????????return false;??
??????}??
????});?


????main = new LinearLayout(this);???
????main.setBackgroundColor(Color.GRAY);
????main.setLayoutParams(new LinearLayout.LayoutParams(320,480));?
????main.setOrientation(LinearLayout.VERTICAL);

????viewA = new TextView(this);
????viewA.setBackgroundColor(Color.YELLOW);
????viewA.setTextColor(Color.BLACK);???
????viewA.setTextSize(16);
????viewA.setLayoutParams(new LinearLayout.LayoutParams(320,50));???
????main.addView(viewA);

????setContentView(main);???
??}

[email protected]
??public boolean onTouchEvent(MotionEvent me) {???
????return gestureScanner.onTouchEvent(me);???
??}???

[email protected]
??public boolean onDown(MotionEvent e) {???
????//viewA.setText("-" + "DOWN" + "-");???
????return true;???
??}???

[email protected]
??public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {???
????//viewA.setText("-" + "FLING" + "- "+velocityX + "- "+velocityY);???
????Log.v("test", "onFling "+e1.getX()+" "+e2.getX());
????return true;???
??}???

[email protected]
??public void onLongPress(MotionEvent e) {???
????//viewA.setText("-" + "LONG PRESS" + "-");???
??}???

[email protected]
??public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {???
????//viewA.setText("-" + "SCROLL" + "- "+distanceX + "- "+distanceY);???
????Log.v("test", "onScroll "+e1.getX()+" "+e2.getX());
????return true;???
??}???

[email protected]
??public void onShowPress(MotionEvent e) {???
????//viewA.setText("-" + "SHOW PRESS" + "-");???
??}

[email protected]
??public boolean onSingleTapUp(MotionEvent e) {???
????Log.v("test", "onSingleTapUp");
????//viewA.setText("-" + "SINGLE TAP UP" + "-"+ i++);???
????return true;???
??}???
??
}??

  相关解决方案