Android的onTouch不规则形状 [英] Android onTouch irregular shape

查看:113
本文介绍了Android的onTouch不规则形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查在 onTouchEvent 如果你摸了不规则形状像的这个等距之一?

How can I check in an onTouchEvent if you touch a irregular shape like this isometric one?

希望你能帮助我。

推荐答案

检查 MotionEvent 坐标是你的形状里,然后做一个点的多边形点击测试。

Check if the MotionEvent coordinates are inside your shape, then do a point-in-polygon hit test.

如果您的等距形状使用的和拥有所有90度角(在评论Geobits作为修正),快速测试可以做= http://en.wikipedia.org/wiki/Manhattan_distance相对=nofollow>曼哈顿距离。退房的链接,画面更清楚为什么这样工作的。

If your isometric shape is a diamond with equal length segments, and has angles of all 90 degrees (as corrected by Geobits in comments), a quick test can be done using Manhatten Distance. Check out the link, the pictures make it more clear why this works.

int rectSize = 10;
Rectangle diamondBoundingRect = new Rectangle(0,0,rectSize,rectSize);
Point point = new Point(9,9);
int dx = Math.abs(point.x - diamondBoundingRect.center.x);
int dy = Math.abs(point.y - diamondBoundingRect.center.y);
if(dx+dy < rectSize){
    //point is inside diamond
}else{
    //point is not inside diamond
}

此测试是不灵活,但它可能是你所需要的。

This test is inflexible, but it may be all you need.

如果你需要更好的东西,的这个博客帖子说明了一点,在多边形命中测试的算法。

If you need something better, this blog post explains the algorithm for point-in-polygon hit tests.

我们的想法是从投行(称为雷)您的 MotionEvent 指向您的多边形,并计算在该行和线段之间的交叉点的数目多边形。如果计数为偶数,该点的形状之外。如果它的奇,关键是里面的造型。

The idea is to cast a line (called a Ray) from your MotionEvent point to your polygon, and count the number of intersections between that line and line segments in that polygon. If the count is even, the point is outside the shape. If its odd, the point is inside the shape.

维基百科有一个漂亮的图片和更多的细节,以帮助您了解这种做法的逻辑。

Wikipedia has a nice picture and more details to help you understand the logic in this approach.

在得到了这个下一部分之前工作集中,因为它可能取决于你的需求被证明是不必要的。

onTouchEvent 采样发送给您的点。该设备只是给你点和方向的一些predetermined率。更快的用户移动他的手指,你的采样速率跟不上少的细节。

The points sent to you in your onTouchEvent are sampled. The device is just giving you points and direction at some predetermined rate. The faster the user moves his finger, the less detail you have as the sampling rate can't keep up.

所以,如果您想了解更多的细节,你必须插值。这是说,你要猜出用户提供一些花哨的数学偷走他的手指的高级方式。余弦插值一直运作良好,我过去那样的速度和结果之间的良好平衡。

So, if you want more detail, you'll have to interpolate . This is an advanced way of saying you need to guess where the user swiped his fingers with some fancy math. Cosine interpolation has worked well for me in the past as its a good balance between speed and results.

编辑:

下面是一些code我用亲手为点在多边形命中测试。

Here's some code I use personally for point-in-polygon hit tests.

public boolean isPointInsidePolygon(double x, double y){
    //pointAry holds the polygon's vertices.
    int polySides = this.pointAry.Length;
    //need at lest 3 vertices to create a polygon.
    if (polySides < 3)
    {
        return false;
    }
    int j = polySides - 1;
    boolean oddNodes = false;
    for (int i = 0; i < polySides; i++)
    {
        Point pi = this.pointAry[i];
        Point pj = this.pointAry[j];
        if (pi.y < y && pj.y >= y || pj.y < y && pi.y >= y)
        {
            if (pi.x + (y - pi.y) / (pj.y - pi.y) * (pj.x - pi.x) < x)
           {
                oddNodes = !oddNodes;
            }
        }
        j = i;
     }
     return oddNodes;
 }

这篇关于Android的onTouch不规则形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆