getX()是表示Widget相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕),getY(),getRawY()一样的道理
1.如果你的某个Activity中实现OnTouchListener接口,需要重写
onTouch(View view,MotionEvent event)
这个方法,
getRawX()和getRawY()获得的是相对屏幕的位置,getX()和getY()获得的永远是view的触摸位置坐标
(这两个值不会超过view的长度和宽度)。
2.如果重写Activity的
onTouchEvent(MotionEvent event)
,这个时候获得的是屏幕点击位置的坐标。event.getX() 与 event.getRawX()获取的值是一样的,event.getY() 与 event.getRawY()获取的值是一样,还能获取触摸标题栏时的坐标位置,但是触摸状态栏时的坐标位置不能获得。
(注:Activity上有两段宽度:一个是状态栏,一个是标题栏,每一个宽度都占了25像素)
总结:
getX()是表示view相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕)
2.代码
public class MainActivity extends AppCompatActivity { private TextView tv_one; private float downX; private float downY; private float downRawX; private float downRawY; private float moveX; private float moveY; private float moveRawX; private float moveRawY; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_one = (TextView) findViewById(R.id.tv_one); tv_one.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { switch (motionEvent.getAction()){ case MotionEvent.ACTION_DOWN: downX = motionEvent.getX(); downY = motionEvent.getY(); downRawX = motionEvent.getRawX(); downRawY = motionEvent.getRawY(); System.out.println("xcq downX "+downX); System.out.println("xcq downY "+downY); System.out.println("xcq downRawX "+downRawX); System.out.println("xcq downRawY "+downRawY); break; case MotionEvent.ACTION_MOVE: moveX = motionEvent.getX(); moveY = motionEvent.getY(); moveRawX = motionEvent.getRawX(); moveRawY = motionEvent.getRawY(); System.out.println("xcq moveX "+moveX); System.out.println("xcq moveY "+moveY); System.out.println("xcq moveRawX "+moveRawX); System.out.println("xcq moveRawY "+moveRawY); if(Math.abs(downX-moveX)>Math.abs(downY-moveY)){ //如果水平位移大于竖直位移//那么listview就不处理时间,让自定义sweepView去做 return false; } break; case MotionEvent.ACTION_UP: break; } return true; } }); } // xcq downX 22.0 // xcq downY 26.0 // xcq downRawX 182.0 // xcq downRawY 547.0 // xcq moveX 20.986893 // xcq moveY 26.0 // xcq moveRawX 180.9869 // xcq moveRawY 547.0 // xcq moveX 19.905106 // xcq moveY 26.0 // xcq moveRawX 179.9051 // xcq moveRawY 547.0
3.图片