getX 和getRawX

weixin_34402408發表於2017-06-01

雖然知道getX是返回相對控制元件的座標,getRawX返回的相對螢幕的座標,但是腦子一下子轉不過來,
認為在移動控制元件的時候,使用getX 相減獲得偏移量和getRawX的應該是一樣的。
實際跑一下發現會有鬼畜效果,後來想了一下getX在獲得座標的時候 控制元件移動了,就會導致獲得的座標偏移量“不準確”。

簡直太白痴了。。


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getX();
                lastY = event.getY();
                lastRawX = event.getRawX();
                lastRawY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float disX = event.getX() - lastX;
                float disY = event.getY() - lastY;


                float disRawX = event.getRawX() - lastRawX;
                float disRawY = event.getRawY() - lastRawY;
                PtrCLog.d("JellyTextView", "disX    ====: " + disX);
                PtrCLog.d("JellyTextView", "lastX   ====: " + lastX);
                PtrCLog.d("JellyTextView", "disRawX ====: " + disRawX);
                PtrCLog.d("JellyTextView", "lastRawX====: " + lastRawX);

                PtrCLog.d("JellyTextView", "====");

                offsetLeftAndRight((int) disRawX);
                offsetTopAndBottom((int) disRawY);
                lastX = event.getX();
                lastY = event.getY();

                lastRawX = event.getRawX();
                lastRawY = event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                mScroller.startScroll((int) getX(), (int) getY(), -(int) (getX() - startX),
                        -(int) (getY() - startY));
                invalidate();
                break;
        }
        /**
         public final float getRawX () {
         return nativeGetRawAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);
         }

         public final float getX(int pointerIndex) {
         return nativeGetAxisValue(mNativePtr, AXIS_X, pointerIndex, HISTORY_CURRENT);
         }
         */
        return super.onTouchEvent(event);
    }

相關文章