gpt4 book ai didi

android - 如何在 Android 的 Region 中找到给定 X 的最大可能 Y 点?

转载 作者:行者123 更新时间:2023-11-30 00:38:51 25 4
gpt4 key购买 nike

enter image description here

我在屏幕上绘制了一个虚拟标尺。当在生长的矩形内发生触摸事件时,我希望能够绘制一条直线,如上面的蓝线。但因为触摸不能 100% 笔直,运动可能像红线一样。这就是为什么我设置一个矩形来监听所有附近的触摸事件然后画一条蓝线。

我现在有

mRulerRect.set(mRulerCenter.x - mRulerBitmap.getWidth() / 2,
mRulerCenter.y - mRulerBitmap.getHeight()),
mRulerCenter.x + mRulerBitmap.getWidth() / 2,
mRulerCenter.y);
mPath.addRect(mRulerRect, Path.Direction.CCW);
mRulerMatrix.setRotate(mRulerAngle, mRulerCenter.x, mRulerCenter.y);
mPath.transform(mRulerMatrix);
mRegions.setPath(mPath, new Region(mRulerRect));

然后我通过 mRegions.contains(x,y) 检查触摸是否发生在棕色矩形内。到目前为止,触摸检测非常完美,但我现在遇到的问题是如何画一条直线。我试图固定 X 点然后计算 Y。当标尺水平时它工作正常然后从水平转向垂直时开始表现得很奇怪。我不知道如何做到这一点。请帮忙!谢谢。

最佳答案

你知道的事情:

  • 那个棕色矩形的中心是 mRulerCenter.x, mRulerCenter.y
  • 你想画的线穿过那个点
  • 直线的角度是mRulerAngle

我们只缺少一个元素,即我们要绘制的线的长度。这可能是标尺宽度的一部分,应该很容易计算 mRulerRect.width() * someFactor

现在,我们想知道线的起点和终点是什么,我们可以用三角函数来计算

float halfLineLength = mRulerRect.width() * someFactor;
float startAngle = (float) Math.toRadians(mRulerAngle);
float endAngle = (float) Math.toRadians(mRulerAngle + 180);

float startX = mRulerCenter.x + (float) Math.cos(startAngle) * halfLineLength;
float startY = mRulerCenter.y + (float) Math.sin(startAngle) * halfLineLength;

float endX = mRulerCenter.x + (float) Math.cos(endAngle) * halfLineLength;
float endY = mRulerCenter.y + (float) Math.sin(endAngle) * halfLineLength;

然后从 (startX, startY) 到 (endX, endY) 画一条线,实际上哪个是开始哪个是结束并不重要

关于android - 如何在 Android 的 Region 中找到给定 X 的最大可能 Y 点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42892718/

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