美文网首页
Android 控件之进度加载

Android 控件之进度加载

作者: Nj_第一批老去的90后 | 来源:发表于2017-08-28 14:52 被阅读13次

效果图

Screenshot_2017-08-28-14-50-54-987_com.baidu.lbs..png

public class ComLoadingView extends Button {

    private Paint mPaint;
    private float mWidth = 0f;
    private float mHeight = 0f;
    private float mMaxRadius = 9;
    private float mPadding = 21;
    private int circularCount = 3;
    private int mJumpValue = 0;
    private int mAniTime = 250;
    private int hightColor = 0xFFFFFFFF;
    private int middleColor = 0xCCFFFFFF;
    private int lowColor = 0x44FFFFFF;
    private boolean mIsRest = false;

    private final Handler mHandler = new Handler(Looper.getMainLooper());

    private Runnable mUpdateTimeRunner = new Runnable() {
        @Override
        public void run() {
            mJumpValue--;
            postInvalidate();
            mHandler.removeCallbacks(mUpdateTimeRunner);
            mHandler.postDelayed(mUpdateTimeRunner, mAniTime);
        }
    };

    public ComLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void initPaint(int jumpValue) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        if (jumpValue == 0) {
            mPaint.setColor(hightColor);
        } else if (jumpValue == -1) {
            mPaint.setColor(middleColor);
        } else {
            mPaint.setColor(lowColor);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isEnabled() && mIsRest) {
            float startDistance = mWidth / 2 - mPadding - mMaxRadius * 2;
            for (int i = 0; i < circularCount; i++) {
                int index = (i + mJumpValue) % circularCount;
                initPaint(index);
                canvas.drawCircle(startDistance + i * (mPadding + mMaxRadius * 2), mHeight / 2, mMaxRadius, mPaint);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    public void startAnim() {
        stopAnim();
        mIsRest = true;
        mHandler.post(mUpdateTimeRunner);
    }

    public void stopAnim() {
        if (mHandler != null) {
            mIsRest = false;
            mJumpValue = 0;
            mHandler.removeCallbacks(mUpdateTimeRunner);
            invalidate();
        }
    }

}

相关文章

网友评论

      本文标题:Android 控件之进度加载

      本文链接:https://www.haomeiwen.com/subject/kthgdxtx.html