簡體   English   中英

如何檢測視圖之間的滑動以在android java中穿過它們?

[英]How to detect swipe between views to make path across them in android java?

我正在嘗試制作帶有字母按鈕的游戲,如果用戶在它們上面滑動,那么它應該檢測視圖並在它們之間划一條線。 我搜索了許多教程、示例和問題,但無法理解。 我附上了圖片以幫助理解這個問題。

在此處輸入圖片說明

以下是代碼:

主活動.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的布局,然后為包含它們的布局設置一個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
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM