我是Android新手,我一直試圖找出如何檢索屏幕上連續觸摸的座標。例如有2個變量(x,y),隨着手指移動而實時更新。我知道如何在觸摸時找到它,但我真的不知道如何在手指移動後讓它返回結果。Android(x,y)連續觸摸(拖動)座標
我一直在嘗試switch語句,而/ for循環與ACTION_MOVE./ UP/DOWN ..。不再組合。
我在網站上發現了類似的問題,但答案僅適用於第一步(僅顯示觸摸協調) 我真的很感激這個解決方案!謝謝!
我是Android新手,我一直試圖找出如何檢索屏幕上連續觸摸的座標。例如有2個變量(x,y),隨着手指移動而實時更新。我知道如何在觸摸時找到它,但我真的不知道如何在手指移動後讓它返回結果。Android(x,y)連續觸摸(拖動)座標
我一直在嘗試switch語句,而/ for循環與ACTION_MOVE./ UP/DOWN ..。不再組合。
我在網站上發現了類似的問題,但答案僅適用於第一步(僅顯示觸摸協調) 我真的很感激這個解決方案!謝謝!
沒有看到你的代碼,我只是猜測,但本質上,如果你不回true
到第一次調用onTouchEvent
,你不會看到任何後續事件的手勢(MOVE,UP等) 。
也許這是你的問題?否則,請將代碼示例提交。
您需要實現一個OnTouchListener
爲您想要識別拖動的任何視圖。
然後在OnTouchListener
中,您需要顯示X和Y座標。我相信你可以通過MotionEvent.getRawX()
和MotionEvent.getRawY()
您可以使用MotionEvent.getAction()
方法找出拖動的發生時間。我相信這個常數是MotionEvent.ACTION_MOVE
。下面是一些僞代碼:
添加OnTouchListener接口
public class XYZ extends Activity implements OnTouchListener
註冊聽衆在onCreate方法
public void onCreate(Bundle savedInstanceState)
{
//other code
View onTouchView = findViewById(R.id.whatever_id);
onTouchView.setOnTouchListener(this);
}
落實onTouch方法
public boolean onTouch(View view, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_MOVE)
{
float x = event.getRawX();
float y = event.getRawY();
// Code to display x and y go here
// you can print the x and y coordinate in a textView for exemple
}
}
非常感謝:)雖然這是正確的,但我遇到的問題是返回語句錯誤,因此Action_Move無法更改返回的內容。 ! – user1422535
如何獲得所有運動點? –
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView xCoord = (TextView) findViewById(R.id.textView1);
final TextView yCoord = (TextView) findViewById(R.id.textView2);
final View touchView = findViewById(R.id.textView3);
touchView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
xCoord.setText(String.valueOf((int) event.getX()));
yCoord.setText(String.valueOf((int) event.getY()));
break;
}
case MotionEvent.ACTION_MOVE:{
xCoord.setText(String.valueOf((int) event.getX()));
yCoord.setText(String.valueOf((int) event.getY()));
break;
}
}
return true;
}
});
}
謝謝很多蒂姆。這真的很有道理(愚蠢的錯誤)。現在正在工作。這裏還有一些代碼會再次遇到相同的錯誤:) – user1422535