通过android中的服务轻扫手势检测

时间:2014-05-17 10:05:53

标签: android

如何通过侧边栏(在Android市场上)等服务检测滑动事件.... 我尝试布局服务,因此我能够检测滑动手势但主要问题 布局视图有自己的区域,因此用服务视图检测滑动事件是个坏主意......

public class GestureSwipe extends Service {

private static final int SWIPE_MIN_DISTANCE =5;
private static final int SWIPE_THRESHOLD_VELOCITY = 10;

@SuppressWarnings("deprecation")
final GestureDetector gt = new GestureDetector(new GestureListener());

private WindowManager wm;
private LinearLayout beam;

@Override
public void onCreate() {
    super.onCreate();
    beam = new LinearLayout(this);
    LayoutParams lp = new LayoutParams(10,LayoutParams.MATCH_PARENT);
    beam.setLayoutParams(lp);
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
            10,WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    mParams.gravity = Gravity.LEFT;
    //beam.setBackgroundColor(Color.RED);
    beam.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(final View view, final MotionEvent event) {
            gt.onTouchEvent(event);

            return true;
        }
    });
    wm.addView(beam, mParams);
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

private class GestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            return false; // Right to left
        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            return false; // Left to right
        }
        if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return false; // Bottom to top
        } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {

            return false; // Top to bottom
        }
        return false;
    }
  }
}

我的视图是否可能仅检测手势并忽略触摸事件和 表现得像其他人的透明触摸...... 请告诉我该怎么做? 这样做有什么其他的想法? 感谢所有程序员..

0 个答案:

没有答案