美文网首页
Android View onTouchEvent

Android View onTouchEvent

作者: suwec | 来源:发表于2019-03-06 17:15 被阅读0次

    重写onTouchEvent方法,将返回写成true消费掉触摸事件,
    然后根据当前触摸的坐标点位移View

    @Override
        public boolean onTouchEvent(MotionEvent event) {
            int rawX = (int)event.getRawX();
            int rawY = (int)event.getRawY();
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    lastX = rawX;
                    lastY = rawY;
                    break;
                case MotionEvent.ACTION_MOVE:
                    int offsetX = rawX - lastX;
                    int offsetY = rawY - lastY;
                    offsetLeftAndRight(offsetX);
                    offsetTopAndBottom(offsetY);
                    lastX = rawX;
                    lastY = rawY;
                    break;
                case MotionEvent.ACTION_UP:
    
                    break;
            }
            return true;
        }
    

    效果


    TIM图片20190306171533.gif

    相关文章

      网友评论

          本文标题:Android View onTouchEvent

          本文链接:https://www.haomeiwen.com/subject/dkitpqtx.html