创建"地图"一个按钮的边缘的 [英] Creating a "map" of the edges of a button

查看:139
本文介绍了创建"地图"一个按钮的边缘的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,从这里这个问题下面就:圆形按钮,我想我需要创造一种地图该视图的边缘,可以说我有一个看起来像这样的观点:

So following on from this question here: Rounded buttons, I think I need to create a kind of map of the edges of the view, lets say I have a view that looks like this:

此按钮不会是蓝色或任何特定的颜色,所以我们不能检查,如果在用户感动是蓝色的,由最后一个问题的答案的一个建议。

The button isn't going to be blue or any specific colour so we can't check if where the user touched is blue, suggested by one of the answers in the last question.

可以说,我收到一个触摸事件,而我得到这个视图中触摸的位置,观点正确,而且我只是想,如果他们美元,即蓝色零件P $ PSS采取投入。我怎样才能弄清楚这一点?

Lets say I receive a touch event, and I get the position of the touch inside this view, the view is rect and I only want to take in input if they press on the part that is blue. How can I figure out this?

推荐答案

让我们假设为图形边框正好填充视图。我们可以进行如下操作。感兴趣的区域是由两个同心圆和由视图界。 (圆心是视图的右下角。)当我们触摸,我们只需要计算从触摸的距离坐标右下角和比较,两个圆圈的半径。 (可以通过的平方距离进行比较,以平方半径避免平方根)。如果距离落在半径之间,触摸是在蓝色(或任何颜色)。我们并不需要计算所述触摸是否为边界框之内;我们已经知道,因为事件被送到视图。

Let's assume that the bounding box for the graphic exactly fills the view. We can proceed as follows. The area of interest is bounded by two concentric circles and by the view. (The circle center is the bottom right corner of the view.) When we get a touch, we just need to compute the distance from the touch coordinate to the bottom right corner and compare that to the two circle radii. (You can avoid a square root by comparing the squared distance to the squared radii.) If the distance falls between the radii, the touch is in the blue (or whatever color). We don't need to compute whether the touch is within the bounding box; we already know that since the event was delivered to the view.

下面是一些示例code。它是该确定的点是否为两个同心圆(一个环形)内的一个探测器,并且如果它是,该象限它击中

Here's some sample code. It's for a detector that determines whether a point is within two concentric circles (an annulus) and, if it is, which quadrant it hit.

public class ArcHitDetector {
    public enum Quadrant {
        TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT
    }
    private int xCenter, yCenter, innerR2, outerR2;
    /**
     * Construct an ArcHitDetector for an annulus centered at given
     * points and with given inner and outer radii.
     *
     * @param xCenter the horizontal center coordinate
     * @param yCenter the vertical center coordinate
     * @param outerR the outer radius of the annulus
     * @param innerR the inner radius of the annulus
     */
    public ArcHitDetector(int xCenter, int yCenter, int outerR, int innerR) {
        this.xCenter = xCenter;
        this.yCenter = yCenter;
        this.outerR2 = outerR * outerR;
        this.innerR2 = innerR * innerR;
    }

    /**
     * Classify a point with respect to the annulus. It assumes
     * screen coordinates (x increases to the right; y increases
     * down).
     *
     * @param x the x coordinate of the point to test
     * @param y the y coordinate of the point to test
     *
     * @return the Quadrant of the annulus in which the point falls,
     *    or null if the point does not lie in the annulus
     */
    public Quadrant classifyHit(int x, int y) {
        int dx = x - xCenter;
        int dy = y - yCenter;
        int d2 = dx * dx + dy * dy;
        if (d2 <= outerR2 && d2 >= innerR2) {
            if (x >= xCenter) {
                return y <= yCenter ? TOP_RIGHT : BOTTOM_RIGHT;
            } else {
                return y <= yCenter ? TOP_LEFT : BOTTOM_LEFT;
            }
        } else {
            return null;
        }
    }
}

这篇关于创建&QUOT;地图&QUOT;一个按钮的边缘的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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