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

时间:2021-07-20 09:23:18

标签: android firebase touch touch-event motionevent

我正在制作一个应用程序,用于将滑动轨迹、触摸持续时间等数据存储到 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 值并将其存储在数据库中。但是在这个实现中发生的事情是对于触摸事件,只有一个单个值被存储在列表中。为什么会发生这种情况?

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

0 个答案:

没有答案