【MotionEvent】亲测MotionEvent中getX()和getRawX()的区别

情景:昨天看了郭霖仿360浮动窗口,在自定义View,onTouchEvent中看到了

event.getX();
event.getY();
event.getRawX();
event.getRawY();

就想知道getX与getRawX的区别

方案:于是用代码去验证下 两个值的差别

public class Res extends Activity implements View.OnTouchListener {  
  
    Button btn = null;  
    int x = 0;  
    int y = 0;  
    int rawx = 0;  
    int rawy = 0;  
  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        btn = (Button) findViewById(R.id.button);  
        btn.setOnTouchListener(this);  
    }  
  
    public boolean onTouch(View view, MotionEvent event) {  
        int eventaction = event.getAction();  
        switch (eventaction) {  
        case MotionEvent.ACTION_DOWN:  
            break;  
        case MotionEvent.ACTION_MOVE:  
            x = (int) event.getX();  
            y = (int) event.getY();  
            rawx = (int) event.getRawX();  
            rawy = (int) event.getRawY();  
            Log.d("DEBUG", "getX=" + x + "getY=" + y + "\n" + "getRawX=" + rawx  
                    + "getRawY=" + rawy + "\n");  
            break;  
  
        case MotionEvent.ACTION_UP:  
  
            break;  
        }  
        return false;  
    }  
}  

结论: getX()是表示Widget相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕),getY(),getRawY()一样的道理




你可能感兴趣的:(android,坐标,widget)