Android recording coordinates of the point in screen

纵饮孤独 提交于 2019-12-08 09:37:02

问题


My activity should record the coordinates of a point on the screen, which the user can move. Specifically, the user touches the screen and appears on the item, which will be able to move. When you lift your finger, the coordinates of the point should be recorded.Please for your advice.


回答1:


EDITED Here is whole simple activity which Toasts the user touch down and touch up co-ordinates

import android.os.Bundle;

import android.app.Activity;

import android.view.MotionEvent;

import android.widget.Toast;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    float x = event.getX();
    float y = event.getY();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Toast.makeText(getApplicationContext(), "Touch Down x="+x+"  y="+y ,Toast.LENGTH_SHORT).show();
            //Handle Touch Down
            break;
        case MotionEvent.ACTION_MOVE:

            //Handle Touch Move
            break;
        case MotionEvent.ACTION_UP:
            Toast.makeText(getApplicationContext(), "Touch Up x="+x+"  y="+y ,Toast.LENGTH_SHORT).show();
            //Handle Touch Up
            break;
    }
    return false;
}

}



来源:https://stackoverflow.com/questions/17922650/android-recording-coordinates-of-the-point-in-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!