安卓沿Path运动轨迹

当然是先看效果拉~中间那个圆圈,顺着圆圈的轨迹运动。

安卓沿Path运动轨迹_第1张图片

实现一个跟随Path路径运动的效果

思路

1.绘制一个路径
2.得到路径的每个点的坐标
3.用属性动画沿着路径点的坐标运动

实现

1.用
mPathMeasure = new PathMeasure(mPath,false);
mPathMeasure.getPosTan(0, mCurrentPosition, null);

得到每个点的坐标(我是根据  http://blog.csdn.net/tianjian4592/article/details/47067161 这篇博客学习的)

PathMeasure 就是把 Path “拉直”,然后给了我们一个接口(getLength)告诉我们path的总长度,我们再根据具体的长度取得相应坐标

2.属性动画,用的是ValueAniamtor

代码

package com.example.myapp.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

/**
 * Created by zyr
 * DATE: 16-3-7
 * Time: 上午11:52
 * Email: [email protected]
 */
public class HeartView extends View {
    private Paint mPaint;
    private Path mPath;
    private Path mPath2;
    private Paint mPaint2;

    private ValueAnimator valueAnimator;
    private PathMeasure mPathMeasure;
    private float[] mCurrentPosition = new float[2];
    public HeartView(Context context) {
        this(context, null);
    }

    public HeartView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(40);
        mPaint.setStyle(Paint.Style.STROKE);

        mPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint2.setColor(Color.BLUE);
        mPaint2.setTextSize(40);
        mPaint2.setStyle(Paint.Style.FILL);

        mPath = new Path();
        mPath.addCircle(200, 200, 200, Path.Direction.CW);

        mPath2 = new Path();
        mPathMeasure = new PathMeasure(mPath,false);
        mPathMeasure.getPosTan(0, mCurrentPosition, null);
        mPath2.moveTo(mCurrentPosition[0], mCurrentPosition[1]);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速

        valueAnimator = ValueAnimator.ofFloat(0,mPathMeasure.getLength());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (Float)animation.getAnimatedValue();
                mPathMeasure.getPosTan(value, mCurrentPosition, null);
                mPath2.lineTo(mCurrentPosition[0],mCurrentPosition[1]);
                postInvalidate();
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                mPath2 = new Path();
                mPathMeasure = new PathMeasure(mPath,false);
                mPathMeasure.getPosTan(0, mCurrentPosition, null);
                mPath2.moveTo(mCurrentPosition[0], mCurrentPosition[1]);
                if(mPaint2.getColor() ==  Color.BLUE){
                    mPaint2.setColor(Color.YELLOW);
                }else{
                    mPaint2.setColor(Color.BLUE);
                }
            }
        });
        valueAnimator.setDuration(5000);
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.start();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(500,500);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLACK);

        canvas.save();
        canvas.translate(50, 50);
        canvas.drawPath(mPath, mPaint);
        canvas.drawCircle(mCurrentPosition[0], mCurrentPosition[1], 20, mPaint);
        canvas.drawPath(mPath2, mPaint2);
        canvas.restore();

    }
}



你可能感兴趣的:(android)