gpt4 book ai didi

android - android中的矩形触摸

转载 作者:行者123 更新时间:2023-11-30 00:52:01 26 4
gpt4 key购买 nike

我创建了一个名为 Rectangle 的新类,它包含以下代码:

package com.example.komeil.speedcolor;

/**
* Created by Komeil on 20/11/2016.
*/

public class Rectangle extends Shape {
private int left;
private int top;
private int right;
private int bottom;
public Rectangle(String name,int color,int left,int top,int right,int bottom)
{
super(name,color);
setLeft(left);
setTop(top);
setRight(right);
setBottom(bottom);
}


public void setLeft(int left)
{
this.left = left;
}

public int getLeft(){
return left;
}

public int getTop()
{
return top;
}

public void setTop(int top){
this.top = top;
}

public void setRight(int right){
this.right = right;
}

public int getRight(){
return right;
}

public void setBottom(int bottom){
this.bottom = bottom;
}

public int getBottom(){
return bottom;
}
}

我想从中创建对象并用 canvas.drawRect() 绘制它,就像代码一样:

canvas.drawRect(rectangle.getLeft(),rectangle.getTop(),rectangle.getRight(),rectangle.getBottom(),paint);

但是当我使用 blow 代码来检测矩形对象上的触摸事件时,它不起作用:

@Override
public boolean onTouchEvent(MotionEvent event) {
return gameThread.doOnTouch(event);
}


public boolean doOnTouch(MotionEvent event){

synchronized (event){

int eventAction = event.getAction();
int xEvent = (int)event.getX();
int yEvent = (int)event.getY();

switch (eventAction){

case MotionEvent.ACTION_DOWN:

if(xEvent >= rectangle.getLeft() && xEvent <= rectangle.getRight()
&& yEvent >= rectangle.getBottom() && yEvent<= rectangle.getTop())
{
touched = true;
score +=5;
}
}
break;
case MotionEvent.ACTION_UP:
touched = false;
break;
}
return true;
}
}

那么如何检测矩形触摸呢?

最佳答案

将 yourCustomView 更改为您的 viewName。

请相应更改

yourCustomView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){

int x = event.getX();
int y = event.getY();
if(x > rectLeftX && x < rectRightX && y > rectBottomY && y < rectTopY){
/* Trigger your action here */
}

}
return true;
}
});

检查这个,你会得到一些提示Detect touch

关于android - android中的矩形触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40884844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com