如何找到用户触摸的坐标?

问题描述

我想获取用户触摸的轨迹/路径的坐标。我将它们存储到 ArrayList 并将它们存储在数据库中。我使用 getX()getY() 方法获取相关坐标,但我只得到 one 组值。这是我实现相同的代码

public boolean dispatchTouchEvent(MotionEvent event) {


        TouchData touchData = new TouchData();
        int index = event.getActionIndex();
        int pointerId = event.getPointerId(index);
        ArrayList<Integer> trajX = new ArrayList<Integer>();
        ArrayList<Integer> trajY = new ArrayList<Integer>();


        int X = (int) event.getX();
        int Y = (int) event.getY();
        trajX.add(X);
        trajY.add(Y);
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                X = (int) event.getX();
                Y = (int) event.getY();
                trajX.add(X);
                trajY.add(Y);
                if(mVeLocityTracker == null) {
                    // Retrieve a new VeLocityTracker object to watch the veLocity of a motion.
                    mVeLocityTracker = VeLocityTracker.obtain();
                }
                else {
                    // Reset the veLocity tracker back to its initial state.
                    mVeLocityTracker.clear();
                }
                // Add a user's movement to the tracker.
                mVeLocityTracker.addMovement(event);
                break;

            case MotionEvent.ACTION_UP:


                float centreX = button.getX() + button.getWidth()  / 2;
                float centreY = button.getY() + button.getHeight() / 2;

            

                X = (int) event.getX();
                Y = (int) event.getY();
                trajX.add(X);
                trajY.add(Y);

                long eventDuration = event.getEventTime() - event.getDownTime();

                DatabaseReference newRef = FirebaseDatabase.getInstance("https://pragya-datacollector.firebaseio.com").getReference();
                touchData.setUniqueID(currentUserID);
                touchData.setTaskName(TASK_NAME);
                touchData.setTouchDuration(eventDuration);
                touchData.setTrajectoryX(trajX);
                touchData.setTrajectoryY(trajY);
                touchData.setVeLocityX(veLocityX);
                touchData.setVeLocityY(veLocityY);
                touchData.setTargetX(centreX);
                touchData.setTargetY(centreY);
                newRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        databaseReference.push().setValue(touchData);

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
                break;
            case MotionEvent.ACTION_MOVE:
                mVeLocityTracker.addMovement(event);
                mVeLocityTracker.computeCurrentVeLocity(1);
                veLocityX = mVeLocityTracker.getXVeLocity();
                veLocityY = mVeLocityTracker.getYVeLocity();
                X = (int) event.getX();
                Y = (int) event.getY();
                trajX.add(X);
                trajY.add(Y);

                break;

            case MotionEvent.ACTION_CANCEL:
                mVeLocityTracker.recycle();
                break;
        }
        

return super.dispatchTouchEvent(event);
    }

这是 TouchData 类:

public class TouchData {
    private String taskName;

    private long touchDuration;

    private List<Integer> trajectoryX;

    private List<Integer> trajectoryY;

    private float veLocityX;

    private float veLocityY;

    private String uniqueID;

    private float targetX;

    private float targetY;


    public TouchData() {

    }


    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public List<Integer> getTrajectoryX() {
        return trajectoryX;
    }

    public void setTrajectoryX(List<Integer> trajectoryX) {
        this.trajectoryX = trajectoryX;
    }

    public List<Integer> getTrajectoryY() {
        return trajectoryY;
    }

    public void setTrajectoryY(List<Integer> trajectoryY) {
        this.trajectoryY = trajectoryY;
    }

    public long getTouchDuration() {
        return touchDuration;
    }

    public void setTouchDuration(long touchDuration) {
        this.touchDuration = touchDuration;
    }

    public float getVeLocityX() {
        return veLocityX;
    }

    public void setVeLocityX(float veLocityX) {
        this.veLocityX = veLocityX;
    }

    public float getVeLocityY() {
        return veLocityY;
    }

    public void setVeLocityY(float veLocityY) {
        this.veLocityY = veLocityY;
    }

    public String getUniqueID()
    {
        return uniqueID;
    }

    public void setUniqueID(String uniqueID) {
        this.uniqueID = uniqueID;
    }

    public float getTargetX() {
        return targetX;
    }

    public void setTargetX(float targetX) {
        this.targetX = targetX;
    }

    public float getTargetY() {
        return targetY;
    }

    public void setTargetY(float targetY) {
        this.targetY = targetY;
    }
}

我浏览了几乎所有相关的问题/答案,其中大多数使用相同的功能,但对我来说,它只给出了一个值。这是这样做的正确方法吗?这个实现到底有什么问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...