Java Code Examples for android.graphics.Region#setPath()

The following examples show how to use android.graphics.Region#setPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FruitProjectileManager.java    From FruitNinja with Apache License 2.0 6 votes vote down vote up
@Override
   public int testForCollisions(List<TimedPath> allPaths) {

int score = 0;
for (TimedPath p : allPaths) {
    for (Projectile f : fruitProjectiles) {

	if(!f.isAlive())
	    continue;

	Region projectile = new Region(f.getLocation());
	Region path = new Region();
	path.setPath(p, clip);
	
	if (!projectile.quickReject(path) && projectile.op(path, Region.Op.INTERSECT)) {
	    f.kill();
	    score++;
	}
    }
}
return score;
   }
 
Example 2
Source File: VectorView.java    From SVG-Android with Apache License 2.0 5 votes vote down vote up
public int findPathIndexByPoint(int x, int y) {
    Region region = new Region();
    for (int i = 0; i < mPaths.length; i++) {
        mPaths[i].computeBounds(RECTF, true);
        region.setPath(mPaths[i], new Region((int)RECTF.left,(int)RECTF.top,(int)RECTF.right,(int)RECTF.bottom));
        if (region.contains(x, y)) {
            return i;
        }
    }
    return INVALID_PATH_INDEX;
}
 
Example 3
Source File: PolyOverlayWithIW.java    From osmdroid with Apache License 2.0 5 votes vote down vote up
/**
   * Used to be if {@link Polygon}
   * Important note: this function returns correct results only if the Poly has been drawn before,
   * and if the MapView positioning has not changed.
   * @return true if the Poly contains the event position.
   */
  public boolean contains(final MotionEvent pEvent){
      if (mPath.isEmpty()) {
	return false;
}
      final RectF bounds = new RectF(); //bounds of the Path
      mPath.computeBounds(bounds, true);
      final Region region = new Region();
      //Path has been computed in #draw (we assume that if it can be clicked, it has been drawn before).
      region.setPath(mPath, new Region((int)bounds.left, (int)bounds.top,
              (int) (bounds.right), (int) (bounds.bottom)));
      return region.contains((int)pEvent.getX(), (int)pEvent.getY());
  }
 
Example 4
Source File: PDGraphicsState.java    From PdfBox-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Modify the current clipping path by intersecting it with the given path.
 * @param path path to intersect with the clipping path
 */
public void intersectClippingPath(Path path)
{
	RectF bounds = new RectF();
	path.computeBounds(bounds, true);
	Region r = new Region();
	Rect boundsRounded = new Rect();
	bounds.round(boundsRounded);
	r.setPath(path, new Region(boundsRounded));
    intersectClippingPath(r);
}
 
Example 5
Source File: PDGraphicsState.java    From PdfBox-Android with Apache License 2.0 5 votes vote down vote up
/**
     * Constructor with a given page size to initialize the clipping path.
     * @param page the size of the page
     */
    public PDGraphicsState(PDRectangle page)
    {
//        clippingPath = new Area(new GeneralPath(page.toGeneralPath()));TODO: PdfBox-Android
    	RectF bounds = new RectF();
    	page.toGeneralPath().computeBounds(bounds, true);
    	clippingPath = new Region();
    	Rect boundsRounded = new Rect();
    	bounds.round(boundsRounded);
    	clippingPath.setPath(page.toGeneralPath(), new Region(boundsRounded));
    }
 
Example 6
Source File: PieGraph.java    From Pimp_my_Z1 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {

    Point point = new Point();
    point.x = (int) event.getX();
    point.y = (int) event.getY();
    
    int count = 0;
    for (PieSlice slice : slices){
    	Region r = new Region();
    	r.setPath(slice.getPath(), slice.getRegion());
    	if (r.contains((int)point.x,(int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN){
    		indexSelected = count;
    	} else if (event.getAction() == MotionEvent.ACTION_UP){
    		if (r.contains((int)point.x,(int) point.y) && listener != null){
    			if (indexSelected > -1){
	    			listener.onClick(indexSelected);
    			}
    			indexSelected = -1;
    		}
    		
    	}
	    count++;
    }
    
    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP){
    	postInvalidate();
    }
    
    

    return true;
}
 
Example 7
Source File: BarGraph.java    From Pimp_my_Z1 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {

	Point point = new Point();
	point.x = (int) event.getX();
	point.y = (int) event.getY();

	int count = 0;
	for (Bar bar : points) {
		Region r = new Region();
		r.setPath(bar.getPath(), bar.getRegion());
		if (r.contains((int) point.x, (int) point.y)
				&& event.getAction() == MotionEvent.ACTION_DOWN) {
			indexSelected = count;
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			if (r.contains((int) point.x, (int) point.y)
					&& listener != null) {
				listener.onClick(indexSelected);
			}
			indexSelected = -1;
		}
		count++;
	}

	if (event.getAction() == MotionEvent.ACTION_DOWN
			|| event.getAction() == MotionEvent.ACTION_UP) {
		shouldUpdate = true;
		postInvalidate();
	}

	return true;
}
 
Example 8
Source File: PathMeasureView.java    From zone-sdk with MIT License 5 votes vote down vote up
private void logRegion() {
    // ▼在屏幕中间添加一个圆
    Path circlePath = new Path();
    circlePath.addCircle(mViewWidth/2, mViewHeight/2, 300, Path.Direction.CW);
    // ▼将剪裁边界设置为视图大小
    Region globalRegion = new Region(0, 0, mViewWidth, mViewHeight/2);

    // ▼将 Path 添加到 Region 中
    Region circleRegion=new Region();
    circleRegion.setPath(circlePath, globalRegion);
    System.err.println("circleRegion"+(circleRegion.contains(mViewWidth/2,mViewHeight/2-100)?"包含":"不包含")+"圆心h-100");
    System.err.println("circleRegion"+(circleRegion.contains(mViewWidth/2,mViewHeight/2+100)?"包含":"不包含")+"圆心h+100");
    System.err.println("circleRegion"+(circleRegion.contains(mViewWidth/2,mViewHeight/2-400)?"包含":"不包含")+"圆心h-400");
}
 
Example 9
Source File: ShapeUtils.java    From cidrawing with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a simple region from the given path.
 *
 * @param path given path
 * @return region object
 */
public static Region createRegionFromPath(Path path) {
    Region region = new Region();
    if (path != null) {
        RectF box = new RectF();
        path.computeBounds(box, true);
        region.setPath(path, new Region((int) box.left, (int) box.top, (int) box.right, (int) box.bottom));
    }
    return region;
}
 
Example 10
Source File: PathUnit.java    From InDoorSurfaceView with Apache License 2.0 5 votes vote down vote up
public PathUnit(List<PointF> list) {
    int i = 0;
    path = new Path();
    for (PointF point : list) {
        if (i == 0) path.moveTo(point.x, point.y);
        path.lineTo(point.x, point.y);
        i++;
    }
    RectF rectF = new RectF();
    path.computeBounds(rectF, true);
    region = new Region();
    region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
}
 
Example 11
Source File: TextDrawable.java    From litho with Apache License 2.0 5 votes vote down vote up
private boolean isClickCloseToSpan(
    ClickableSpan span,
    Spanned buffer,
    Layout layout,
    Region touchAreaRegion,
    Region clipBoundsRegion) {
  final Region clickableSpanAreaRegion = new Region();
  final Path clickableSpanAreaPath = new Path();

  layout.getSelectionPath(
      buffer.getSpanStart(span), buffer.getSpanEnd(span), clickableSpanAreaPath);
  clickableSpanAreaRegion.setPath(clickableSpanAreaPath, clipBoundsRegion);

  return clickableSpanAreaRegion.op(touchAreaRegion, Region.Op.INTERSECT);
}
 
Example 12
Source File: BitmapSticker.java    From imsdk-android with MIT License 5 votes vote down vote up
private void calculateClickType(int x, int y) {
    RectF rectF = new RectF(x - btSize / 2 - 40, y - btSize / 2 - 40, x + btSize / 2 + 40, y + btSize / 2 + 40);
    if (rectF.contains(dstPs[2] - 20, dstPs[3])) {
        clickType = ClickType.DELETE;
    } else if (rectF.contains(dstPs[0], dstPs[1])) {
        clickType = ClickType.MIRROR;
    } else if (rectF.contains(dstPs[4] + 20, dstPs[5])) {
        clickType = ClickType.SCALE;
    } else if (rectF.contains(dstPs[6] - 20, dstPs[7])) {
        clickType = ClickType.IMAGE;
    } else {

        RectF bounds = new RectF();
        path.computeBounds(bounds, true);
        Region region = new Region();
        region.setPath(path, new Region((int)bounds.left, (int)bounds.top,(int)bounds.right, (int)bounds.bottom));

        if (region.contains(x, y)) {
            if (isOut) {
                isOut = false;
            }
            if (!isUsing) {
                isUsing = true;
                listener.onUsing();
                postInvalidate();
            }
            clickType = ClickType.IMAGE;
        } else {
            if (isUsing) {
                isUsing = false;
                postInvalidate();
            }
            if (!isOut) {
                isOut = true;
            }
            clickType = ClickType.OUT;
        }
    }
}
 
Example 13
Source File: DingRegionManager.java    From CombineBitmap with Apache License 2.0 5 votes vote down vote up
@Override
public Region[] calculateRegion(int size, int subSize, int gap, int count) {
    Region[] regions = new Region[count];
    Region globalRegion = new Region(0, 0, size, size);

    int[][] dxy = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};

    for (int i = 0; i < count; i++) {
        float width = size;
        float height = size;

        if (count == 2 || (count == 3 && i == 0)) {
            width = (size - gap) / 2;
            height = size;
        } else if ((count == 3 && (i == 1 || i == 2)) || count == 4) {
            width = (size - gap) / 2;
            height = (size - gap) / 2;
        }

        int dx = dxy[i][0];
        int dy = dxy[i][1];

        float left = dx * (size + gap) / 2.0f;
        float top = dy * (size + gap) / 2.0f;
        float right = left + width;
        float bottom = top + height;
        Path path = new Path();
        path.addRect(left, top, right, bottom, Path.Direction.CW);

        Region region = new Region();
        region.setPath(path, globalRegion);

        regions[i] = region;
    }
    return regions;
}
 
Example 14
Source File: BitmapSticker.java    From EasyPhotos with Apache License 2.0 5 votes vote down vote up
private void calculateClickType(int x, int y) {
    RectF rectF = new RectF(x - btSize / 2 - 40, y - btSize / 2 - 40, x + btSize / 2 + 40, y + btSize / 2 + 40);
    if (rectF.contains(dstPs[2] - 20, dstPs[3])) {
        clickType = ClickType.DELETE;
    } else if (rectF.contains(dstPs[0], dstPs[1])) {
        clickType = ClickType.MIRROR;
    } else if (rectF.contains(dstPs[4] + 20, dstPs[5])) {
        clickType = ClickType.SCALE;
    } else if (rectF.contains(dstPs[6] - 20, dstPs[7])) {
        clickType = ClickType.IMAGE;
    } else {

        RectF bounds = new RectF();
        path.computeBounds(bounds, true);
        Region region = new Region();
        region.setPath(path, new Region((int)bounds.left, (int)bounds.top,(int)bounds.right, (int)bounds.bottom));

        if (region.contains(x, y)) {
            if (isOut) {
                isOut = false;
            }
            if (!isUsing) {
                isUsing = true;
                listener.onUsing();
                postInvalidate();
            }
            clickType = ClickType.IMAGE;
        } else {
            if (isUsing) {
                isUsing = false;
                postInvalidate();
            }
            if (!isOut) {
                isOut = true;
            }
            clickType = ClickType.OUT;
        }
    }
}
 
Example 15
Source File: TextSticker.java    From EasyPhotos with Apache License 2.0 4 votes vote down vote up
private void calculateClickType(int x, int y) {
        RectF rectF = new RectF(x - btSize / 2 - 40, y - btSize / 2 - 40, x + btSize / 2 + 40, y + btSize / 2 + 40);

        Rect rect = new Rect();


        if (rectF.contains(dstPs[2] - 20, dstPs[3])) {
            clickType = ClickType.DELETE;
        }
//        else if (rectF.contains(dstPs[0], dstPs[1])) {
//            clickType = ClickType.EDITOR;
//        }
        else if (rectF.contains(dstPs[4] + 20, dstPs[5])) {
            clickType = ClickType.SCALE;
        }
//        else if (rectF.contains(dstPs[6] - 20, dstPs[7])) {
//            clickType = ClickType.IMAGE;
//        }
        else {
            RectF bounds = new RectF();
            path.computeBounds(bounds, true);
            Region region = new Region();
            region.setPath(path, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom));

            if (region.contains(x, y)) {
                if (isOut) {
                    isOut = false;
                }
                if (!isUsing) {
                    isUsing = true;
                    listener.onUsing();
                    postInvalidate();
                }
                clickType = ClickType.IMAGE;
            } else {
                if (isUsing) {
                    isUsing = false;
                    postInvalidate();
                }
                if (!isOut) {
                    isOut = true;
                }
                clickType = ClickType.OUT;
            }
        }
    }
 
Example 16
Source File: LineGraph.java    From Pimp_my_Z1 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {

    Point point = new Point();
    point.x = (int) event.getX();
    point.y = (int) event.getY();

    int count = 0;
    int lineCount = 0;
    int pointCount = 0;

    Region r = new Region();
    for (Line line : lines) {
        pointCount = 0;
        for (LinePoint p : line.getPoints()) {

            if (p.getPath() != null && p.getRegion() != null) {
                r.setPath(p.getPath(), p.getRegion());
                if (r.contains((int) point.x, (int) point.y) && event.getAction() == MotionEvent.ACTION_DOWN) {
                    indexSelected = count;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (r.contains((int) point.x, (int) point.y) && listener != null) {
                        listener.onClick(lineCount, pointCount);
                    }
                    indexSelected = -1;
                }
            }

            pointCount++;
            count++;
        }
        lineCount++;

    }

    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_UP) {
        shouldUpdate = true;
        postInvalidate();
    }


    return true;
}
 
Example 17
Source File: PieChart.java    From OXChart with Apache License 2.0 4 votes vote down vote up
/**计算各种绘制坐标*/
    private void evaluatorData(float animPre){
        paintLabel.setTextSize(tagTextSize);
        float tagTextLead = FontUtil.getFontLeading(paintLabel);
        float tagTextHeight = FontUtil.getFontHeight(paintLabel);
        float oneStartAngle = startAngle;
        for(int i = 0; i < dataList.size(); i++) {
            PieChartBean bean = dataList.get(i);
           /* if(bean.getNum() == 0 && !showZeroPart){
                continue;
            }*/
            /**1、绘制扇形*/
            float arcLeft = centerPoint.x - chartRaidus;  //扇形半径
            float arcTop = centerPoint.y - chartRaidus;
            float arcRight = centerPoint.x + chartRaidus;
            float arcBottom = centerPoint.y + chartRaidus;

//            float percentage = 360.0f / total * bean.getNum();
            float percentage = (bean.getNum()==0?0:(360.0f / total * bean.getNum())*animPre);
            bean.setArcRect(new RectF(arcLeft, arcTop, arcRight, arcBottom));
            bean.setStartAngle(oneStartAngle);
            bean.setSweepAngle(percentage);

            /**2、计算扇形区域*/
            arcLeft = centerPoint.x - chartSize;
            arcTop = centerPoint.y - chartSize;
            arcRight = centerPoint.x + chartSize;
            arcBottom = centerPoint.y + chartSize;
            Path allPath = new Path();
            allPath.moveTo(centerPoint.x, centerPoint.y);//添加原始点
            float ovalX = centerPoint.x + (float) (chartRaidus * Math.cos(Math.toRadians(oneStartAngle)));
            float ovalY = centerPoint.y + (float) (chartRaidus * Math.sin(Math.toRadians(oneStartAngle)));
            allPath.lineTo(ovalX, ovalY);
            RectF touchOval = new RectF(arcLeft, arcTop, arcRight, arcBottom);
            allPath.addArc(touchOval, oneStartAngle, percentage);
            allPath.lineTo(centerPoint.x, centerPoint.y);
            allPath.close();
            RectF r = new RectF();
            allPath.computeBounds(r, true);
            Region region = new Region();
            region.setPath(allPath, new Region((int) r.left, (int) r.top, (int) r.right, (int) r.bottom));
            bean.setRegion(region);

            if(MODUL_CHART == tagModul) {
                /**3、绘制直线*/
                //确定直线的起始和结束的点的位置
                float startX = centerPoint.x + (float) (chartRaidus * Math.cos(Math.toRadians(oneStartAngle + percentage / 2)));
                float startY = centerPoint.y + (float) (chartRaidus * Math.sin(Math.toRadians(oneStartAngle + percentage / 2)));
                float endX = centerPoint.x + (float) ((chartRaidus + lineLenth - 20) * Math.cos(Math.toRadians(oneStartAngle + percentage / 2)));
                float endY = centerPoint.y + (float) ((chartRaidus + lineLenth - 20) * Math.sin(Math.toRadians(oneStartAngle + percentage / 2)));
                boolean isRight = true;
                float lineAngle = oneStartAngle + percentage / 2;
                if (lineAngle > 90 && lineAngle < 270) {
                    isRight = false;
                }
//            LogUtil.i(TAG, "直线坐标:start=("+startX+","+startY +")  end=("+endX+","+endY+")"+"   lineAngle="+lineAngle+"  isRight="+isRight);
                List<PointF> tagLinePoints = new ArrayList<>();
                tagLinePoints.add(new PointF(startX, startY));
                tagLinePoints.add(new PointF(endX, endY));
                float textX = isRight ? (endX + 20) : (endX - 20);
                tagLinePoints.add(new PointF(textX, endY));
                bean.setTagLinePoints(tagLinePoints);

                /**3、绘制指示标签*/
                String tagText = "";
                paintLabel.setTextSize(tagTextSize);
                if (tagType == PieChartLayout.TAG_TYPE.TYPE_NUM) {
                    tagText = bean.getNum() + "";
                } else if (tagType == PieChartLayout.TAG_TYPE.TYPE_PERCENT) {
                    DecimalFormat decimalFormat = new DecimalFormat("0.0%");
                    tagText = (total==0?"/":decimalFormat.format(((float) bean.getNum() / (float) total)));
                }
                float textW = FontUtil.getFontlength(paintLabel, tagText);
                textX = isRight ? textX + textSpace : (textX - textW - textSpace);
                float textY = endY - tagTextHeight / 2 + tagTextLead;
                bean.setTagStr(tagText);
                bean.setTagTextPoint(new PointF(textX, textY));
            }

            /*开始角度累加*/
            oneStartAngle += percentage;
        }

    }
 
Example 18
Source File: TextSticker.java    From imsdk-android with MIT License 4 votes vote down vote up
private void calculateClickType(int x, int y) {
        RectF rectF = new RectF(x - btSize / 2 - 40, y - btSize / 2 - 40, x + btSize / 2 + 40, y + btSize / 2 + 40);

        Rect rect = new Rect();


        if (rectF.contains(dstPs[2] - 20, dstPs[3])) {
            clickType = ClickType.DELETE;
        }
//        else if (rectF.contains(dstPs[0], dstPs[1])) {
//            clickType = ClickType.EDITOR;
//        }
        else if (rectF.contains(dstPs[4] + 20, dstPs[5])) {
            clickType = ClickType.SCALE;
        }
//        else if (rectF.contains(dstPs[6] - 20, dstPs[7])) {
//            clickType = ClickType.IMAGE;
//        }
        else {
            RectF bounds = new RectF();
            path.computeBounds(bounds, true);
            Region region = new Region();
            region.setPath(path, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom));

            if (region.contains(x, y)) {
                if (isOut) {
                    isOut = false;
                }
                if (!isUsing) {
                    isUsing = true;
                    listener.onUsing();
                    postInvalidate();
                }
                clickType = ClickType.IMAGE;
            } else {
                if (isUsing) {
                    isUsing = false;
                    postInvalidate();
                }
                if (!isOut) {
                    isOut = true;
                }
                clickType = ClickType.OUT;
            }
        }
    }
 
Example 19
Source File: WechatRegionManager.java    From CombineBitmap with Apache License 2.0 4 votes vote down vote up
@Override
public Region[] calculateRegion(int size, int subSize, int gap, int count) {
    Region[] regions = new Region[count];
    Region globalRegion = new Region(0, 0, size, size);

    for (int i = 0; i < count; i++) {

        float x = 0;
        float y = 0;

        if (count == 2) {
            x = gap + i * (subSize + gap);
            y = (size - subSize) / 2.0f;
        } else if (count == 3) {
            if (i == 0) {
                x = (size - subSize) / 2.0f;
                y = gap;
            } else {
                x = gap + (i - 1) * (subSize + gap);
                y = subSize + 2 * gap;
            }
        } else if (count == 4) {
            x = gap + (i % 2) * (subSize + gap);
            if (i < 2) {
                y = gap;
            } else {
                y = subSize + 2 * gap;
            }
        } else if (count == 5) {
            if (i == 0) {
                x = y = (size - 2 * subSize - gap) / 2.0f;
            } else if (i == 1) {
                x = (size + gap) / 2.0f;
                y = (size - 2 * subSize - gap) / 2.0f;
            } else if (i > 1) {
                x = gap + (i - 2) * (subSize + gap);
                y = (size + gap) / 2.0f;
            }
        } else if (count == 6) {
            x = gap + (i % 3) * (subSize + gap);
            if (i < 3) {
                y = (size - 2 * subSize - gap) / 2.0f;
            } else {
                y = (size + gap) / 2.0f;
            }
        } else if (count == 7) {
            if (i == 0) {
                x = (size - subSize) / 2.0f;
                y = gap;
            } else if (i < 4) {
                x = gap + (i - 1) * (subSize + gap);
                y = subSize + 2 * gap;
            } else {
                x = gap + (i - 4) * (subSize + gap);
                y = gap + 2 * (subSize + gap);
            }
        } else if (count == 8) {
            if (i == 0) {
                x = (size - 2 * subSize - gap) / 2.0f;
                y = gap;
            } else if (i == 1) {
                x = (size + gap) / 2.0f;
                y = gap;
            } else if (i < 5) {
                x = gap + (i - 2) * (subSize + gap);
                y = subSize + 2 * gap;
            } else {
                x = gap + (i - 5) * (subSize + gap);
                y = gap + 2 * (subSize + gap);
            }
        } else if (count == 9) {
            x = gap + (i % 3) * (subSize + gap);
            if (i < 3) {
                y = gap;
            } else if (i < 6) {
                y = subSize + 2 * gap;
            } else {
                y = gap + 2 * (subSize + gap);
            }
        }

        float left = x;
        float top = y;
        float right = left + subSize;
        float bottom = top + subSize;
        Path path = new Path();
        path.addRect(left, top, right, bottom, Path.Direction.CW);

        Region region = new Region();
        region.setPath(path, globalRegion);

        regions[i] = region;
    }
    return regions;
}