【问题标题】:How to get exact region of a path on canvas如何在画布上获取路径的确切区域
【发布时间】:2017-04-15 20:09:06
【问题描述】:

我正在使用它来获取路径区域:

 RectF rectF = new RectF();
 Path currentPath = getCurrentPath();
 currentPath.computeBounds(rectF, true);
 Region region = new Region();

这给了我一个这样的矩形:

绿色矩形是我可以从圆圈中得到的区域。

我想通过拖动来移动红色圆圈,所以我需要它的确切边界而不是这个矩形。

使用这种方法,圆可以从它的中心移动,但我不想要它。我希望它可以仅从其确切的红点移动。

我如何确定确切的界限?

【问题讨论】:

    标签: android canvas path region


    【解决方案1】:

    我只是通过使用两个区域来解决这个问题。下面是示例代码:

    首先,获取大圆Region为clickableRegion:

    Region clickableRegion = new Region(); 
    RectF rectF = new RectF();
    // bigCirclePath is the red ring
    Path bigCirclePath = getBigCirclePath();
    bigCirclePath.computeBounds(rectF, true);
    Rect rect = new Rect((int)rectF.left, (int)rectF.top, (int)rectF.right, (int)rectF.bottom);
    // this will get the circle Region
    clickableRegion.setPath(bigCirclePath, new Region(rect));
    

    然后,使用类似的代码来获取小圆圈区域为disClickableRegion。小圆圈应与您的大圆圈具有相同的中心。

    定义一个contains函数;

    public boolean conatins(Point p) { 
        if(disClickableRegion.contains(p.x, p.y)) {
            return false;
        }
    
        return clickableRegion.contains(p.x, p.y);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-29
      • 2013-05-02
      • 2023-03-25
      • 2015-05-23
      • 2018-06-07
      • 1970-01-01
      • 2017-01-02
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多