Android自定义控件之圆形进度条Android自定义控件之-圆形进度条

Android自定义控件之-圆形进度条

先上图:

填充的

环形的

贴代码不废话:

** CircleProgressBar.java **

package com.xiaolei.xiaoui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by xiaolei on 2017/3/23.
 */

public class CircleProgressBar extends View
{
    private int height;
    private int width;
    private Paint mPaint;
    private int strokeWidth = 5;//线条宽度
    private RectF rectF;
    private int normalColor = Color.parseColor("#A5A5A5");//普通的颜色
    private int progressColor = Color.parseColor("#FA9025");//已经走了的进度条颜色
    private int textColor = normalColor;//文字颜色
    private float textSize = 20;//文字大小
    private int progress = 0;//进度条
    private String centerText = "100%";//中心填充文字
    private Paint fontPaint = null;
    private Paint.Style progress_style = Paint.Style.STROKE;//填充式还是环形式
    
    public CircleProgressBar(Context context)
    {
        this(context, null);
    }

    public CircleProgressBar(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public CircleProgressBar(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        textSize = array.getDimension(R.styleable.CircleProgressBar_text_size,textSize);
        textColor = array.getColor(R.styleable.CircleProgressBar_text_color,textColor);
        centerText = array.getString(R.styleable.CircleProgressBar_text)==null?centerText:array.getString(R.styleable.CircleProgressBar_text);
        strokeWidth = array.getInteger(R.styleable.CircleProgressBar_stroke_width,strokeWidth);
        normalColor = array.getColor(R.styleable.CircleProgressBar_normal_color,normalColor);
        progressColor = array.getColor(R.styleable.CircleProgressBar_progress_color,progressColor);
        progress = array.getInt(R.styleable.CircleProgressBar_progress,progress);
        progress_style = array.getInt(R.styleable.CircleProgressBar_progress_style,0)==0?Paint.Style.STROKE:Paint.Style.FILL;
        
        array.recycle();
        initPaint();
    }

    // 2.初始化画笔
    private void initPaint()
    {
        mPaint = new Paint();
        mPaint.setColor(normalColor);       //设置画笔颜色
        mPaint.setAntiAlias(true);
        mPaint.setStyle(progress_style);  //设置画笔模式为描边
        mPaint.setStrokeWidth(strokeWidth);         //设置画笔宽度为10px
        
        fontPaint = new Paint();
        fontPaint.setTextSize(textSize);
        fontPaint.setAntiAlias(true);
        fontPaint.setColor(textColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        height = MeasureSpec.getSize(heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);

        if (height > width)//高大于宽的情况
        {
            rectF = new RectF(strokeWidth, (height / 2 - width / 2) + strokeWidth, width - strokeWidth, (height / 2 + width / 2) - strokeWidth);
        } else if (width > height)//宽大于高的情况
        {
            rectF = new RectF((width / 2 - height / 2) + strokeWidth, strokeWidth, (width / 2 + height / 2) - strokeWidth, height - strokeWidth);
        } else//宽等于高的情况
        {
            rectF = new RectF(strokeWidth, strokeWidth, width - strokeWidth, height - strokeWidth);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    Paint.FontMetrics fontMetrics = null;

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        mPaint.setColor(normalColor);
        if(progress < 360)
        {
            canvas.drawArc(rectF, 270+progress, 360 - progress, progress_style== Paint.Style.FILL, mPaint);
        }
        
        mPaint.setColor(progressColor);
        canvas.drawArc(rectF, 270, progress, progress_style== Paint.Style.FILL, mPaint);
        
        fontMetrics = fontPaint.getFontMetrics();
        float textWidth = fontPaint.measureText(centerText);
        float textHeight = fontPaint.ascent() + fontPaint.descent();
        canvas.drawText(centerText, width / 2 - textWidth / 2, height / 2 - textHeight / 2, fontPaint);
    }
    
    
    /**
     * 更新界面
     * @param progress
     * @param text
     */
    public void update(int progress,String text)
    {
        this.progress = progress;
        this.centerText = text;
        postInvalidate();
    }
    
}

** declare-styleable **

    <declare-styleable name="CircleProgressBar">
        <!--文字大小-->
        <attr name="text_size" format="dimension"/>
        <!--文字颜色-->
        <attr name="text_color" format="color"/>
        <!--文本-->
        <attr name="text" format="string"/>
        <!--变宽宽度-->
        <attr name="stroke_width" format="integer"/>
        <!--未走的进度的颜色-->
        <attr name="normal_color" format="color"/>
        <!--已走的进度的颜色-->
        <attr name="progress_color" format="color"/>
        <!--已走的进度 0-360 -->
        <attr name="progress" format="integer"/>
        <!--类型 填充型,还是环形-->
        <attr name="progress_style" format="enum">
            <enum name="STROKE" value="0"/>
            <enum name="FILL" value="1"/>
        </attr>
    </declare-styleable>

使用方式:

<com.xiaolei.xiaoui.CircleProgressBar
        android:id="@+id/circleProgressBar"
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:stroke_width="20"
        app:text_color="#ff0000"
        app:text_size="30sp"
        app:progress_style="STROKE"
        app:normal_color="#A5A5A5"
        app:progress_color="#FA9025"
        app:progress="60"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>

** MainActivity.java **

CircleProgressBar progressBar = (CircleProgressBar) findViewById(R.id.circleProgressBar);
........
while (true)
{
    Thread.sleep(30);
    progress ++;
    progressBar.update(progress,progress*100/360+"%");
    if(progress == 360)
    {
        progress = 0;
    }
}

大功告成(' _ ')

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码匠的流水账

聊聊kafka consumer offset lag increase异常

本文主要解析一下遇到的一个kafka consumer offset lag不断增大的异常。

741
来自专栏linux驱动个人学习

高通Audio中ASOC的machine驱动

ASoC被分为Machine、Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的...

9784
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1142
来自专栏码匠的流水账

java9系列(五)Stack-Walking API

java9新增这个类的目的是提供一个标准API用于访问当前线程栈,之前只有Throwable::getStackTrace、Thread::getStackTr...

421
来自专栏跟着阿笨一起玩NET

将图片保存到XML文件的方法

752
来自专栏Pulsar-V

Save Camera Document

#pragma once #include "HCCamera.h" #include <time.h> #include <cstdio> #incl...

2828
来自专栏MelonTeam专栏

Bitmap 源码阅读笔记

导语: Android 系统上的图片的处理,跟Bitmap 这个类脱不了关系,我们有必要去深入阅读里面的源码,以便在工作中能更好的处理Bitmap相关的问题...

2518
来自专栏专知

2018年SCI期刊最新影响因子排行,最高244,人工智能TPAMI9.455

2018年6月26日,最新的SCI影响因子正式发布,涵盖1万2千篇期刊。CA-Cancer J Clin 依然拔得头筹,其影响因子今年再创新高,达244.585...

1282
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

1112
来自专栏技术之路

wpf键盘记录器

很简单的一个wpf键盘记录器 ? 这个程序我一样用了全局勾子,之前用的都是winform上运行了,前一段时间 在国外的论坛上逛看到了一个wpf能用的就做了一个小...

2035

扫码关注云+社区