Android自定义控件:渐进圆形进度条

效果展示

Android自定义控件:渐进圆形进度条_第1张图片
circleprogress.gif

真机效果要比这个好很多

实现十分简单,主要有两点:

1、利用canvasdrawArc方法来绘制白色的圆环
 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        int width = getWidth(), height = getHeight();
        int sr = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);


        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(circleColor);
        paint.setStrokeWidth(circleWidth);
        paint.setStyle(Paint.Style.STROKE);

        int size = Math.min(width, height) - circleWidth;
        int left = (width - size) / 2, top = (height - size) / 2;
        RectF rectF = new RectF(left, top, left + size, top + size);

        canvas.drawArc(rectF, mDegress
                , 360 - mDegress < MaxDegress ? 360 - mDegress : Math.min(mDegress, MaxDegress)
                , false, paint);

        canvas.restoreToCount(sr);
        canvas.restore();
    }
2、利用ValueAnimator来控制弧形圆环的起始位置,同时利用其动画时间和动画插值器控制圆环的旋转速度和旋转效果
       ValueAnimator.AnimatorUpdateListener animatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                mDegress = value;
                invalidate();
            }
        };
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
        valueAnimator.addUpdateListener(animatorUpdateListener);
        valueAnimator.setDuration(1600);
        valueAnimator.setInterpolator(new DecelerateInterpolator(0.6f));
        valueAnimator.setRepeatCount(-1);
        valueAnimator.start();

完整代码:

package com.hpplay.muiltythreaddemo.progress;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

/**
 * Created by DON on 2017/7/26.
 */

public class CircleProgress extends View {

    private Context mContext;
    private int mDegress = 360;
    private int MaxDegress = 120;
    private int circleWidth = 10;
    private int circleColor = Color.WHITE;

    public CircleProgress(Context context) {
        this(context, null, 0);
    }

    public CircleProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mContext = getContext();

        ValueAnimator.AnimatorUpdateListener animatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                mDegress = value;
                invalidate();
            }
        };
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
        valueAnimator.addUpdateListener(animatorUpdateListener);
        valueAnimator.setDuration(1600);
        valueAnimator.setInterpolator(new DecelerateInterpolator(0.6f));
        valueAnimator.setRepeatCount(-1);
        valueAnimator.start();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        int width = getWidth(), height = getHeight();
        int sr = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);


        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(circleColor);
        paint.setStrokeWidth(circleWidth);
        paint.setStyle(Paint.Style.STROKE);

        int size = Math.min(width, height) - circleWidth;
        int left = (width - size) / 2, top = (height - size) / 2;
        RectF rectF = new RectF(left, top, left + size, top + size);

        canvas.drawArc(rectF, mDegress
                , 360 - mDegress < MaxDegress ? 360 - mDegress : Math.min(mDegress, MaxDegress)
                , false, paint);

        canvas.restoreToCount(sr);
        canvas.restore();
    }


    public int getCircleWidth() {
        return circleWidth;
    }

    public void setCircleWidth(int circleWidth) {
        this.circleWidth = circleWidth;
    }

    public int getCircleColor() {
        return circleColor;
    }

    public void setCircleColor(int circleColor) {
        this.circleColor = circleColor;
    }
}

使用示例:

新建CircleProgressFragment

package com.hpplay.muiltythreaddemo.progress;

import android.graphics.Color;
import android.os.Bundle;
import android.sax.RootElement;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * Created by DON on 2017/7/26.
 */

public class CircleProgressFragment extends Fragment{

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        RelativeLayout relativeLayout = new RelativeLayout(getActivity());
        relativeLayout.setBackgroundColor(Color.BLACK);
        return relativeLayout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RelativeLayout rootLayout = (RelativeLayout)view;
        CircleProgress circleProgress = new CircleProgress(getActivity());
        circleProgress.setBackgroundColor(Color.TRANSPARENT);
        RelativeLayout.LayoutParams circleParams = new RelativeLayout.LayoutParams(
                200, 200);
        circleParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        rootLayout.addView(circleProgress,circleParams);

    }

}

你可能感兴趣的:(Android自定义控件:渐进圆形进度条)