前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >018android初级篇之自定义圆形进度条

018android初级篇之自定义圆形进度条

作者头像
上善若水.夏
发布2018-09-28 10:38:27
5140
发布2018-09-28 10:38:27
举报
文章被收录于专栏:上善若水上善若水

项目需要,需要一个圆形的进度条,所想到的实现方案是这样的:

自定义View,订制一个圆形的进度条。下面简述实现,有不当之处敬请指正。

主题思想

  1. 自定义类继承自View,在构造函数中,获得从配置文件中获得的自定义属性值。
  2. 在onDraw中绘制,跟据获得的属性值,绘制想要的图形。

自定义属性的定义

在文件attrs.xml中

代码语言:javascript
复制
<resources>
    <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> 
</resources>

则,在layout文件中,可以使用这些自定义属性:

代码语言:javascript
复制
<com.running.aid.aidrunning.RoundProgressBar
    android:id="@+id/roundProgressBar"
    android:layout_width="250dip"
    android:layout_height="250dip"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="78dp"
    android:layout_centerHorizontal="true"


    android_custom:roundColor="#D1D1D1"
    android_custom:roundProgressColor="@android:color/holo_green_light"
    android_custom:textColor="@color/gray9"
    android_custom:roundWidth="25dip"
    android_custom:textSize="18sp" />

在自定义的进度条类的构造函数中,获取配置的自定义属性值:

代码语言:javascript
复制
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);

绘制控件

画最外层的大圆环

代码语言:javascript
复制
    int centre = getWidth()/2; //获取圆心的x坐标
    int radius = (int) (centre - roundWidth/2); //圆环的半径
    paint.setColor(roundColor); //设置圆环的颜色
    paint.setStyle(Paint.Style.STROKE); //设置空心
    paint.setStrokeWidth(roundWidth); //设置圆环的宽度
    paint.setAntiAlias(true);  //消除锯齿
    canvas.drawCircle(centre, centre, radius, paint); //画出圆环

绘制文本

这里是模仿计步器,显示的当前步数,总步数,分三行显示。

代码语言:javascript
复制
paint.setStrokeWidth(0);
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体

//if(textIsDisplayable && percent != 0 && style == STROKE){
if(textIsDisplayable && style == STROKE){
    stepNumValueStr = "" + currentValue;
    stepAIMValueStr = "" + maxValue;
    float stepNumStrWidth = paint.measureText(stepNumStr);
    
    float stepAIMStrWidth = paint.measureText(stepAIMStr+stepAIMValueStr);
    float y = paint.getFontMetrics().bottom - paint.getFontMetrics().top;
    canvas.drawText(stepNumStr,centre-stepNumStrWidth/2,centre -2*y,paint);
    canvas.drawText(stepAIMStr+stepAIMValueStr,centre-stepAIMStrWidth/2,centre+2*y,paint);
    
    paint.setTextSize(textSize * 2);
    paint.setColor(getResources().getColor(R.color.blue6));
    float stepNumValueStrWidth = paint.measureText(stepNumValueStr);
    canvas.drawText(stepNumValueStr,centre-stepNumValueStrWidth/2,centre,paint);


}

绘制扇形,进度条

代码语言:javascript
复制
    paint.setStrokeWidth(roundWidth); //设置圆环的宽度
    paint.setColor(roundProgressColor);  //设置进度的颜色
    RectF oval = new RectF(centre - radius, centre - radius, centre
            + radius, centre + radius);  //用于定义的圆弧的形状和大小的界限

    switch (style) {
        case STROKE:{
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawArc(oval, 0, 360 * progress / max, false, paint);  //根据进度画圆弧
            break;
        }
        case FILL:{
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            if(progress !=0)
                canvas.drawArc(oval, 0, 360 * progress / max, true, paint);  //根据进度画圆弧
            break;
        }
    }

参考链接

  1. 017android初级篇之android canvas的使用
  2. Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015.11.19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 主题思想
  • 自定义属性的定义
    • 画最外层的大圆环
      • 绘制文本
        • 绘制扇形,进度条
        • 参考链接
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档