下面列出了android.graphics.Path#computeBounds ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
PathInfo(Path path, float width, float height) {
this.path = path;
float tmpWidth = width;
float tmpHeight = height;
RectF bounds = new RectF();
path.computeBounds(bounds, true);
if(width <= 0 && height <= 0) {
tmpWidth = (float) Math.ceil(bounds.width());
tmpHeight = (float) Math.ceil(bounds.height());
path.offset(-1 * (float) Math.floor(bounds.left),
-1 * (float) Math.round(bounds.top));
}
this.width = tmpWidth;
this.height = tmpHeight;
}
public void setGesture(Gesture gesture) {
if (mCurrentGesture != null) {
clear(false);
}
setCurrentColor(mCertainGestureColor);
mCurrentGesture = gesture;
final Path path = mCurrentGesture.toPath();
final RectF bounds = new RectF();
path.computeBounds(bounds, true);
// TODO: The path should also be scaled to fit inside this view
mPath.rewind();
mPath.addPath(path, -bounds.left + (getWidth() - bounds.width()) / 2.0f,
-bounds.top + (getHeight() - bounds.height()) / 2.0f);
mResetGesture = true;
invalidate();
}
PathInfo(Path path, float width, float height) {
this.path = path;
float tmpWidth = width;
float tmpHeight = height;
RectF bounds = new RectF();
path.computeBounds(bounds, true);
if(width <= 0 && height <= 0) {
tmpWidth = (float) Math.ceil(bounds.width());
tmpHeight = (float) Math.ceil(bounds.height());
path.offset(-1 * (float) Math.floor(bounds.left),
-1 * (float) Math.round(bounds.top));
}
this.width = tmpWidth;
this.height = tmpHeight;
}
@Override
public Rectangle getBounds()
{
RectF rectf=new RectF();
if(_Shape instanceof GeneralPath)
{
Path path=((GeneralPath)_Shape).getPath();
path.computeBounds(rectf, true);
int width=(int)rectf.right-(int)rectf.left;
int height=(int)rectf.bottom-(int)rectf.top;
Rectangle rect=new Rectangle((int)rectf.left,(int)rectf.top,width,height);
return rect;
}
else
return this.getBounds();
}
@Override
public boolean doTextContainer(TextContainer obj)
{
if (obj instanceof SVG.TextPath)
{
// Since we cheat a bit with our textPath rendering, we need
// to cheat a bit with our bbox calculation.
SVG.TextPath tpath = (SVG.TextPath) obj;
SVG.SvgObject ref = obj.document.resolveIRI(tpath.href);
if (ref == null) {
error("TextPath path reference '%s' not found", tpath.href);
return false;
}
SVG.Path pathObj = (SVG.Path) ref;
Path path = (new PathConverter(pathObj.d)).getPath();
if (pathObj.transform != null)
path.transform(pathObj.transform);
RectF pathBounds = new RectF();
path.computeBounds(pathBounds, true);
bbox.union(pathBounds);
return false;
}
return true;
}
@Override
public boolean doTextContainer(TextContainer obj)
{
if (obj instanceof SVG.TextPath)
{
// Since we cheat a bit with our textPath rendering, we need
// to cheat a bit with our bbox calculation.
SVG.TextPath tpath = (SVG.TextPath) obj;
SVG.SvgObject ref = obj.document.resolveIRI(tpath.href);
if (ref == null) {
error("TextPath path reference '%s' not found", tpath.href);
return false;
}
SVG.Path pathObj = (SVG.Path) ref;
Path path = (new PathConverter(pathObj.d)).getPath();
if (pathObj.transform != null)
path.transform(pathObj.transform);
RectF pathBounds = new RectF();
path.computeBounds(pathBounds, true);
bbox.union(pathBounds);
return false;
}
return true;
}
/**
* Creates a bitmap of the gesture with a transparent background.
*
* @param width
* @param height
* @param inset
* @param color
* @return the bitmap
*/
public Bitmap toBitmap(int width, int height, int inset, int color) {
final Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
final Paint paint = new Paint();
paint.setAntiAlias(BITMAP_RENDERING_ANTIALIAS);
paint.setDither(BITMAP_RENDERING_DITHER);
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(BITMAP_RENDERING_WIDTH);
final Path path = toPath();
final RectF bounds = new RectF();
path.computeBounds(bounds, true);
final float sx = (width - 2 * inset) / bounds.width();
final float sy = (height - 2 * inset) / bounds.height();
final float scale = sx > sy ? sy : sx;
paint.setStrokeWidth(2.0f / scale);
path.offset(-bounds.left + (width - bounds.width() * scale) / 2.0f,
-bounds.top + (height - bounds.height() * scale) / 2.0f);
canvas.translate(inset, inset);
canvas.scale(scale, scale);
canvas.drawPath(path, paint);
return bitmap;
}
private void applyPadding(Path path, RectF textBounds, Rect paddingBounds) {
final Rect viewBounds = getBounds();
float deltaWidth = ((float) paddingBounds.width() / textBounds.width());
float deltaHeight = ((float) paddingBounds.height() / textBounds.height());
float attenuate = (deltaWidth < deltaHeight) ? deltaWidth : deltaHeight;
float textSize = paint.getTextSize();
textSize *= attenuate;
paint.setTextSize(textSize);
paint.getTextPath(String.valueOf(fontCode), 0, 1, 0, viewBounds.height(), path);
path.computeBounds(textBounds, true);
}
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));
}
/**
* Return the actual bounding box of the graphics without transformation
*
* @return
*/
public RectF getOuterBoundingBox() {
if (boundingBox != null) {
Path path = new Path();
path.addRect(boundingBox, Path.Direction.CW);
path.transform(getDisplayMatrix());
RectF box = new RectF();
path.computeBounds(box, true);
return box;
}
return new RectF();
}
/**
* 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;
}
private static void toRectAndAddToRegion(Path p, Region inoutRegion, Rect inoutRect) {
final RectF rectF = new RectF();
p.computeBounds(rectF, false /* unused */);
rectF.round(inoutRect);
inoutRegion.op(inoutRect, Op.UNION);
}
/**计算各种绘制坐标*/
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;
}
}
RNLinearTextGradientSpan(
float[] locations,
int[] colors,
float[] start,
float[] end,
boolean useViewFrame,
Layout layout,
int textStart,
int textEnd,
float maxWidth,
float maxHeight,
String text
) {
if (
start != null &&
end != null &&
colors != null &&
locations != null &&
text != null &&
!YogaConstants.isUndefined(maxWidth) &&
maxWidth != 0 &&
maxHeight != 0 &&
layout != null
) {
Path path = new Path();
RectF bounds = new RectF();
layout.getSelectionPath(textStart, textEnd, path);
path.computeBounds(bounds, true);
if (layout.getLineForOffset(textEnd - 1) != layout.getLineForOffset(textEnd)) {
RectF lastSymbolBounds = new RectF();
layout.getSelectionPath(textEnd - 1, textEnd, path);
path.computeBounds(lastSymbolBounds, true);
if (lastSymbolBounds.contains(bounds) && bounds.contains(lastSymbolBounds)) {
layout.getSelectionPath(textStart, textEnd - 1, path);
path.computeBounds(bounds, true);
}
}
float width = useViewFrame ? maxWidth : bounds.width();
float height = useViewFrame ? maxHeight : bounds.height();
float x0 = (useViewFrame ? 0 : bounds.left) + start[0] * width;
float y0 = (useViewFrame ? 0 : bounds.top) + start[1] * height;
float x1 = (useViewFrame ? 0 : bounds.left) + end[0] * width;
float y1 = (useViewFrame ? 0 : bounds.top) + end[1] * height;
mGradient = new LinearGradient(x0, y0, x1, y1, colors, locations, Shader.TileMode.CLAMP);
}
}
private Box calculatePathBounds(Path path)
{
RectF pathBounds = new RectF();
path.computeBounds(pathBounds, true);
return new Box(pathBounds.left, pathBounds.top, pathBounds.width(), pathBounds.height());
}
private void doLimits(Path path) {
path.computeBounds(rect, false);
doLimits(rect.left, rect.top);
doLimits(rect.right, rect.bottom);
}
private void doLimits(Path path) {
path.computeBounds(rect, false);
doLimits(rect.left, rect.top);
doLimits(rect.right, rect.bottom);
}
private RectF createTextBounds(Path path) {
RectF textBounds = new RectF();
path.computeBounds(textBounds, true);
return textBounds;
}
private void doLimits(Path path) {
path.computeBounds(rect, false);
doLimits(rect.left, rect.top);
doLimits(rect.right, rect.bottom);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
stopUpTimer();
mCurX = (int) event.getX();
mCurY = (int) event.getY();
mPrevX = mCurX;
mPrevY = mCurY;
StrokeMgr.instance().addStroke((short) mCurX, (short) mCurY);
if (StrokeMgr.instance().isBrush) {
drawPenScript(mCurX - 1, mCurY);
drawPenScript(mCurX - 1, mCurY - 1);
drawPenScript(mCurX, mCurY - 1);
drawPenScript(mCurX, mCurY);
}
mPath = new Path();
mPath.moveTo(mCurX, mCurY);
invalidate();
return true;
}
int N = event.getHistorySize();
int x = 0;
int y = 0;
for (int i = 0; i < N; i++) {
x = (int) event.getHistoricalX(i);
y = (int) event.getHistoricalY(i);
drawPoint(mCurX, mCurY, x, y);
mCurX = x;
mCurY = y;
StrokeMgr.instance().addStroke((short) mCurX, (short) mCurY);
}
x = (int) event.getX();
y = (int) event.getY();
drawPoint(mCurX, mCurY, x, y);
mCurX = x;
mCurY = y;
StrokeMgr.instance().addStroke((short) mCurX, (short) mCurY);
if (action == MotionEvent.ACTION_MOVE) {
mPath.computeBounds(mRectF, true);
mRect.set(((int) mRectF.left - mScriptWidth),
((int) mRectF.top - mScriptWidth),
((int) mRectF.right + mScriptWidth),
((int) mRectF.bottom + mScriptWidth));
// Log.v(TAG, "action move, " + mRect.left + ", " + mRect.right + ", " + mRect.top + ", " + mRect.bottom);
if (StrokeMgr.instance().isBrush) {
drawPenScript(mCurX, mCurY);
}
} else if (action == MotionEvent.ACTION_UP) {
StrokeMgr.instance().addStroke((short) -1, (short) 0);
mPath.computeBounds(mRectF, true);
mRect.set(((int) mRectF.left - mScriptWidth),
((int) mRectF.top - mScriptWidth),
((int) mRectF.right + mScriptWidth),
((int) mRectF.bottom + mScriptWidth));
// Log.v(TAG, "action up, " + mRect.left + ", " + mRect.right + ", " + mRect.top + ", " + mRect.bottom);
PathInfo pathInfo = new PathInfo(mPath, mRect);
mPathInfo.add(pathInfo);
fadePoints();
mCurX = -1;
mCurY = 0;
if (StrokeMgr.instance().isBrush) {
drawPenScript(mCurX, mCurY);
}
startUpTimer();
}
invalidate(mRect);
return true;
}