美文网首页
Android 自定义进度条控件,仿IOS App Store下

Android 自定义进度条控件,仿IOS App Store下

作者: 逗笔小新 | 来源:发表于2019-08-23 15:13 被阅读0次

    仿IOS App Store的下载进度,效果如下图:

    效果图

    主要是通过自定义View绘制。细分下来,画2个同心圆,1段圆弧,中心的停止图片是1个圆角正方形。

    配置属性,在attrs.xml文件上

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- 仿IOS AppStore下载进度条 -->
        <declare-styleable name="CircleProgressView">
            <!-- 进度条宽度 -->
            <attr name ="progress_width" format="dimension"/>
            <!-- 背景颜色 -->
            <attr name="circle_background_color" format="color"/>
            <!-- 进度条背景颜色 -->
            <attr name="progress_background_color" format="color"/>
            <!-- 进度条颜色 -->
            <attr name="progress_color" format="color"/>
            <!-- 中心图标大小 -->
            <attr name="center_icon_width" format="dimension"/>
            <!-- 进度条总进度 -->
            <attr name="total_progress" format="integer"/>
        </declare-styleable>
    </resources>
    

    自定义CircleProgressView,继承于View

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class CircleProgressView extends View {
    
        // 画背景的画笔
        private Paint mCirclePaint;
        // 画进度背景画笔
        private Paint mProgressBgPaint;
        // 画进度的画笔
        private Paint mProgressPaint;
        // 画中心图标的画笔
        private Paint mIconPaint;
        // 背景颜色
        private int mCircleColor;
        // 进度背景颜色
        private int mProgressBgColor;
        // 进度颜色
        private int mProgressColor;
        // 半径
        private float mRadius;
        // 进度半径
        private float mProgressRadius;
        // 进度条宽度
        private float mProgressWidth;
        // 圆心x坐标
        private int mXCenter;
        // 圆心y坐标
        private int mYCenter;
        // 总进度
        private int mTotalProgress;
        // 当前进度
        private int mProgress;
        // 中心图标大小
        private float mCenterIconWidth;
    
        public CircleProgressView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // 获取自定义的属性
            initAttrs(context, attrs);
            initVariable();
        }
    
        // 初始化属性
        private void initAttrs(Context context, AttributeSet attrs) {
    
            TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                    R.styleable.CircleProgressView, 0, 0);
    
            mProgressWidth = typeArray.getDimension(R.styleable.CircleProgressView_progress_width, 10);
            mCircleColor = typeArray.getColor(R.styleable.CircleProgressView_circle_background_color, 0xFFFFFFFF);
            mProgressColor = typeArray.getColor(R.styleable.CircleProgressView_progress_color, 0xFFFFFFFF);
            mProgressBgColor = typeArray.getColor(R.styleable.CircleProgressView_progress_background_color, 0xFFFFFFFF);
            mTotalProgress = typeArray.getInt(R.styleable.CircleProgressView_total_progress, 100);
            mCenterIconWidth = typeArray.getDimension(R.styleable.CircleProgressView_center_icon_width, 20);
        }
    
        // 初始化画笔
        private void initVariable() {
            // 内圆
            mCirclePaint = new Paint();
            mCirclePaint.setAntiAlias(true);
            mCirclePaint.setColor(mCircleColor);
            mCirclePaint.setStyle(Paint.Style.FILL);
    
            // 外圆背景
            mProgressBgPaint = new Paint();
            mProgressBgPaint.setAntiAlias(true);
            mProgressBgPaint.setColor(mProgressBgColor);
            mProgressBgPaint.setStyle(Paint.Style.STROKE);
            mProgressBgPaint.setStrokeWidth(mProgressWidth);
    
            // 外圆弧(进度条)
            mProgressPaint = new Paint();
            mProgressPaint.setAntiAlias(true);
            mProgressPaint.setColor(mProgressColor);
            mProgressPaint.setStyle(Paint.Style.STROKE);
            mProgressPaint.setStrokeWidth(mProgressWidth);
    
            mIconPaint = new Paint();
            mIconPaint.setAntiAlias(true);
            mIconPaint.setColor(mProgressColor);
            mIconPaint.setStyle(Paint.Style.STROKE);
            mIconPaint.setStrokeWidth(mProgressWidth);
        }
    
        // 画图
        @Override
        protected void onDraw(Canvas canvas) {
            mXCenter = getWidth() / 2;
            mYCenter = getHeight() / 2;
    
            // 如果宽 > 高,半径 = 高 / 2 - 边粗
            if (getWidth() > getHeight()) {
                mRadius = mYCenter - mProgressWidth;
            } else {
                mRadius = mXCenter - mProgressWidth;
            }
            mProgressRadius = mRadius + mProgressWidth / 2;
    
            // 内圆
            canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
    
            // 外圆弧背景
            RectF oval1 = new RectF();
            oval1.left = (mXCenter - mProgressRadius);
            oval1.top = (mYCenter - mProgressRadius);
            oval1.right = mProgressRadius * 2 + (mXCenter - mProgressRadius);
            oval1.bottom = mProgressRadius * 2 + (mYCenter - mProgressRadius);
            canvas.drawArc(oval1, 0, 360, false, mProgressBgPaint);
    
            // 外圆弧
            if (mProgress > 0) {
                RectF oval2 = new RectF();
                oval2.left = (mXCenter - mProgressRadius);
                oval2.top = (mYCenter - mProgressRadius);
                oval2.right = mProgressRadius * 2 + (mXCenter - mProgressRadius);
                oval2.bottom = mProgressRadius * 2 + (mYCenter - mProgressRadius);
                canvas.drawArc(oval2, -90, ((float) mProgress / mTotalProgress) * 360, false, mProgressPaint);
            }
    
            mIconPaint.setStyle(Paint.Style.FILL);
            RectF oval3 = new RectF();
            oval3.left = mXCenter - (mCenterIconWidth / 2);
            oval3.top = mYCenter - (mCenterIconWidth / 2);
            oval3.right = mXCenter + (mCenterIconWidth / 2);
            oval3.bottom = mYCenter + (mCenterIconWidth / 2);
            canvas.drawRoundRect(oval3, 5, 5, mIconPaint);
        }
    
        // 设置进度
        public void setProgress(int progress) {
            mProgress = progress;
            postInvalidate();
        }
    
        // 获取进度
        public int getProgress() {
            return  mProgress;
        }
    }
    

    在布局文件当中使用

    <com.wushaohong.circleprogressapp.CircleProgressView
            android:id="@+id/cp"
            android:layout_width="50dp"
            android:layout_height="50dp"
            app:progress_width="5dp"
            app:circle_background_color="#ffffff"
            app:progress_background_color="#EEEEEE"
            app:progress_color="#3398FE"
            app:total_progress="100"
            app:center_icon_width="15dp"/>
    

    在java代码中进行更新进度,控件对象设置进度值

    circleProgressView.setProgress(进度值);
    

    相关文章

      网友评论

          本文标题:Android 自定义进度条控件,仿IOS App Store下

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