为什么onTouch事件的event.getRawX() 等参数使用强转到int反而更准确了?

pdog
  • 573

为什么onTouch事件的event.getRawX() 等参数使用强转到int反而更准确了

       mImageView.setOnTouchListener(new View.OnTouchListener() {

            private int mStartX;
            private int mStartY;
 @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mStartX = (int) event.getRawX();
                        mStartY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int newX = (int) event.getRawX();
                        int newY = (int) event.getRawY();
                        int moveX = newX - mStartX;
                        int moveY = newY - mStartY;
                        mParams.x += moveX;
                        mParams.y += moveY;
                        mStartX = (int) event.getRawX();
                        mStartY = (int) event.getRawY();
                        mWindowManager.updateViewLayout(mImageView,mParams);
                        break;
                    case MotionEvent.ACTION_UP:
                        Toast.makeText(RocketService.this, "asdgasds", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
回复
阅读 2.4k
1 个回答
✓ 已被采纳

因为强转损失了精度

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏