Android獲取點(diǎn)擊屏幕的位置坐標(biāo)
在Android開發(fā)過程中,有時(shí)需要獲取觸摸位置的坐標(biāo),以便作進(jìn)一步處理,比如做炫酷的動(dòng)畫效果,或者響應(yīng)其他操作。
本文簡(jiǎn)單介紹Android中觸屏操作時(shí),觸屏的開始位置、當(dāng)前位置、結(jié)束位置。
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.com.TouchTest" >
<LinearLayout
android:id="@+id/ll_touch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/touch_show_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/touch_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
</RelativeLayout>
Activity中的操作:
public class TouchTest extends Activity implements OnTouchListener {
private TextView tvTouchShowStart;
private TextView tvTouchShow;
private LinearLayout llTouch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_touch_test);
init();
}
private void init() {
tvTouchShowStart = (TextView) findViewById(R.id.touch_show_start);
tvTouchShow = (TextView) findViewById(R.id.touch_show);
llTouch = (LinearLayout) findViewById(R.id.ll_touch);
llTouch.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
/**
* 點(diǎn)擊的開始位置
*/
case MotionEvent.ACTION_DOWN:
tvTouchShowStart.setText("起始位置:(" + event.getX() + "," + event.getY());
break;
/**
* 觸屏實(shí)時(shí)位置
*/
case MotionEvent.ACTION_MOVE:
tvTouchShow.setText("實(shí)時(shí)位置:(" + event.getX() + "," + event.getY());
break;
/**
* 離開屏幕的位置
*/
case MotionEvent.ACTION_UP:
tvTouchShow.setText("結(jié)束位置:(" + event.getX() + "," + event.getY());
break;
default:
break;
}
/**
* 注意返回值
* true:view繼續(xù)響應(yīng)Touch操作;
* false:view不再響應(yīng)Touch操作,故此處若為false,只能顯示起始位置,不能顯示實(shí)時(shí)位置和結(jié)束位置
*/
return true;
}
}
效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析
OkHttp(GitHub主頁https://github.com/square/okhttp)是一款高人氣的第三方Android網(wǎng)絡(luò)編程包,這里我們來看一下Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析:2016-07-07
Android仿iOS實(shí)現(xiàn)側(cè)滑返回功能(類似微信)
這篇文章主要為大家詳細(xì)介紹了Android仿iOS實(shí)現(xiàn)側(cè)滑返回功能,類似微信功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android自定義View實(shí)現(xiàn)課程表表格
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)課程表表格,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Android設(shè)計(jì)模式之Builder模式詳解
這篇文章主要為大家詳細(xì)介紹了Android設(shè)計(jì)模式之Builder模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android Zipalign工具優(yōu)化Android APK應(yīng)用
本文主要介紹Android Zipalign工具優(yōu)化Android APK應(yīng)用,這里整理了相關(guān)資料及簡(jiǎn)單優(yōu)化實(shí)例,有需要的小伙伴可以參考下2016-09-09
Android編程開發(fā)之NotiFication用法詳解
這篇文章主要介紹了Android編程開發(fā)之NotiFication用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了NotiFication的功能、使用技巧與注意事項(xiàng),需要的朋友可以參考下2015-12-12

