如何在Android Java中检测视图之间的滑动以在它们之间建立路径? [英] How to detect swipe between views to make path across them in android java?

查看:154
本文介绍了如何在Android Java中检测视图之间的滑动以在它们之间建立路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作带有字母按钮的游戏,如果用户在其上滑动,则它应该检测视图并在它们之间划一条线.我搜索了许多教程,示例和问题,但无法理解.我已附上图片以帮助理解该问题.

以下是代码:

MainActivity.java

package com.example.detecttouch;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView btn1, btn2;
    TextView txtresult;
    Rect outRect = new Rect();
    int[] location = new int[2];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn1 = findViewById(R.id.btn1);
        btn2 =  findViewById(R.id.btn2);
        txtresult = (TextView) findViewById(R.id.txtresult);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {}
        });

        btn1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int)event.getRawX();
                int y = (int)event.getRawY();
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if(isViewInBounds(btn2, x, y))
                        btn2.dispatchTouchEvent(event);
                    else if(isViewInBounds(btn1, x, y)){
                        Log.d("Panauti", "onTouch ViewA");
                        //Here goes code to execute on onTouch ViewA
                    }
                }
                // Further touch is not handled
                return false;
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {}
        });

        btn2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int)event.getRawX();
                int y = (int)event.getRawY();
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if(isViewInBounds(btn1, x, y))
                        btn1.dispatchTouchEvent(event);
                    else if(isViewInBounds(btn2, x, y)){
                        Log.d("Panauti", "onTouch ViewB");
                        //Here goes code to execute on onTouch ViewB
                    }
                }
                // Further touch is not handled
                return false;
            }
        });
    }

    private boolean isViewInBounds(View view, int x, int y){
        view.getDrawingRect(outRect);
        view.getLocationOnScreen(location);
        outRect.offset(location[0], location[1]);
        return outRect.contains(x, y);
    }

}

和activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="102dp"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="A" />

    <TextView
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="160dp"
        android:text="B" />


</RelativeLayout>

请帮助.预先感谢.

我建议制作一个包含四个TextViews的布局,然后在包含四个TextViews的布局中设置一个onTouchListener.这样,只要触摸布局,它就可以画一条线,而不是一个特定的按钮,并且可以在视图之间迁移:

private int startX, startY, endX, endY; // fields to determine the coordinates of the Rect
layout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startX = event.getRawX();
            startY = event.getRawY();
        } else {
            endX = event.getRawX();
            endY = event.getRawY();
            // set line bounds
        }
    }
});

I am trying to make game for which there are buttons with alphabets and if user swipes over them then it should detect the views as well as make a line across them. I searched many tutorials, examples and questions but unable to get the idea. I have attached image to help understand the question.

Here are the codes:

MainActivity.java

package com.example.detecttouch;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView btn1, btn2;
    TextView txtresult;
    Rect outRect = new Rect();
    int[] location = new int[2];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btn1 = findViewById(R.id.btn1);
        btn2 =  findViewById(R.id.btn2);
        txtresult = (TextView) findViewById(R.id.txtresult);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {}
        });

        btn1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int)event.getRawX();
                int y = (int)event.getRawY();
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if(isViewInBounds(btn2, x, y))
                        btn2.dispatchTouchEvent(event);
                    else if(isViewInBounds(btn1, x, y)){
                        Log.d("Panauti", "onTouch ViewA");
                        //Here goes code to execute on onTouch ViewA
                    }
                }
                // Further touch is not handled
                return false;
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {}
        });

        btn2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int)event.getRawX();
                int y = (int)event.getRawY();
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if(isViewInBounds(btn1, x, y))
                        btn1.dispatchTouchEvent(event);
                    else if(isViewInBounds(btn2, x, y)){
                        Log.d("Panauti", "onTouch ViewB");
                        //Here goes code to execute on onTouch ViewB
                    }
                }
                // Further touch is not handled
                return false;
            }
        });
    }

    private boolean isViewInBounds(View view, int x, int y){
        view.getDrawingRect(outRect);
        view.getLocationOnScreen(location);
        outRect.offset(location[0], location[1]);
        return outRect.contains(x, y);
    }

}

and activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txtresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="102dp"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp"
        android:text="A" />

    <TextView
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="160dp"
        android:text="B" />


</RelativeLayout>

Please Help. Thanks in advance.

解决方案

I suggest making a layout that contains the four TextViews and then setting up an onTouchListener to the layout containing them. That way, it can draw a line whenever the layout is touched rather than a specific button and can migrate between views:

private int startX, startY, endX, endY; // fields to determine the coordinates of the Rect
layout.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startX = event.getRawX();
            startY = event.getRawY();
        } else {
            endX = event.getRawX();
            endY = event.getRawY();
            // set line bounds
        }
    }
});

这篇关于如何在Android Java中检测视图之间的滑动以在它们之间建立路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆