国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android獲取點擊屏幕的位置坐標(biāo)

2019-12-12 00:51:24
字體:
供稿:網(wǎng)友

在Android開發(fā)過程中,有時需要獲取觸摸位置的坐標(biāo),以便作進一步處理,比如做炫酷的動畫效果,或者響應(yīng)其他操作。

本文簡單介紹Android中觸屏操作時,觸屏的開始位置、當(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()) {    /**    * 點擊的開始位置    */    case MotionEvent.ACTION_DOWN:      tvTouchShowStart.setText("起始位置:(" + event.getX() + "," + event.getY());      break;    /**    * 觸屏實時位置    */    case MotionEvent.ACTION_MOVE:      tvTouchShow.setText("實時位置:(" + 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,只能顯示起始位置,不能顯示實時位置和結(jié)束位置    */    return true;  }}

效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 泽州县| 吴堡县| 嘉禾县| 和政县| 安义县| 贵阳市| 南江县| 历史| 哈尔滨市| 西林县| 邻水| 万年县| 东平县| 红桥区| 嘉峪关市| 济南市| 西宁市| 老河口市| 江北区| 朝阳县| 白玉县| 赤峰市| 天等县| 彰化县| 湘阴县| 佛坪县| 汪清县| 贵溪市| 双桥区| 望城县| 灌阳县| 筠连县| 阳信县| 敖汉旗| 东乡族自治县| 静乐县| 商丘市| 健康| 蒙阴县| 岑溪市| 精河县|