欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android獲取手指觸摸位置的方法

 更新時間:2018年05月26日 08:58:53   作者:王伴農(nóng)  
這篇文章主要為大家詳細介紹了android獲取手指觸摸位置,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了android獲取手指觸摸位置的具體代碼,供大家參考,具體內(nèi)容如下

手機屏幕事件的處理方法onTouchEvent。該方法在View類中的定義,并且所有的View子類全部重寫了該方法,應(yīng)用程序可以通過該方法處理手機屏幕的觸摸事件。

其原型是:

public boolean onTouchEvent(MotionEvent event)

參數(shù)event:參數(shù)event為手機屏幕觸摸事件封裝類的對象,其中封裝了該事件的所有信息,例如觸摸的位置、觸摸的類型以及觸摸的時間等。該對象會在用戶觸摸手機屏幕時被創(chuàng)建。

返回值:該方法的返回值機理與鍵盤響應(yīng)事件的相同,同樣是當(dāng)已經(jīng)完整地處理了該事件且不希望其他回調(diào)方法再次處理時返回true,否則返回false。

該方法并不像之前介紹過的方法只處理一種事件,一般情況下以下三種情況的事件全部由onTouchEvent方法處理,只是三種情況中的動作值不同。

屏幕被按下:當(dāng)屏幕被按下時,會自動調(diào)用該方法來處理事件,此時MotionEvent.getAction()的值為MotionEvent.ACTION_DOWN,如果在應(yīng)用程序中需要處理屏幕被按下的事件,只需重新該回調(diào)方法,然后在方法中進行動作的判斷即可。

屏幕被抬起:當(dāng)觸控筆離開屏幕時觸發(fā)的事件,該事件同樣需要onTouchEvent方法來捕捉,然后在方法中進行動作判斷。當(dāng)MotionEvent.getAction()的值為MotionEvent.ACTION_UP時,表示是屏幕被抬起的事件。

在屏幕中拖動:該方法還負責(zé)處理觸控筆在屏幕上滑動的事件,同樣是調(diào)用MotionEvent.getAction()方法來判斷動作值是否為MotionEvent.ACTION_MOVE再進行處理。
示例代碼如下:

MainActivity.java

package com.example.touchpostionshow; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MotionEvent; 
import android.widget.EditText; 
 
public class MainActivity extends Activity { 
 public EditText pox,poY,condition; 
  
  
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
   pox = (EditText)findViewById(R.id.editText1); 
   poY = (EditText)findViewById(R.id.editText2); 
   condition = (EditText)findViewById(R.id.editText3); 
   
   
 } 
 
 @Override 
 public boolean onCreateOptionsMenu(Menu menu) { 
  // Inflate the menu; this adds items to the action bar if it is present. 
  getMenuInflater().inflate(R.menu.main, menu); 
  return true; 
 } 
 @Override 
 public boolean onTouchEvent(MotionEvent event) 
 { 
  float x = event.getX(); 
  float y = event.getY(); 
  try 
  { 
   switch(event.getAction()) 
   { 
    case MotionEvent.ACTION_DOWN: pox.setText(""+x);poY.setText(""+y);condition.setText("down");break; 
    case MotionEvent.ACTION_UP:pox.setText(""+x);poY.setText(""+y);condition.setText("up");break; 
    case MotionEvent.ACTION_MOVE:pox.setText(""+x);poY.setText(""+y);condition.setText("move");break; 
   } 
   return true; 
  } 
  catch(Exception e) 
  { 
   Log.v("touch", e.toString()); 
   return false; 
  } 
 } 
 
} 

XML文件中添加三個編輯文本框分別用來顯示坐標(biāo)的X Y以及手指是按下 抬起還是處于移動。

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

相關(guān)文章

最新評論