View提供的獲取座標方法
getTop() 獲取View自身的頂邊到父佈局頂邊的距離
getLeft() 獲取View自身的左邊到父佈局左邊的距離
getRight() 獲取View自身的右邊到父佈局左邊的距離
getBottom() 獲取View自身的底邊到父佈局頂邊的距離
示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:id="@+id/rl"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:background="@color/green"
>
<TextView
android:text="A"
android:gravity="center"
android:id="@+id/tv"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="100dp"
android:background="#ff0000"
/>
</RelativeLayout>
</LinearLayout>
Activity中取數據
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
mTv.getLeft();
mTv.getRight();
mTv.getTop();
mTv.getBottom();
Log.e(TAG, "initView: Left " + mTv.getLeft() + " px");
Log.e(TAG, "initView: Right " + mTv.getRight() + " px");
Log.e(TAG, "initView: Top " + mTv.getTop() + " px");
Log.e(TAG, "initView: Bottom " + mTv.getBottom() + " px");
Log.e(TAG, "initView: 轉換爲dp Left " + px2dp(mTv.getLeft()) + " dp");
Log.e(TAG, "initView: 轉換爲dp Right " + px2dp(mTv.getRight()) + " dp");
Log.e(TAG, "initView: 轉換爲dp Top " + px2dp(mTv.getTop()) + " dp");
Log.e(TAG, "initView: 轉換爲dp Bottom " + px2dp(mTv.getBottom()) + " dp");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
如圖
MotionEvent
在手指觸摸屏幕的過程中,典型的事件類型有以下幾種
ACTION_DOWN 手指接觸屏幕
ACTION_MOVE 手指在屏幕上移動
ACTION_UP 手機從屏幕上鬆開的瞬間
getX() 獲取點擊處View距離當前控件左邊的距離
getY() 獲取點擊處View距離當前控件頂邊的距離
getRawX() 獲取點擊處View距離整個屏幕左邊的距離
getRawY() 獲取點擊處View距離整個屏幕頂邊的距離
佈局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@color/green"
>
<TextView
android:text="B"
android:gravity="center"
android:id="@+id/tv_yellow"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#FFFF00"
/>
</RelativeLayout>
Acitivity中
mTvYellow.setOnTouchListener(mOnTouchListener);
private int lastX;
private int lastY;
private int startLeft;
private int startRight;
private int startTop;
private int startBottom;
View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
//獲取手機觸摸的座標
int x = (int) event.getX();
int y = (int) event.getY();
int rawx = (int) event.getRawX();
int rawy = (int) event.getRawY();
Log.e(TAG, "onTouch: _x: "+x +" ___ y: "+y);
Log.e(TAG, "onTouch: rawx: "+rawx +" ___ rawy: "+rawy);
switch (action){
case MotionEvent.ACTION_DOWN://按下,獲取View初始的位置
startLeft = v.getLeft();
startRight = v.getRight();
startTop = v.getTop();
startBottom = v.getBottom();
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE://移動,View跟隨手指的移動
int offsetX = x - lastX;
int offsetY = y - lastY;
v.layout(v.getLeft()+offsetX,v.getTop()+offsetY,
v.getRight()+offsetX,v.getBottom()+offsetY);
break;
case MotionEvent.ACTION_UP://當手指擡起時,回到View初始的位置
v.layout(startLeft, startTop, startRight, startBottom);
break;
}
return true;
}
};
即getX/getY返回的是相對於當前View左上角的x和y座標,
而getRawX/getRawY返回的是相對於手機屏幕左上角的x和y座標.