设为首页 加入收藏

TOP

Android进度条学习(一)
2017-10-13 10:35:57 】 浏览:6538
Tags:Android 进度 学习

自定义属性

    <!--
    roundColor 圆环的颜色

    roundProgressColor 进度的颜色

    roundWidth 圆环的宽度

    textColor 文字颜色

    textSize  文字大小

    max 最大值

    textIsDisplayable  是否显示进度文本


    style 样式
    STROKE 空心
    FILL 实心
    -->

    <declare-styleable name="RoundProgressBar">
        <attr name="roundColor" format="color"/>
        <attr name="roundProgressColor" format="color"/>
        <attr name="roundWidth" format="dimension"></attr>
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="max" format="integer"></attr>
        <attr name="textIsDisplayable" format="boolean"></attr>
        <attr name="style">
            <enum name="STROKE" value="0"></enum>
            <enum name="FILL" value="1"></enum>
        </attr>
    </declare-styleable>

 

public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();

        /**
         * 获取自定义的属性
         */
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);

        //底色
        mRoundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
        //进度的颜色
        mRoundProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.BLUE);
        //圆形的宽
        mRoundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 20);

        //字体颜色 中间
        mTextColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.BLUE);

        //中间进度显示的字体大小
        mTextSize = typedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);

        //最大值
        mMax = typedArray.getInteger(R.styleable.RoundProgressBar_max, 100);

        //文字是否显示
        mTextIsDisplayable = typedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);

        //实心或者 空心
        mStyle = typedArray.getInt(R.styleable.RoundProgressBar_style, 0);


        typedArray.recycle();

    }

 

 

绘制

        //圆心
        int centerOfCircle = getWidth() / 2;
        //radius 半径
        int radius = (int) (centerOfCircle - mRoundWidth / 2);


        //设置画笔
        mPaint.setAntiAlias(true);
        //圆环的颜色
        mPaint.setColor(mRoundColor);

        //设置空心
        mPaint.setStyle(Paint.Style.STROKE);

        //画笔宽度
        mPaint.setStrokeWidth(mRoundWidth);

        //画圆
        canvas.drawCircle(centerOfCircle, centerOfCircle, radius, mPaint);


        /**
         * 画百分比
         */
        mPaint.setStrokeWidth(0);
        //字体大小
        mPaint.setTextSize(mTextSize);
        //画笔颜色
        mPaint.setColor(mTextColor);
        //字体
        mPaint.setTypeface(Typeface.DEFAULT_BOLD);

        //计算百分比
        int percent = (int) (((float) mProgress / (float) mMax) * 100);

        //测量字体的宽度
        float textWidth = mPaint.measureText(percent + "%");
        //判断是否显示进度文字 不是0,风格是空心的
        if (mTextIsDisplayable && percent != 0 && mStyle == STROKE) {

            canvas.drawText(percent + "%", centerOfCircle - textWidth / 2, centerOfCircle + textWidth / 2, mPaint);
        }


        /**
         * 设置进度
         */
        mPaint.setColor(mRoundProgressColor);
        //画笔宽度
        mPaint.setStrokeWidth(mRoundWidth);
        mPain
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇android选择时间攻略 下一篇ionic 打包签名

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目