Android開發(fā)中MotionEvent坐標(biāo)獲取方法分析
本文實(shí)例講述了Android開發(fā)中MotionEvent坐標(biāo)獲取方法。分享給大家供大家參考,具體如下:
Android MotionEvent中g(shù)etX()與getRawX()都是獲取屏幕坐標(biāo)(橫),但二者又有區(qū)別
getX() : 是獲取相對(duì)當(dāng)前控件(View)的坐標(biāo)
getRawX() : 是獲取相對(duì)顯示屏幕左上角的坐標(biāo)
演示示例代碼
Java代碼:
public class MainActivity extends Activity implements OnTouchListener {
private Button btn;
private int x = 0, y = 0;
private int rawX = 0, rawY = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn.setOnTouchListener(this);
}
@Override
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.e("homer", "x = " + x + "; y = " + y + "; rawX = " + rawX + "; rawY = " + rawY);
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
}
xml 代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt"
android:layout_centerInParent="true"
android:text="button me" />
</RelativeLayout>
運(yùn)行結(jié)果:

點(diǎn)擊屏幕中間的Button,獲取的坐標(biāo)信息:

結(jié)果說明:
x,y : 分別獲取的相對(duì)Button控件的坐標(biāo) getX(), getY()
rawX,rawY : 分別獲取的相對(duì)顯示屏幕左上角的坐標(biāo) getRawX(), getRawY()
總結(jié):
getX() 是表示W(wǎng)idget相對(duì)于自身左上角的x坐標(biāo),而getRawX()是表示相對(duì)于屏幕左上角的x坐標(biāo)值(注意:這個(gè)屏幕左上角是手機(jī)屏幕左上角,不管activity是否有titleBar或是否全屏幕); getY(),getRawY()一樣的道理
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android手機(jī)信息操作技巧匯總》、《Android資源操作技巧匯總》及《Android基本組件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android如何基于坐標(biāo)對(duì)View進(jìn)行模擬點(diǎn)擊事件詳解
- Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析
- Android中GPS坐標(biāo)轉(zhuǎn)換為高德地圖坐標(biāo)詳解
- ANDROID中使用VIEWFLIPPER類實(shí)現(xiàn)屏幕切換(關(guān)于坐標(biāo)軸的問題已補(bǔ)充更改)
- Android應(yīng)用 坐標(biāo)系詳細(xì)介紹
- 深入淺析Android坐標(biāo)系統(tǒng)
- android獲得當(dāng)前view在屏幕中坐標(biāo)的方法
- Android中如何指定SnackBar在屏幕的位置及小問題解決
- Android仿淘寶view滑動(dòng)至屏幕頂部會(huì)一直停留在頂部的位置
- Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
相關(guān)文章
Android仿抖音右滑清屏左滑列表功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android仿抖音右滑清屏左滑列表功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
Android實(shí)現(xiàn)本地Service方法控制音樂播放
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)本地Service方法控制音樂播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
Android開發(fā)中button按鈕的使用及動(dòng)態(tài)添加組件方法示例
這篇文章主要介紹了Android開發(fā)中button按鈕的使用及動(dòng)態(tài)添加組件方法,涉及Android針對(duì)button按鈕的事件響應(yīng)及TextView動(dòng)態(tài)添加相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
android實(shí)現(xiàn)icon動(dòng)態(tài)旋轉(zhuǎn)效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)icon動(dòng)態(tài)旋轉(zhuǎn)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android自定義View繪制隨機(jī)生成圖片驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android實(shí)現(xiàn)使用微信登錄第三方APP的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用微信登錄第三方APP的方法,結(jié)合實(shí)例形式分析了Android微信登錄APP的操作步驟與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11

