如何检测视图之间的滑动以在android java中创建路径?
Posted
技术标签:
【中文标题】如何检测视图之间的滑动以在android java中创建路径?【英文标题】:How to detect swipe between views to make path across them in android java? 【发布时间】:2020-06-13 08:04:19 【问题描述】:我正在尝试制作带有字母按钮的游戏,如果用户在它们上滑动,那么它应该检测视图并在它们之间划一条线。我搜索了许多教程、示例和问题,但无法理解。我附上了图片以帮助理解这个问题。
代码如下:
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_
android:layout_
tools:context=".MainActivity">
<TextView
android:id="@+id/txtresult"
android:layout_
android:layout_
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_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="A" />
<TextView
android:id="@+id/btn2"
android:layout_
android:layout_
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"
android:text="B" />
</RelativeLayout>
请帮忙。提前致谢。
【问题讨论】:
您是否尝试过创建一个包含四个TextViews
的RelativeLayout
,然后将onTouchListener
设置为RelativeLayout
?我在回答中详细说明了
你的回答不完整且肤浅。
能否详细说明或编辑一下?
【参考方案1】:
我建议制作一个包含四个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
);
【讨论】:
以上是关于如何检测视图之间的滑动以在android java中创建路径?的主要内容,如果未能解决你的问题,请参考以下文章
检测用户何时向左或向右滑动以在 WKWebView 上前后移动