PathMeasure從名字就可以看出株茶,這個(gè)類應(yīng)該關(guān)聯(lián)一個(gè)path類對(duì)象。它是對(duì)它關(guān)聯(lián)path對(duì)象的進(jìn)一步測(cè)量笛洛。所以PathMeasure與Path是一一對(duì)應(yīng)的畅蹂,一個(gè)PathMeasure對(duì)象如果沒(méi)有與他關(guān)聯(lián)的Path對(duì)象垒拢,那么這個(gè)PathMeasure也就沒(méi)什么存在的意義了旬迹。它提供了測(cè)量path長(zhǎng)度,返回一個(gè)點(diǎn)的坐標(biāo)和正切值等方法求类。
-
構(gòu)造函數(shù)(Constructors)
- 無(wú)參構(gòu)造函數(shù):僅僅是構(gòu)造了一個(gè)PathMeasure對(duì)象奔垦,并沒(méi)有關(guān)聯(lián)任何path。要想使用它可以調(diào)用setPath傳入想要關(guān)聯(lián)的path尸疆。
- 有參構(gòu)造函數(shù):接收一個(gè)path對(duì)象與之關(guān)聯(lián)椿猎,這個(gè)path對(duì)象就是我們想要測(cè)量的實(shí)體惶岭。第二個(gè)boolean類型的forceClosed的意義。如果傳入的是true犯眠,那么即使傳入的path不是閉合的按灶,那么也強(qiáng)制認(rèn)為它的閉合的。如果是false筐咧,就不會(huì)又任何變化鸯旁。一般使用的時(shí)候都會(huì)傳入false。
-
公共方法
- getLength:返回關(guān)聯(lián)path當(dāng)前輪廓的長(zhǎng)度量蕊,如果沒(méi)有關(guān)聯(lián)path铺罢,將會(huì)返回0;
- getMatrix:傳入的distance要大于0残炮,并且小于path當(dāng)前輪廓的長(zhǎng)度韭赘。它會(huì)計(jì)算distance在Path上對(duì)應(yīng)的點(diǎn)的信息,并將信息封裝到傳入的martrix中势就。通過(guò)返回的boolean值可以判斷計(jì)算是否成功泉瞻。
- getPosTan:傳入的distance要大于0,并且小于path當(dāng)前輪廓的長(zhǎng)度苞冯。它會(huì)計(jì)算distance在Path上對(duì)應(yīng)的點(diǎn)的信息瓦灶,并將信息封裝到float[] pos和float[] tan中,他們分別是一個(gè)長(zhǎng)度為2的數(shù)組抱完,數(shù)組中index=0是x方向上的信息贼陶,index=1是y方向上的信息。通過(guò)返回的boolean值可以判斷計(jì)算是否成功巧娱。
- getSegment:startD和stopD分別是起始距離和終止距離碉怔,此方法會(huì)裁剪取出他們中間的path放到dst中。startD和stopD都應(yīng)該是合法的值禁添,否則返回false撮胧。裁剪出來(lái)的path長(zhǎng)度為0也會(huì)返回false,如果是正常的情況老翘,就會(huì)返回true芹啥。startWithMoveTo為true表示截取到的path的第一個(gè)點(diǎn)不變。如果為false铺峭,截取到的path的起點(diǎn)會(huì)連接到dst之前到的最后一個(gè)點(diǎn)墓怀。
- isClosed:當(dāng)前輪廓是否是閉合的
- nextContour:移動(dòng)到path中的下一個(gè)輪廓上
- setPath:設(shè)置與之有關(guān)的path.第二個(gè)boolean類型的forceClosed的意義。如果傳入的是true卫键,那么即使傳入的path不是閉合的傀履,那么也強(qiáng)制認(rèn)為它的閉合的。如果是false莉炉,就不會(huì)又任何變化钓账。一般使用的時(shí)候都會(huì)傳入false碴犬。
方法就這么幾個(gè),而且很簡(jiǎn)單梆暮。接下來(lái)利用這幾個(gè)方法服协,來(lái)做兩個(gè)炫酷的效果。
旋轉(zhuǎn)小飛機(jī)
public class PlaneView extends View{
private Paint mPaint;
private int mWidth,mHeight;
private Path mPath;
private Bitmap mBitmap;
private int mBitmapWidth,mBitmapHeight;
private Matrix mMatrix;
private float[] pos;
private float[] tan;
private PathMeasure pathMeasure;
private float totalLength,currentLength;
public PlaneView(Context context) {
this(context,null);
}
public PlaneView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public PlaneView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
pos = new float[2];
tan = new float[2];
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStrokeWidth(4);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.RED);
mPath = new Path();
// 獲取縮放的小飛機(jī)圖片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow,options);
mBitmapWidth = mBitmap.getWidth();
mBitmapHeight = mBitmap.getHeight();
mMatrix = new Matrix();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = getWidth();
mHeight = getHeight();
mPath.addCircle(mWidth/2,mHeight/2,220, Path.Direction.CW);
// 創(chuàng)建一個(gè)PathMeasure與Path關(guān)聯(lián)
pathMeasure = new PathMeasure(mPath,false);
// 獲取Path的總長(zhǎng)度
totalLength = pathMeasure.getLength();
}
@Override
protected void onDraw(Canvas canvas) {
// 畫坐標(biāo)軸
canvas.drawLine(mWidth/2,0,mWidth/2,mHeight,mPaint);
canvas.drawLine(0,mHeight/2,mWidth,mHeight/2,mPaint);
// 畫圓圈
canvas.drawPath(mPath,mPaint);
// 獲取當(dāng)前點(diǎn)的信息
boolean posTan = pathMeasure.getPosTan(currentLength, pos, tan);
if (posTan){
mMatrix.reset();
// 計(jì)算圖片要旋轉(zhuǎn)的角度
float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI);
// 旋轉(zhuǎn)圖片
mMatrix.postRotate(degrees,mBitmapWidth / 2,mBitmapHeight / 2);
// 將圖片中心繪制到當(dāng)前點(diǎn)
mMatrix.postTranslate(pos[0] - mBitmapWidth / 2,pos[1] - mBitmapHeight / 2);
canvas.drawBitmap(mBitmap,mMatrix,mPaint);
currentLength += 5;
if (currentLength >= totalLength){
currentLength = 0.0f;
}
invalidate();
}
}
}
一個(gè)正在加載中的小特效
public class CircleView extends View {
private int mWidth,mHeight;
private Path mPath,mDst;
private Paint mPaint;
private PathMeasure pathMeasure;
private float animatedValue;
public CircleView(Context context) {
this(context,null);
}
public CircleView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPath = new Path();
mDst = new Path();
// 執(zhí)行動(dòng)畫
final ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1);
valueAnimator.setDuration(2000);
valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
valueAnimator.start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
animatedValue = (float) animation.getAnimatedValue();
invalidate();
}
});
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mWidth = getWidth();
mHeight = getHeight();
mPath.addCircle(mWidth / 2,mHeight /2,220, Path.Direction.CW);
pathMeasure = new PathMeasure(mPath,false);
}
@Override
protected void onDraw(Canvas canvas) {
mDst.reset();
// 計(jì)算新path的終點(diǎn)位置
float stop = pathMeasure.getLength() * animatedValue;
// 計(jì)算新path的起點(diǎn)位置
float start = (float) (stop - ((0.5 - Math.abs(animatedValue - 0.5)) * pathMeasure.getLength()));
// 根據(jù)計(jì)算的值截取出新Path
boolean segment = pathMeasure.getSegment(start, stop, mDst, true);
if (segment){
canvas.drawPath(mDst,mPaint);
}
}
}
小船隨波逐流
public class WaveView extends View {
private static int DRAW_BOAT_OFFSET = 15;
private Bitmap bitmap;
private int mWaveLength;
private Path mPath;
private int mHeight;
private int mWidth;
private Paint mPaint;
private Matrix mMatrix;
private PathMeasure pathMeasure;
private float animatedValue;
public WaveView(Context context) {
this(context,null);
}
public WaveView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
// 加載小船圖片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.timg, options);
// 初始化需要用到的變量
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.argb(105,0,0,255));
mPaint.setStyle(Paint.Style.FILL);
mPath = new Path();
mMatrix = new Matrix();
pathMeasure = new PathMeasure();
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mWidth = getWidth();
mHeight = getHeight();
mWaveLength = mWidth / 2;
}
@Override
protected void onDraw(Canvas canvas) {
mPath.reset();
// 根據(jù)動(dòng)畫的進(jìn)行進(jìn)度設(shè)置波浪形的path路徑啦粹,并繪制波浪
mPath.moveTo(-mWaveLength+(mWaveLength/2)*animatedValue,mHeight/2);
for (int i = -mWaveLength; i < mWidth + mWaveLength; i+=mWaveLength) {
mPath.rQuadTo(mWaveLength / 2 +(mWaveLength/2)*animatedValue,
60,
mWaveLength+(mWaveLength/2)*animatedValue,
0);
mPath.rQuadTo(mWaveLength / 2 +(mWaveLength/2)*animatedValue,
-60,
mWaveLength+(mWaveLength/2)*animatedValue,
0);
}
mPath.lineTo(mWidth,mHeight);
mPath.lineTo(0,mHeight);
mPath.close();
canvas.drawPath(mPath,mPaint);
// 因?yàn)槊看卫L制的path可能都不同蚯涮,所以每次都為pathMeasure設(shè)置path
pathMeasure.setPath(mPath,false);
// 根據(jù)動(dòng)畫進(jìn)行的進(jìn)度取出當(dāng)前長(zhǎng)度的matrix
boolean matrix = pathMeasure.getMatrix(animatedValue * pathMeasure.getLength(), mMatrix,
PathMeasure.TANGENT_MATRIX_FLAG | PathMeasure.POSITION_MATRIX_FLAG);
if (matrix){
// 操作matrix,繪制小船
mMatrix.preTranslate(-bitmap.getWidth() / 2,-bitmap.getHeight() + DRAW_BOAT_OFFSET);
canvas.drawBitmap(bitmap,mMatrix,null);
}
}
// 開(kāi)啟動(dòng)畫
public void startAnimator(){
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,1);
valueAnimator.setDuration(15000);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setRepeatCount(INFINITE);
valueAnimator.start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
animatedValue = (float) animation.getAnimatedValue();
invalidate();
}
});
}
}