为什么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.3k
1 个回答
✓ 已被采纳

因为强转损失了精度

宣传栏