如何获得用户触摸的“所有”坐标?

问题描述

我正在制作一个应用程序,用于将滑动轨迹、触摸持续时间等数据存储到 Firebase。应为每个触摸会话存储此数据。但是我在获取用户触摸的所有坐标时遇到了问题。为了实现这一点,我将 dispatchTouchEvent() 类和其他函数用于 motionEvent,如下所示:

public boolean dispatchTouchEvent(MotionEvent event) {


    TouchData touchData = new TouchData();
    int index = event.getActionIndex();
    int pointerId = event.getPointerId(index);
    List<Float> xTraj = new ArrayList<Float>();
    List<Float> yTraj = new ArrayList<Float>();


    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    xTraj.add(event.getX());
    yTraj.add(event.getY());
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:

            xTraj.add(event.getX());
            yTraj.add(event.getY());
            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;

            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.setxTrajectory(xTraj);
            touchData.setyTrajectory(yTraj);
            touchData.setxVeLocity(xVeLocity);
            touchData.setyVeLocity(yVeLocity);
            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);
            xVeLocity = mVeLocityTracker.getXVeLocity();
            yVeLocity = mVeLocityTracker.getYVeLocity();
            xTraj.add(event.getX());
            yTraj.add(event.getY());

            break;

        case MotionEvent.ACTION_CANCEL:
            mVeLocityTracker.recycle();
            break;
    }
    return super.dispatchTouchEvent(event);
}

其中 TouchData 如下:

public class TouchData {
    private String taskName;

    private long touchDuration;

    private List<Float> xTrajectory;

    private List<Float> yTrajectory;

    private float xVeLocity;

    private float yVeLocity;

    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<Float> getxTrajectory() {
        return xTrajectory;
    }

    public void setxTrajectory(List<Float> touchTrajectory) {
        this.xTrajectory = xTrajectory;
    }

    public List<Float> getyTrajectory() {
        return yTrajectory;
    }

    public void setyTrajectory(List<Float> touchTrajectory) {
        this.yTrajectory = yTrajectory;
    }

    public long getTouchDuration() {
        return touchDuration;
    }

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

    public float getxVeLocity() {
        return xVeLocity;
    }

    public void setxVeLocity(float xVeLocity) {
        this.xVeLocity = xVeLocity;
    }

    public float getyVeLocity() {
        return yVeLocity;
    }

    public void setyVeLocity(float yVeLocity) {
        this.yVeLocity = yVeLocity;
    }

    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;
    }
}

一旦用户触摸屏幕,我就需要一组新的 TouchData 值并将其存储在数据库中。但是在这个实现中发生的事情是对于触摸事件,只有一个单个值被存储在列表中。为什么会发生这种情况?

这种存储用户触摸轨迹的实现到底有什么问题?

解决方法

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

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

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

相关问答

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