PathMeasure加载完成

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/CMS111/article/details/59163031
package com.example.cms.testmapshowdetaildemo;

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.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by cms on 2017/3/1.
 */

public class LoadingView extends View {

    private float mStrokeWidth = 10;

    private int loading_duration = 1500;
    private int endind_duration = 600;

    //动画处理
    private Handler mAnimatorHandler;
    private Paint mPaint;
    private Path path_circle;
    private ValueAnimator mLoadingValueAnimator;
    private ValueAnimator.AnimatorUpdateListener mUpdateListener;
    private Animator.AnimatorListener animatorListener;
    private float animatedValue;
    //加载是否结束
    private boolean isOver = false;

    private State mCurrentState = State.NONE;

    private ValueAnimator mEndingAnimator;
    private PathMeasure measure;

    private int count=0;
    private ValueAnimator mSucceedAnimator;
    private Path path_succeed;

    //状态
    public static enum State{
        NONE,
        STARTING,
        LOADING,
        ENDING,
        SUCCEED
    }
    public LoadingView(Context context) {
        this(context,null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        initPath();
        initListener();
        initHandler();
        initAnimator();
        //进入开始动画
        mCurrentState=State.STARTING;

    }
    private void initPaint() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(mStrokeWidth);

    }
    private void initPath() {
        path_circle = new Path();
        path_succeed = new Path();
        measure = new PathMeasure();

        RectF rectF = new RectF(-100,-100,100,100);
        path_circle.addArc(rectF,0,359.9f);

        measure.setPath(path_circle,false);

        path_succeed.moveTo(-65,25);
        path_succeed.lineTo(-10,65);
        path_succeed.lineTo(65,-35);


    }

    private void initListener() {
        mUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animatedValue = ((float) animation.getAnimatedValue());
                invalidate();
            }
        };

        animatorListener = new ValueAnimator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //发消息通知动画状态更新
                mAnimatorHandler.sendEmptyMessage(0);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        };
    }


    private void initHandler() {
        mAnimatorHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (mCurrentState) {
                    case STARTING:
                        //从开始转变为加载状态
                        isOver=false;
                        mCurrentState= State.LOADING;
                        mLoadingValueAnimator.start();
                        break;
                    case LOADING:
                        if (!isOver){
                            //结束条件 isOver = true;
                            count++;
                            if (count>2) {
                                isOver = true;
                            }
                            mLoadingValueAnimator.start();
                        }else {
                            mCurrentState=State.ENDING;
                            mEndingAnimator.start();//开始动画
                        }
                        break;
                    case ENDING:
                        //从结束状态转变为无状态
                        mCurrentState=State.SUCCEED;
                        mEndingAnimator.start();
                        break;
                    case SUCCEED:
                        mCurrentState=State.NONE;
                        break;
                }
            }
        };
    }

    private void initAnimator() {
        mLoadingValueAnimator = ValueAnimator.ofFloat(0,1).setDuration(loading_duration);
        mEndingAnimator = ValueAnimator.ofFloat(0,1).setDuration(endind_duration);
        mSucceedAnimator = ValueAnimator.ofFloat(0,1).setDuration(endind_duration);

        mLoadingValueAnimator.addUpdateListener(mUpdateListener);
        mEndingAnimator.addUpdateListener(mUpdateListener);
        mSucceedAnimator.addUpdateListener(mUpdateListener);

        mLoadingValueAnimator.addListener(animatorListener);
        mEndingAnimator.addListener(animatorListener);
        mSucceedAnimator.addListener(animatorListener);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawLoading(canvas);
    }

    private void drawLoading(Canvas canvas) {
        canvas.translate(getWidth()/2,getHeight()/2);
        canvas.drawColor(Color.GRAY);
        switch (mCurrentState) {
            case NONE:
                canvas.drawPath(path_circle,mPaint);
                break;
            case STARTING:
                mAnimatorHandler.sendEmptyMessage(0);
                break;
            case LOADING:
                measure.setPath(path_circle,false);
                float stop=animatedValue*measure.getLength();
                float start=(float) (stop - ((0.5 - Math.abs(animatedValue - 0.5)) * 200f));

                Path dst = new Path();
                measure.getSegment(start,stop, dst,true);
                canvas.drawPath(dst,mPaint);
                break;
            case ENDING:
                measure.setPath(path_circle,false);
                Path dst2 = new Path();
                measure.getSegment(0,animatedValue*measure.getLength(), dst2,true);
                canvas.drawPath(dst2,mPaint);
                break;
            case SUCCEED:
                canvas.drawPath(path_circle,mPaint);
                measure.setPath(path_succeed,false);
                Path dst3 = new Path();
                measure.getSegment(0,animatedValue*measure.getLength(), dst3,true);
                canvas.drawPath(dst3,mPaint);
                break;
        }
    }

}
展开阅读全文

没有更多推荐了,返回首页