- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在构建自定义 View 。
它有 4 个弧。
我在 onDraw()
期间使用 RectF
形状绘制圆弧:
// arcPaint is a Paint object initialized
// in the View's constructor
arcPaint.setStrokeWidth(strokeWidth);
arcPaint.setColor(arcColor);
// Draw arcs, arc1, arc2, arc3 & arc4 are
// measured and initialized during onMeasure()
canvas.drawArc(arc1, startAngle, arc1Angle, false, arcPaint);
canvas.drawArc(arc2, startAngle, arc2Angle, false, arcPaint);
canvas.drawArc(arc3, startAngle, arc3Angle, false, arcPaint);
canvas.drawArc(arc4, startAngle, arc4Angle, false, arcPaint);
所以,结果是:
如果我使用 canvas.drawRect()
和这段代码绘制 RectF
对象:
canvas.drawRect(arc1, arcPaint);
canvas.drawRect(arc2, arcPaint);
canvas.drawRect(arc3, arcPaint);
canvas.drawRect(arc4, arcPaint);
结果是这样的:
将所有的圆弧和矩形画在一起,得到:
我知道我可以覆盖 onTouchEvent()
方法并获取 X、Y 坐标,但我不知道每个坐标如何与 Canvas 中绘制的每个形状相关。
我想做的是检测何时在 canvas
中触摸了弧线,而不是矩形,我该如何实现?
编辑:
不是矩形,我的意思是,我不想检测用户何时触摸这些区域(矩形的角):
最佳答案
这基本上归结为比较距该图形中心点的距离。由于边界矩形始终是正方形,因此这些距离之一基本上是固定的,这使得这更容易。
这里的一般方法是计算从中心点到触摸点的距离,并检查该距离与圆弧半径的差值是否在任一笔画宽度的一半以内。
private static boolean isOnRing(MotionEvent event, RectF bounds, float strokeWidth) {
// Figure the distance from center point to touch point.
final float distance = distance(event.getX(), event.getY(),
bounds.centerX(), bounds.centerY());
// Assuming square bounds to figure the radius.
final float radius = bounds.width() / 2f;
// The Paint stroke is centered on the circumference,
// so the tolerance is half its width.
final float halfStrokeWidth = strokeWidth / 2f;
// Compare the difference to the tolerance.
return Math.abs(distance - radius) <= halfStrokeWidth;
}
private static float distance(float x1, float y1, float x2, float y2) {
return (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
在 onTouchEvent()
方法中,isOnRing()
只是通过 MotionEvent
调用,圆弧的边界 RectF
,以及 Paint
的笔画宽度。例如:
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isOnRing(event, arc1, arcPaint.getStrokeWidth())) {
// Hit.
}
break;
...
}
...
}
这本身只会确定触摸点是否位于由半径和笔划宽度描述的完整环上的任何位置。也就是说,它不考虑弧中的“间隙”。如果需要,有多种方法可以处理该问题。
如果您打算使起始角和扫描角与 x/y 轴齐平,最简单的方法可能是检查触摸点和中心点坐标差异的标志。从广义上讲,如果 touch x 减去 center x 是正的,那么你在右边;如果为负,则您在左侧。同样,计算顶部或底部将使您能够确定触摸发生在哪个象限,以及是否忽略该事件。
final float dx = event.getX() - arc1.centerX();
final float dy = event.getY() - arc1.centerY();
if (dx > 0) {
// Right side
}
else {
// Left side
}
...
但是,如果任何一个角都不与轴共端点,那么数学就有点复杂了。例如:
private static boolean isInSweep(MotionEvent event, RectF bounds,
float startAngle, float sweepAngle) {
// Figure atan2 angle.
final float at =
(float) Math.toDegrees(Math.atan2(event.getY() - bounds.centerY(),
event.getX() - bounds.centerX()));
// Convert from atan2 to standard angle.
final float angle = (at + 360) % 360;
// Check if in sweep.
return angle >= startAngle && angle <= startAngle + sweepAngle;
}
然后 onTouchEvent()
中的 if
将更改以合并这两个检查。
if (isOnRing(event, arc1, arcPaint.getStrokeWidth()) &&
isInSweep(event, arc1, startAngle, arc1Angle)) {
// Hit.
}
这些方法可以很容易地组合成一个,但为了清楚起见,它们在这里分开。
关于android - 如何从 Android canvas 获取触摸形状(弧形)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44512045/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!