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

时间:2021-07-21 17:25:59

标签: android arraylist touch touch-event motionevent

我想获取用户触摸的轨迹/路径的坐标。我将它们存储到 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;
    }
}

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

0 个答案:

没有答案