下面列出了android.graphics.PathDashPathEffect#android.graphics.PathMeasure 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void matchVertsToPath(Path path, float bottomCoord, float extraOffset) {
PathMeasure pm = new PathMeasure(path, false);
for (int i = 0; i < staticVerts.length / 2; i++) {
float yIndexValue = staticVerts[i * 2 + 1];
float xIndexValue = staticVerts[i * 2];
float percentOffsetX = (0.000001f + xIndexValue) / bitmap.getWidth();
float percentOffsetX2 = (0.000001f + xIndexValue) / (bitmap.getWidth() + extraOffset);
percentOffsetX2 += pathOffsetPercent;
pm.getPosTan(pm.getLength() * (1f - percentOffsetX), coords, null);
pm.getPosTan(pm.getLength() * (1f - percentOffsetX2), coords2, null);
if (yIndexValue == 0) {
setXY(drawingVerts, i, coords[0], coords2[1]);
} else {
float desiredYCoord = bottomCoord;
setXY(drawingVerts, i, coords[0], desiredYCoord);
}
}
}
public FloatParticle(int width, int height) {
mWidth = width;
mHeight = height;
mRandom = new Random();
startPoint = new Point((int) (mRandom.nextFloat() * mWidth), (int) (mRandom.nextFloat() * mHeight));
// 抗锯齿
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);
// 防抖动
mPaint.setDither(true);
mPaint.setStyle(Paint.Style.FILL);
// 设置模糊效果 边缘模糊
mPaint.setMaskFilter(new BlurMaskFilter(BLUR_SIZE, BlurMaskFilter.Blur.SOLID));
mPath = new Path();
mPathMeasure = new PathMeasure();
startPoint.x = (int) (mRandom.nextFloat() * mWidth);
startPoint.y = (int) (mRandom.nextFloat() * mHeight);
}
/**
* calculate and set position to menu items
*/
private void calculateMenuItemPosition() {
float itemRadius = (expandedRadius + collapsedRadius) / 2, f;
RectF area = new RectF(
center.x - itemRadius,
center.y - itemRadius,
center.x + itemRadius,
center.y + itemRadius);
Path path = new Path();
path.addArc(area, (float) fromAngle, (float) (toAngle - fromAngle));
PathMeasure measure = new PathMeasure(path, false);
float len = measure.getLength();
int divisor = getChildCount();
float divider = len / divisor;
for (int i = 0; i < getChildCount(); i++) {
float[] coords = new float[2];
measure.getPosTan(i * divider + divider * .5f, coords, null);
FilterMenu.Item item = (FilterMenu.Item) getChildAt(i).getTag();
item.setX((int) coords[0] - item.getView().getMeasuredWidth() / 2);
item.setY((int) coords[1] - item.getView().getMeasuredHeight() / 2);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
float size = Math.min(w, h) - tableWidth * 2;
//油表的位置方框
mTableRectF = new RectF(0, 0, size, size);
mPath.reset();
//在油表路径中增加一个从起始弧度
mPath.addArc(mTableRectF, 60, 240);
//计算路径的长度
PathMeasure pathMeasure = new PathMeasure(mPath, false);
float length = pathMeasure.getLength();
float step = length / 60;
dashPathEffect = new DashPathEffect(new float[]{step / 3, step * 2 / 3}, 0);
float radius = size / 2;
mColorShader = new SweepGradient(radius, radius,SWEEP_GRADIENT_COLORS,null);
//设置指针的路径位置
mPointerPath.reset();
mPointerPath.moveTo(radius, radius - 20);
mPointerPath.lineTo(radius, radius + 20);
mPointerPath.lineTo(radius * 2 - tableWidth, radius);
mPointerPath.close();
}
private void initProgressBar() {
basePath = new Path();
secPath = new Path();
pathMeasure = new PathMeasure();
setPaints();
xVertiCoord = new float[sideCount];
yVertiCoord = new float[sideCount];
x1VertiCoord = new float[sideCount];
y1VertiCoord = new float[sideCount];
x2VertiCoord = new float[sideCount];
y2VertiCoord = new float[sideCount];
xMidPoints = new float[sideCount];
yMidPoints = new float[sideCount];
}
public TextPathAnimView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextPathAnimView, defStyle, 0);
mAnimDuration = typedArray.getInt(R.styleable.TextPathAnimView_duration, 1500);
mTextBgColor = typedArray.getColor(R.styleable.TextPathAnimView_text_bg_color, Color.BLACK);
mTextFgColor = typedArray.getColor(R.styleable.TextPathAnimView_text_fg_color, Color.WHITE);
mIsLoop = typedArray.getBoolean(R.styleable.TextPathAnimView_loop, true);
String contentText = typedArray.getString(R.styleable.TextPathAnimView_text);
float sizeScale = typedArray.getFloat(R.styleable.TextPathAnimView_text_size_scale, dp2px(14));
float stokeWidth = typedArray.getFloat(R.styleable.TextPathAnimView_text_stoke_width, 3f);
float textInterval = typedArray.getDimension(R.styleable.TextPathAnimView_text_interval, dp2px(5));
typedArray.recycle();
mSourceTextPath = new TextPath(contentText, sizeScale, textInterval);
mSourcePath = mSourceTextPath.getPath();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(stokeWidth);
mAnimPath = new Path();
mPathMeasure = new PathMeasure();
}
/**
* Make sure it can be seen in "FILL" mode
*/
private void warpStraightLines() {
PathMeasure pmTemp = new PathMeasure();
for (int i = 0; i < mConfig.complexity; i++) {
if(lineRifts[i].isStraight())
{
pmTemp.setPath(lineRifts[i], false);
lineRifts[i].setStartLength(pmTemp.getLength() / 2);
float[] pos = new float[2];
pmTemp.getPosTan(pmTemp.getLength() / 2, pos, null);
int xRandom = (int) (pos[0] + Utils.nextInt(-Utils.dp2px(1), Utils.dp2px(1)));
int yRandom = (int) (pos[1] + Utils.nextInt(-Utils.dp2px(1), Utils.dp2px(1)));
lineRifts[i].reset();
lineRifts[i].moveTo(0,0);
lineRifts[i].lineTo(xRandom,yRandom);
lineRifts[i].lineToEnd();
}
}
}
List<Path> pathsFromComplexPath(Path p)
{
List<Path>pathList = new ArrayList<>();
PathMeasure pm = new PathMeasure(p,false);
Boolean fin = false;
while (!fin)
{
float len = pm.getLength();
if (len > 0)
{
Path np = new Path();
pm.getSegment(0,len,np,true);
pathList.add(np);
}
fin = !pm.nextContour();
}
return pathList;
}
public OBGroup splitPath (OBPath obp)
{
int lengthPerSplit = 100;
PathMeasure pm = new PathMeasure(obp.path(), false);
float splen = pm.getLength();
int noSplits = (int) (splen / lengthPerSplit);
List<OBPath> newOBPaths = splitInto(obp, pm, noSplits, 1.0f / (noSplits * 4));
for (OBPath newOBPath : newOBPaths)
{
//newOBPath.setBounds(obp.bounds);
//newOBPath.setPosition(obp.position());
//newOBPath.sizeToBox(obp.bounds());
newOBPath.setStrokeColor(Color.argb((int) (255 * 0.4f), 255, 0, 0));
newOBPath.setLineJoin(OBStroke.kCALineJoinRound);
newOBPath.setFillColor(0);
newOBPath.setLineWidth(swollenLineWidth);
}
MainActivity.log("Now serving " + newOBPaths.size());
OBGroup grp = new OBGroup((List<OBControl>) (Object) newOBPaths);
return grp;
}
private void init(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
height = (int) (wm.getDefaultDisplay().getHeight() * 3 / 2f);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(2);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
pathMeasure = new PathMeasure();
builderFollower(fllowerCount, fllowers1);
builderFollower(fllowerCount, fllowers2);
builderFollower(fllowerCount, fllowers3);
}
public PathInterpolatorDonut(Path path) {
final PathMeasure pathMeasure = new PathMeasure(path, false /* forceClosed */);
final float pathLength = pathMeasure.getLength();
final int numPoints = (int) (pathLength / PRECISION) + 1;
mX = new float[numPoints];
mY = new float[numPoints];
final float[] position = new float[2];
for (int i = 0; i < numPoints; ++i) {
final float distance = (i * pathLength) / (numPoints - 1);
pathMeasure.getPosTan(distance, position, null /* tangent */);
mX[i] = position[0];
mY[i] = position[1];
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRect = new RectF(getWidth() / 2 - RADIUS, PADDING, getWidth() / 2 + RADIUS, getHeight() - PADDING);
// 刻度的数量
Path arcPath = new Path();
arcPath.addArc(mRect, START_ANAGLE, 360 - (START_ANAGLE - 45));
PathMeasure pathMeasure = new PathMeasure(arcPath, false);
float pathMeasureLength = pathMeasure.getLength();
// 短刻度
mShortEffect = new PathDashPathEffect(mShortDash,
(pathMeasureLength - Utils.dp2px(2)) / 50,// 每个间距为多少
0,// 第一个从什么地方开始
PathDashPathEffect.Style.ROTATE);
// 长刻度
mLongEffect = new PathDashPathEffect(mLongDash,
(pathMeasureLength - Utils.dp2px(2)) / 10,// 每个间距为多少
0,// 第一个从什么地方开始
PathDashPathEffect.Style.ROTATE);
}
private void initRoomTempPath(float[] data) {
mRoomTempPath.reset();
// Path path = new Path();
float pointX;
float pointY;
// 横向
mRoomTempPath.moveTo(Xpoint + xFirstPointOffset,
getDataY(data[0], Ylabel));
mRoomTempPath.moveTo(Xpoint + xFirstPointOffset,
getDataY(data[0], Ylabel));
for (int i = 0; i < Xlabel.length; i++) {
float startX = Xpoint + i * Xscale + xFirstPointOffset;
// 绘制数据连线
if (i != 0) {
pointX = Xpoint + (i - 1) * Xscale + xFirstPointOffset;
pointY = getDataY(data[i - 1], Ylabel);
mRoomTempPath.lineTo(pointX, pointY);
}
if (i == Xlabel.length - 1) {
pointX = startX;
pointY = getDataY(data[i], Ylabel);
mRoomTempPath.lineTo(pointX, pointY);
}
}
mRoomTempPathMeasure = new PathMeasure(mRoomTempPath, false);
}
public void matchVertsToPath(Path path, float extraOffset) {
PathMeasure pm = new PathMeasure(path, false);
int index = 0;
for (int i = 0; i < staticVerts.length / 2; i++) {
float yIndexValue = staticVerts[i * 2 + 1];
float xIndexValue = staticVerts[i * 2];
float percentOffsetX = (0.000001f + xIndexValue) / bitmap.getWidth();
float percentOffsetX2 = (0.000001f + xIndexValue) / (bitmap.getWidth() + extraOffset);
percentOffsetX2 += pathOffsetPercent;
pm.getPosTan(pm.getLength() * (1f - percentOffsetX), coords, null);
pm.getPosTan(pm.getLength() * (1f - percentOffsetX2), coords2, null);
if (yIndexValue == 0) {
setXY(drawingVerts, i, coords[0], coords2[1] + verticalOffset);
} else {
float desiredYCoord = Math.max(coords2[1], coords2[1] + easedFoamCoords[Math.min(easedFoamCoords.length - 1, index)]);
setXY(drawingVerts, i, coords[0], desiredYCoord + verticalOffset);
index += 1;
}
}
}
public FreshDownloadView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
circular_edge = getResources().getDimension(R.dimen.edge);
bounds = new Rect();
mTempBounds = new RectF();
publicPaint = new Paint();
path1 = new Path();
path2 = new Path();
path3 = new Path();
pathMeasure1 = new PathMeasure();
pathMeasure2 = new PathMeasure();
pathMeasure3 = new PathMeasure();
textBounds = new Rect();
parseAttrs(context.obtainStyledAttributes(attrs, R.styleable.FreshDownloadView));
initPaint();
}
public SunnyType(Context context, ShortWeatherInfo info) {
super(context);
mPathFront = new Path();
mPathRear = new Path();
sunPath = new Path();
mPaint = new Paint();
pos = new float[2];
tan = new float[2];
mMatrix = new Matrix();
measure = new PathMeasure();
sunMeasure = new PathMeasure();
currentSunPosition = TimeUtils.getTimeDiffPercent(info.getSunrise(), info.getSunset());
currentMoonPosition = TimeUtils.getTimeDiffPercent(info.getMoonrise(), info.getMoonset());
if (currentSunPosition >= 0 && currentSunPosition <= 1) {
setColor(colorDay);
boat = decodeResource(boat, R.drawable.ic_boat_day);
} else {
setColor(colorNight);
boat = decodeResource(boat, R.drawable.ic_boat_night);
}
cloud = decodeResource(cloud, R.drawable.ic_cloud);
}
public RainType(Context context, @RainLevel int rainLevel, @WindLevel int windLevel) {
super(context);
setColor(0xFF6188DA);
this.rainLevel = rainLevel;
this.windLevel = windLevel;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);
mRains = new ArrayList<>();
mSnows = new ArrayList<>();
matrix = new Matrix();
bitmap = decodeResource(bitmap, R.drawable.ic_rain_ground);
mDstFlash1 = new Path();
flashPathMeasure1 = new PathMeasure();
mDstFlash2 = new Path();
flashPathMeasure2 = new PathMeasure();
mDstFlash3 = new Path();
flashPathMeasure3 = new PathMeasure();
}
/**
* 画指示器
*
* @param canvas
*/
private void drawIndicator(Canvas canvas) {
PathMeasure mPathMeasure = new PathMeasure();
mPathMeasure.setPath(mArcPath, false);
float[] tan = new float[2];
float[] pos = new float[2];
mPathMeasure.getPosTan(mPathMeasure.getLength() * (0.5f), pos, tan);
canvas.save();
double angle = calcArcAngle(Math.atan2(tan[1], tan[0])) + 90;
canvas.rotate((float) angle, pos[0], pos[1]);
//画直线
canvas.drawLine(pos[0], pos[1], pos[0] + 80, pos[1], mIndicatorPaint);
Path linePath = new Path();
//画箭头
linePath.moveTo(pos[0] + 80, pos[1] - 20);
linePath.lineTo(pos[0] + 80 + 20, pos[1]);
linePath.lineTo(pos[0] + 80, pos[1] + 20);
canvas.drawPath(linePath, mIndicatorPaint);
canvas.restore();
}
public PathInterpolatorBase(Path path) {
final PathMeasure pathMeasure = new PathMeasure(path, false /* forceClosed */);
final float pathLength = pathMeasure.getLength();
final int numPoints = (int) (pathLength / PRECISION) + 1;
mX = new float[numPoints];
mY = new float[numPoints];
final float[] position = new float[2];
for (int i = 0; i < numPoints; ++i) {
final float distance = (i * pathLength) / (numPoints - 1);
pathMeasure.getPosTan(distance, position, null /* tangent */);
mX[i] = position[0];
mY[i] = position[1];
}
}
private void init(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
width = wm.getDefaultDisplay().getWidth();
// height = wm.getDefaultDisplay().getHeight();
height = (int) (wm.getDefaultDisplay().getHeight() * 3 / 2f);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(2);
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Paint.Style.STROKE);
pathMeasure = new PathMeasure();
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_heart);
builderFollower(fllowerCount, fllowers1);
builderFollower(fllowerCount, fllowers2);
builderFollower(fllowerCount, fllowers3);
}
protected PathPoints[] getPoints(Path path, int size) {
//Size of 100 indicates that, 100 points
// would be extracted from the path
PathPoints[] pointArray = new PathPoints[size];
PathMeasure pm = new PathMeasure(path, false);
float length = pm.getLength();
float distance = 0f;
float speed = length / size;
int counter = 0;
float[] aCoordinates = new float[2];
while ((distance < length) && (counter < size)) {
pm.getPosTan(distance, aCoordinates, null);
pointArray[counter] = new PathPoints(aCoordinates[0], aCoordinates[1]);
counter++;
distance = distance + speed;
}
return pointArray;
}
/**
* 画折线
*
* @param canvas
*/
public void drawLines(Canvas canvas, Line line) {
if (line != null && isShow) {
linePaint.setColor(line.getLineColor());
linePaint.setStrokeWidth(LeafUtil.dp2px(mContext, line.getLineWidth()));
linePaint.setStyle(Paint.Style.STROKE);
List<PointValue> values = line.getValues();
Path path = line.getPath();
int size = values.size();
for (int i = 0; i < size; i++) {
PointValue point = values.get(i);
if (i == 0) path.moveTo(point.getOriginX(), point.getOriginY());
else path.lineTo(point.getOriginX(), point.getOriginY());
}
measure = new PathMeasure(path, false);
linePaint.setPathEffect(createPathEffect(measure.getLength(), phase, 0.0f));
canvas.drawPath(path, linePaint);
}
}
private void buildPathData() {
SvgPathParser parser = getPathParser();
pathData = new PathData();
try {
pathData.path = parser.parsePath(svgPath);
} catch (ParseException e) {
pathData.path = new Path();
}
PathMeasure pm = new PathMeasure(pathData.path, true);
while (true) {
pathData.length = Math.max(pathData.length, pm.getLength());
if (!pm.nextContour()) {
break;
}
}
}
@Override
protected void onSizeChanged(final int w, final int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
// Scale and translate the SVG path.
Matrix matrix = new Matrix();
RectF rectF = new RectF();
mPath.computeBounds(rectF, false);
RectF largeRectF = new RectF(0, 0, w * SCALE_FACTOR, h * SCALE_FACTOR);
matrix.setRectToRect(rectF, largeRectF, Matrix.ScaleToFit.CENTER);
float pathTranslationY = getResources().getDimension(R.dimen.path_translation_y);
float pathTranslationX = getResources().getDimension(R.dimen.path_translation_x);
matrix.postTranslate(pathTranslationX, pathTranslationY);
mPath.transform(matrix);
mPathMeasure = new PathMeasure(mPath, false);
mLength = mPathMeasure.getLength();
if (mProgress != 0) {
// Animate the restored frame.
animate(mProgress);
}
}
/**
* 绘制对勾
*/
private void initOk() {
//对勾的路径
path.moveTo(default_all_distance + height / 8 * 3, height / 2);
path.lineTo(default_all_distance + height / 2, height / 5 * 3);
path.lineTo(default_all_distance + height / 3 * 2, height / 5 * 2);
pathMeasure = new PathMeasure(path, true);
}
public void setRadius(int radius) {
myRadius = radius;
//对勾的路径
int cHeight = radius * 2;
path.moveTo(+cHeight / 8 * 3, cHeight / 2);
path.lineTo(+cHeight / 2, cHeight / 5 * 3);
path.lineTo(+cHeight / 3 * 2, cHeight / 5 * 2);
pathMeasure = new PathMeasure(path, true);
invalidate();
}
@Test
public void testRegularStarPolygonNoOutlinePerimeter() {
Path path = Paths.regularStarPolygon(0, 0, 100, 100, 5, 2, 0, false);
PathMeasure pm = new PathMeasure(path, false);
// Perimeter of non-outlined five pointed star inscribed in circle with diameter 100
float expectedPerimeter = 475.53f;
assertEquals(expectedPerimeter, pm.getLength(), 0.01f);
}
/**
* Sets the Path defining a pattern of motion between two coordinates.
* The pattern will be translated, rotated, and scaled to fit between the start and end points.
* The pattern must not be empty and must have the end point differ from the start point.
*
* @param patternPath A Path to be used as a pattern for two-dimensional motion.
* @attr ref android.R.styleable#PatternPathMotion_patternPathData
*/
public void setPatternPath(Path patternPath) {
PathMeasure pathMeasure = new PathMeasure(patternPath, false);
float length = pathMeasure.getLength();
float[] pos = new float[2];
pathMeasure.getPosTan(length, pos, null);
float endX = pos[0];
float endY = pos[1];
pathMeasure.getPosTan(0, pos, null);
float startX = pos[0];
float startY = pos[1];
if (startX == endX && startY == endY) {
throw new IllegalArgumentException("pattern must not end at the starting point");
}
mTempMatrix.setTranslate(-startX, -startY);
float dx = endX - startX;
float dy = endY - startY;
float distance = (float) Math.hypot(dx, dy);
float scale = 1 / distance;
mTempMatrix.postScale(scale, scale);
double angle = Math.atan2(dy, dx);
mTempMatrix.postRotate((float) Math.toDegrees(-angle));
patternPath.transform(mTempMatrix, mPatternPath);
mOriginalPatternPath = patternPath;
}
private void init(Context context) {
animator = new TimeAnimator();
animator.setTimeListener(this);
paint = new Paint();
paint.setColor(Color.WHITE);
density = getResources().getDisplayMetrics().density;
path = new Path();
pathMeasure = new PathMeasure();
position = new PointF();
velocity = new PointF();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int move=w/4;
RectF rectF=new RectF(move,move,move*3,move*3);
mPath.addArc(rectF,45,360);
mPathMeasure=new PathMeasure(mPath,true);
}