onDoubleTap在CustomView中的GestureDetector.SimpleOnGestureListener中不起作用

时间:2016-11-22 21:58:35

标签: android touch android-custom-view android-windowmanager

  

我创建了一个像ChatHead一样工作的自定义按钮,也喜欢在自定义Button上处理Single,Double和LongPress。除了onDoubleTap()之外,所有其他手势都正常工作。

我创建了一个customView,所有工作都在该类中完成。但是doubleTap无法正常工作。

CustomView

public class CustomButton extends ImageButton {

    private WindowManager mWindowManager;
    private WindowManager.LayoutParams params;
    private CustomButton mCustomButton;

    private Context mContext;

    private int initialX =0;
    private int initialY = 0;
    private float initialTouchX = 0 ;
    private float initialTouchY = 0;

    private GestureDetector gestureDetector;
    private OnMultiEventListener onMultiEventListener;

    public CustomButton(Context context) {
        super(context);
        initView(context);
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        /*setBackgroundColor(Color.YELLOW);
        setImageResource(R.drawable.bg);*/
        mContext = context;
        mCustomButton = this;
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        params = getBtnParams();
        gestureDetector = new GestureDetector(mContext, new SingleTapConfirm());
        /*setBackgroundColor(Color.RED);
        setTextColor(Color.WHITE);
        setText("Hud");*/
        mWindowManager.addView(mCustomButton,params);
        /*View view = LayoutInflater.from(context).inflate(R.layout.linear_assitive_button, null);
        addView(view);*/
    }

    public void setOnMultiEventListener(OnMultiEventListener onMultiEventListener) {
        this.onMultiEventListener = onMultiEventListener;
    }

    private WindowManager.LayoutParams getBtnParams() {
        WindowManager.LayoutParams btnParams = new WindowManager.LayoutParams(
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.TRANSLUCENT);
        btnParams.gravity = Gravity.LEFT | Gravity.TOP;
        btnParams.x = 0;
        btnParams.y = 0;
        return btnParams;
    }

    public void removeFromWindowManager(){
        mWindowManager.removeView(mCustomButton);
    }

    // All event are handled drag, Single click ,Double and Long Pressed Too.
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (gestureDetector.onTouchEvent(event)) {
            // single tap
            //Toast.makeText(mContext, "I am Signle Tap", Toast.LENGTH_SHORT).show();
            return true;
        } else {
            // Code for move and drag

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    return true;
                case MotionEvent.ACTION_MOVE:
                    params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);
                    mWindowManager.updateViewLayout(mCustomButton, params);
                    return true;
            }
        }

        return false;
    }

    private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onSingleTapUp(MotionEvent e) {

            if(onMultiEventListener != null)
                onMultiEventListener.onClick();
            //Toast.makeText(mContext,"Single Pressed",Toast.LENGTH_LONG).show();
            return true;// true means let this event be handle by others
            //return super.onSingleTapUp(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if(onMultiEventListener != null)
                onMultiEventListener.onDoubleClick();
            //Toast.makeText(mContext,"Double Tap",Toast.LENGTH_LONG).show();
            return true;
            //return super.onDoubleTap(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);
            if(onMultiEventListener != null)
                onMultiEventListener.onLongPressed();
            //Toast.makeText(mContext,"Long Pressed",Toast.LENGTH_LONG).show();
        }
    }
}

0 个答案:

没有答案