升級簡單繪製進度條,加粗樣式

public class CircleView extends View {
    private static final String TAG = "CircleView";
    private Paint mPaint;
    private int mCenterX;
    private int mCenterY;
    private float ract;
    // 動畫執行的時間
    private final long mDuration = 2000;
    private final long strokewidth = DisplayUtil.dip2px(getContext(),3);//dp -->px
    private ObjectAnimator animator;

    public CircleView(Context context) {
        super(context);
        init();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    // 初始化操作...
    private void init() {
        mPaint = new Paint();
        // 設置鋸齒
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokewidth);
        animator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360.0f);
        animator.setDuration(mDuration);
        animator.setRepeatCount(ObjectAnimator.INFINITE);
        // 線性插值器 勻速旋轉
        animator.setInterpolator(new LinearInterpolator());

    
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mCenterX = getMeasuredWidth() / 2;//控間的寬度
        mCenterY = getMeasuredHeight() / 2;
        ract = mCenterX-strokewidth;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_9));

        canvas.drawCircle(mCenterX, mCenterY, ract, mPaint);

        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.color_3));

        //rectF 外切矩形的坐上角座標和右下角座標
        //startAngle (右邊中點開始爲0度)起始角度
        //  sweepAngle, 從起點開始掃描到截至的中點角度
  /*      RectF oval = new RectF(ract - mCenterX, ract - mCenterX,
                ract + mCenterX, ract + mCenterX);*/
        RectF oval = new RectF(mCenterX-ract, mCenterX-ract,
                ract*2 + strokewidth,  ract*2 + strokewidth);
        canvas.drawArc(oval, -90, 60, false, mPaint);
    }

    public void stop() {
        animator.end();
    }

    public void start() {
        animator.start();
    }
}

 

效果圖

 

github地址:https://github.com/pingcc/MyApplication

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章