gpt4 book ai didi

java - onSingleTapConfirmed 未给出准确的 y 坐标

转载 作者:行者123 更新时间:2023-12-01 10:02:55 25 4
gpt4 key购买 nike

MainActivity类使用GestureDetector获取坐标的代码:

public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener {

DrawView drawView;
private GestureDetectorCompat g1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.TRANSPARENT);
setContentView(drawView);
g1 = new GestureDetectorCompat(this,this);
g1.setOnDoubleTapListener(this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
g1.onTouchEvent(event);
return super.onTouchEvent(event);
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
drawView.setXandY(e.getX(), e.getY());
Log.v("id2","message2");
return false;
}

使用此坐标的代码:

public class DrawView extends View {
Paint paint = new Paint();
static float x_touch = -1;
static float y_touch = -1;
static int [][] arr = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
static int maxX = -1, maxY = -1;
public DrawView(Context context) {
super(context);
super.setWillNotDraw(false);
paint.setColor(Color.BLACK);
}

@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display mdisp = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
maxX = mdispSize.x;
maxY = mdispSize.y;
canvas.drawLine(maxX/3, 0, maxX/3, maxY, paint);
canvas.drawLine(maxX*2/3, 0, maxX*2/3, maxY, paint);
canvas.drawLine(0, maxY / 3, maxX, maxY / 3, paint);//This is first Horizontal line

canvas.drawLine(0, maxY * 2 / 3, maxX, maxY * 2 / 3, paint); //This is second horizontal line

Log.v("id1", "message1");
paint.setTextSize(150f);
if (x_touch>0 && y_touch>0) {
if (x_touch < maxX/3 && y_touch < maxY/3)
canvas.drawText("X", maxX/6, maxY/6, paint);
else if (x_touch>maxX/3 && x_touch<2*maxX/3 && y_touch<maxY/3)
canvas.drawText("X", maxX/2, maxY/6, paint);
else if (x_touch>maxX*2/3 && y_touch<maxY/3)
canvas.drawText("X", 5*maxX/6, maxY/6, paint);
else if (x_touch<maxX/3 && y_touch>maxY/3 && y_touch<2*maxY/3)
canvas.drawText("X", maxX/6, maxY/2, paint);
else if (x_touch>maxX/3 && x_touch<2*maxX/3 && y_touch>maxY/3 && y_touch<2*maxY/3)
canvas.drawText("X", maxX/2, maxY/2, paint);
else if (x_touch>2*maxX/3 && y_touch>maxY/3 && y_touch<2*maxY/3)
canvas.drawText("X", 5*maxX/6, maxY/2, paint);
else if (x_touch<maxX/3 && y_touch>2*maxY/3)
canvas.drawText("X", maxX/6, 5*maxY/6, paint);
else if (x_touch>maxX/3 && x_touch<2*maxX/3 && y_touch>2*maxY/3)
canvas.drawText("X", maxX/2, 5*maxY/6, paint);
else if (x_touch>2*maxX/3 && y_touch>2*maxY/3)
canvas.drawText("X", 5*maxX/6, 5*maxY/6, paint);
Log.v("id3", "maxX="+Float.toString(maxX)+" maxY="+Float.toString(maxY)+
" x_touch="+Float.toString(x_touch)+" y_touch="+Float.toString(y_touch));
}
}

public void setXandY(float x, float y) {
x_touch = x;
y_touch = y;
this.invalidate();
}
}

单击第一条水平线上方将在第一条水平线下方打印“X”,点击第二条水平线上方将在第二条水平线下方打印“X”。

Logcat 显示,单击第一条水平线上方确实会给出大于 maxY/3 的值。

编辑:我尝试从应用程序中删除标题栏,其准确性在一定程度上有所提高,但仍然不是很准确。

最佳答案

问题是您正在使用 Activity ( documentation ) 的 onTouchEvent 接收到的 MotionEvent ,因此您的坐标是接收是基于 Activity 而不是基于 View 。

要解决您的问题,您可以将手势检测移至 DrawView:

public class DrawView extends View implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {
private GestureDetectorCompat g1;

public DrawView(final Context context) {
super(context);

g1 = new GestureDetectorCompat(getContext(), this);
g1.setOnDoubleTapListener(this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
g1.onTouchEvent(event);
return super.onTouchEvent(event);
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
setXandY(e.getX(), e.getY());
Log.v("id2","message2");
return false;
}
}

此外,要在 DrawView 上接收触摸事件,请不要忘记使其可点击:

drawView.setClickable(true);

根据评论进行编辑:

在此示例中,黑色 X 代表触摸。

enter image description here

当你在ViewonTouchEvent上收到MotionEvent时,你收到的坐标是View相关的,所以你会收到(124 ,187)。

当你在ActivityonTouchEvent上接收到MotionEvent时,你接收到的坐标是与Activity相关的,所以你会接收到(324 ,487)。

当你在View上绘制时,坐标是View相关的,所以如果你绘制接收到的Activity相关的坐标,你就会在红点上绘制。

关于java - onSingleTapConfirmed 未给出准确的 y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36675130/

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