通过实现加载动画简单了解Path和PathMeasure,下面演示的是几种效果的动态图:
代码比较简单,这里就直接贴出view的代码:
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import java.text.DecimalFormat;
public class LoadView extends View {
private Paint mPaint;
private int mViewWidth;
private int mViewHeight;
private float mAnimValue; //0-1,计算截取位置
private PathMeasure mMeasure;
private Path mDst;
private float[] pos;
private float[] tan;
private Matrix mMatrix;
private Bitmap mBitMap;
public LoadView(Context context) {
this(context, null);
}
public LoadView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public LoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mViewWidth = w;
mViewHeight = h;
}
private void init() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);
pos = new float[2];
tan = new float[2];
mMatrix = new Matrix();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
mBitMap = BitmapFactory.decodeResource(getResources(), R.mipmap.arrow, options);
Path path = new Path();
mDst = new Path();
//画圆,Path.Direction绘制方向,CW顺时针,CCW逆时针
path.addCircle(0, 0, 100, Path.Direction.CW);
//菱形 (顺时针)
//path.moveTo(-100, 0);//控制开始点
//path.lineTo(0, -100);
//path.lineTo(100, 0);
//path.lineTo(0, 100);
//正方形
// RectF rectF=new RectF();
// rectF.left=-100;
// rectF.right=100;
// rectF.top=100;
// rectF.bottom=-100;
// path.addRect(rectF,Path.Direction.CW);
//第一个参数是被关联的 Path ,第二个参数是用来确保 Path 闭合,如果设置为 true,则不论之前Path是否闭合,都会自动闭合该 Path(如果Path可以闭合的话)。
mMeasure = new PathMeasure(path, true);
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mAnimValue = (float) animation.getAnimatedValue();//循环获取0-1的数据
DecimalFormat fnum = new DecimalFormat("##0.00");//格式化小数点2位
mAnimValue = Float.valueOf(fnum.format(mAnimValue));
postInvalidate();
}
});
valueAnimator.setDuration(3000);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.start();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.translate(mViewWidth / 2, mViewHeight / 2);//显示的view放到中间
//重置,不重置会覆盖
mDst.reset();
mMatrix.reset();
//获取Path绘制总长度
float length = mMeasure.getLength();
float end = length * mAnimValue;
float start = (float) (end - ((0.5 - Math.abs(mAnimValue - 0.5)) * length));
//截取的长度
//参数:起始距离,终点距离,接收截取的path,是否把截取的path移动到起始点。
mMeasure.getSegment(start, end, mDst, true);
canvas.drawPath(mDst, mPaint);
//方案一:自己计算出位置和角度进行平移和旋转
mMeasure.getPosTan(mAnimValue * mMeasure.getLength(), pos, tan);
float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180f / Math.PI);
mMatrix.postRotate(degrees, mBitMap.getWidth() / 2, mBitMap.getHeight() / 2);
mMatrix.postTranslate(pos[0] - mBitMap.getWidth() / 2, pos[1] - mBitMap.getHeight()/2);
// 方法二 :直接使用API进行旋转平移
//mMeasure.getMatrix(length*mAnimValue, mMatrix, PathMeasure.TANGENT_MATRIX_FLAG | PathMeasure.POSITION_MATRIX_FLAG);
//mMatrix.preTranslate(- mBitMap.getWidth() / 2, - mBitMap.getHeight()/2);
canvas.drawBitmap(mBitMap,mMatrix,mPaint);
}
}
注:PathMeasure中有一个方法 getPosTan(float distance, float pos[], float tan[])
distance:指定Path路径某一个点的长度。
第二、三个参数是根据Path上的某个点的长度得到位置和切线。
pos[0]:当前点的X轴坐标。
pos[1]:当前点的Y轴坐标。
tan[0]:当前点的正切点X轴坐标。
tan[1]:当前点的正切点Y轴坐标。