我正在尝试制作带有字母按钮的游戏,如果用户在其上滑动,则它应该检测视图并在它们之间划一条线。我搜索了许多教程,示例和问题,但无法理解。我已附上图片以帮助理解该问题。
这里是代码:
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>
请帮助。预先感谢。
我建议在包含按钮的布局中设置一个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
}
}
});