自定义圆形/横向进度条

0. 效果图

自定义进度条

1. 基础

1.0 注意注意点

  • 在 Android 原生的API中使用的尺寸都是像素(px);

1.1 为什么要自定义控件

  • 实现特定的显示风格.
  • 处理特有的用户交互.
  • 优化我们的布局.
  • 封装等...

1.2 如何自定义控件

  • 自定义属性的定义和声明.
    • res/values/ attrs.xml 定义声明.
    • 在layout中使用.
    • 在View的构造方法中进行获取.
  • 测量 onMeasure.
  • 布局 onLayout(ViewGroup).
  • 绘制 onDraw.
  • onTouchEvent
  • onInterceptTouchEvent (拦截子View事件).
  • 状态的回复与保存.

2. 横向/圆形进度条

自定义属性文件 : res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- 自定义属性可以单独声明也可以和declare-styleable 放在一起.-->
    <attr name="progress_reach_color" format="color"/>
    <attr name="progress_reach_height" format="dimension"/>
    <attr name="progress_unreach_color" format="color"/>
    <attr name="progress_unreach_height" format="dimension"/>
    <attr name="progress_text_color" format="color"/>
    <attr name="progress_text_offset" format="dimension"/>
    <attr name="progress_text_size" format="dimension"/>
    <!-- 横向进度条自定义属性. -->
    <declare-styleable name="HorizontalProgressWithProgress">
        <attr name="progress_reach_color"/>
        <attr name="progress_reach_height" />
        <attr name="progress_unreach_color" />
        <attr name="progress_unreach_height" />
        <attr name="progress_text_color" />
        <attr name="progress_text_offset" />
        <attr name="progress_text_size" />
    </declare-styleable>
    <!-- 圆形进度条自定义属性 -->
    <declare-styleable name="CircleProgressWithProgress">
        <attr name="progress_radius" format="dimension"/>
    </declare-styleable>
</resources>

横向进度条 HorizontalProgressWithProgress

package com.wsj.test.custom.progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

import com.wsj.test.customviewone.R;

/**
 * 自定义水平进度条.<br>
 *     最后一部分可能会有点问题.
 * Created by WSJ on 2017/1/16.
 */

public class HorizontalProgressWithProgress extends ProgressBar {
    private static final String TAG = "HorizontalProgressWithProgress";

    private final int DEFAULT_TEXT_SIZE        = 10 ; // sp
    private final int DEFAULT_TEXT_OFFSET      = 10 ; // dp
    private final int DEFAULT_REACH_HEIGHT     = 2 ;  // dp
    private final int DEFAULT_UNREACH_HEIGHT   = 2 ;  // dp

    private final int DEFAULT_TEXT_COLOR       = 0xFFFC00D1 ;
    private final int DEFAULT_REACH_COLOR      = DEFAULT_TEXT_COLOR ;
    private final int DEFAULT_UNREACH_COLOR    = 0xFFD3D6DA ;

    protected int mTextSize = sp2px(DEFAULT_TEXT_COLOR);
    protected int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET); // 文本和Bar之间的距离.
    protected int mReachHeight = dp2px(DEFAULT_REACH_HEIGHT);
    protected int mUnreachHeight = dp2px(DEFAULT_UNREACH_HEIGHT);

    protected int mTextColor = DEFAULT_TEXT_COLOR;
    protected int mReachColor = DEFAULT_REACH_COLOR;
    protected int mUnreachColor = DEFAULT_UNREACH_COLOR;
    // 画笔.
    protected Paint mPaint = new Paint();
    // 当前控件宽度 - Padding值. 在 onMeasure 中赋值.在onDraw 中使用.
    private int mRealWidth;


    public HorizontalProgressWithProgress(Context context) {
        this(context,null);
    }

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

    public HorizontalProgressWithProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 1. 获取自定义属性.
        obtainStyledAttrs(attrs);
        // 2. 设置字体大小. , 一定不能少.
        mPaint.setTextSize(mTextSize);
    }

    // 获取自定义属性.
    private void obtainStyledAttrs(AttributeSet attrs) {
        // 第二个参数就是在 attrs.xml 的 name
        // TypeArray 使用完后一定要记得释放 .
        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressWithProgress);

        mTextSize = (int) ta.getDimension(R.styleable.HorizontalProgressWithProgress_progress_text_size,mTextSize);
        mTextColor = ta.getColor(R.styleable.HorizontalProgressWithProgress_progress_text_color,mTextColor);
        mTextOffset = (int) ta.getDimension(R.styleable.HorizontalProgressWithProgress_progress_text_offset,mTextOffset);

        mReachColor = ta.getColor(R.styleable.HorizontalProgressWithProgress_progress_reach_color,mReachColor);
        mReachHeight = (int) ta.getDimension(R.styleable.HorizontalProgressWithProgress_progress_reach_height,mReachHeight);

        mUnreachColor = ta.getColor(R.styleable.HorizontalProgressWithProgress_progress_unreach_color,mReachColor);
        mUnreachHeight = (int) ta.getDimension(R.styleable.HorizontalProgressWithProgress_progress_unreach_height,mReachHeight);

        ta.recycle();

    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 计算尺寸
        final int height = measureHeight(heightMeasureSpec);
        final int width = MeasureSpec.getSize(widthMeasureSpec);

        setMeasuredDimension(width,height);

        mRealWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft();
    }


    // 计算高度.
    private int measureHeight(int heightMeasureSpec) {
        final int size = MeasureSpec.getSize(heightMeasureSpec);
        final int mode = MeasureSpec.getMode(heightMeasureSpec);

        int result = 0;

        if (mode == MeasureSpec.EXACTLY){
            // 精确模式 不需要修改.
            result = size;
        }else {
            // 计算Text 高度.
            final int textHeight = (int) (mPaint.descent() - mPaint.ascent());
            // 记得加上Padding值.
            result = getPaddingTop() + getPaddingBottom() + Math.max(Math.max(mReachHeight,mUnreachHeight),textHeight);
            if (mode == MeasureSpec.AT_MOST){
                result = Math.min(result,size);
            }
        }
        return result;
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.save();
        // 一定画笔
        canvas.translate(getPaddingLeft(),getHeight() / 2);

        boolean noNeedUnreach = false;

        // draw reach
        // 进度百分比.
        final float radio = getProgress() * 1.0f / getMax();
        String text = getProgress() + "%";
        // 测量文本宽度.
        int textWidth = (int) mPaint.measureText(text);
        float progressX = radio * mRealWidth;

        if ((progressX + textWidth) > mRealWidth){
            // 右侧不需要绘制.
            progressX = mRealWidth - textWidth;
            noNeedUnreach = true;
        }
        // 终点 :
        float endX = progressX - mTextOffset / 2 ;
        if (endX > 0){
            mPaint.setColor(mReachColor);
            mPaint.setStrokeWidth(mReachHeight);
            canvas.drawLine(0,0,endX,0,mPaint);
        }

        // draw text
        mPaint.setColor(mTextColor);
        int y = (int) (-(mPaint.descent() + mPaint.ascent()) / 2);
        canvas.drawText(text,progressX,y,mPaint);

        // draw unreach
        if (!noNeedUnreach) {
            float startX = progressX + mTextOffset / 2 + textWidth;
            mPaint.setColor(mUnreachColor);
            mPaint.setStrokeWidth(mUnreachHeight);
            canvas.drawLine(startX, 0, mRealWidth, 0, mPaint);
            canvas.restore();
        }

    }
    // dp --> px
    protected int dp2px(final int dpValue){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,getResources().getDisplayMetrics());
    }
    // sp --> px
    protected int sp2px(final int spValue){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,getResources().getDisplayMetrics());
    }
}

圆形进度条 CircleProgressWithProgress

package com.wsj.test.custom.progress;

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.util.Log;

import com.wsj.test.customviewone.R;

/**
 * 圆形进度条.
 * Created by WSJ on 2017/1/16.
 */

public class CircleProgressWithProgress extends HorizontalProgressWithProgress {

    private static final int DEFAULT_PRORESS_RADIUS = 10; // dp;
    // 半径.
    private int mRadius = dp2px(DEFAULT_PRORESS_RADIUS);

    private int mMaxPaintWidth = 0;


    public CircleProgressWithProgress(Context context) {
        this(context,null);
    }

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

    public CircleProgressWithProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // 获取自定义属性.
        TypedArray ta = getContext().obtainStyledAttributes(attrs,R.styleable.CircleProgressWithProgress);
        mRadius = (int) ta.getDimension(R.styleable.CircleProgressWithProgress_progress_radius,mRadius);
        ta.recycle();
        Log.d("Circle","半径 : " + mRadius);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mMaxPaintWidth = Math.max(mReachHeight , mUnreachHeight);
        int expectSize = mRadius * 2 + mMaxPaintWidth + getPaddingLeft() + getPaddingRight();

        int width = resolveSize(expectSize,widthMeasureSpec);
        int height = resolveSize(expectSize,heightMeasureSpec);

        int realWidth = Math.min(width,height);

        mRadius = (realWidth - getPaddingLeft() - getPaddingRight() - mMaxPaintWidth) / 2;

        setMeasuredDimension(realWidth,realWidth);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        String text = getProgress() + "%";
        float textWidth = mPaint.measureText(text);
        float textHeight = (mPaint.descent() + mPaint.ascent()) / 2;
        canvas.save();
        canvas.translate(getPaddingLeft() + mMaxPaintWidth/2,getPaddingTop() + mMaxPaintWidth / 2);
        mPaint.setStyle(Paint.Style.STROKE);
        // draw unreach
        mPaint.setColor(mUnreachColor);
        mPaint.setStrokeWidth(mUnreachHeight);
        canvas.drawCircle(mRadius,mRadius,mRadius,mPaint);
        // draw reach
        mPaint.setColor(mReachColor);
        mPaint.setStrokeWidth(mReachHeight);
        // 计算角度.
        float sweepAngle = getProgress() * 1.0f / getMax() * 360;
        final RectF rect = new RectF(0,0,mRadius * 2, mRadius * 2);
        canvas.drawArc(rect,0,sweepAngle,false,mPaint);
        // draw text
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mTextColor);
        canvas.drawText(text,mRadius - textWidth / 2,mRadius - textHeight / 2,mPaint);
        canvas.restore();
    }
}

使用--布局文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:wsj="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <com.wsj.test.custom.progress.HorizontalProgressWithProgress
            android:id="@+id/id_progress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="50"
            android:padding="1dp"
            android:background="#FFFF00"
            wsj:progress_text_size="20sp"
            wsj:progress_unreach_color="#00F5FF"
            wsj:progress_text_color="#000000"
            android:layout_marginTop="30dp"
            />
        <com.wsj.test.custom.progress.HorizontalProgressWithProgress
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="50"
            android:padding="5dp"
            android:background="#FFFF00"
            wsj:progress_text_color="#000000"
            wsj:progress_text_size="20sp"
            wsj:progress_unreach_color="#00F5FF"
            android:layout_marginTop="30dp"
            />
        <com.wsj.test.custom.progress.CircleProgressWithProgress
            android:id="@+id/id_progress_circle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="50"
            android:padding="5dp"
            wsj:progress_text_color="#000000"
            wsj:progress_text_size="20sp"
            wsj:progress_unreach_color="#00F5FF"
            wsj:progress_radius="200dp"
            android:layout_marginTop="30dp"
            />
    </LinearLayout>

</ScrollView>

使用 MainActivity

package com.wsj.test.customviewone;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.wsj.test.custom.progress.CircleProgressWithProgress;
import com.wsj.test.custom.progress.HorizontalProgressWithProgress;

public class MainActivity extends AppCompatActivity {

    private HorizontalProgressWithProgress mHorizontalProgressWithProgress;
    private CircleProgressWithProgress mCircleProgressWithProgress;

    private static final int MSG_UPDATE = 0x01;
    private Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            int progress = mHorizontalProgressWithProgress.getProgress();
            mHorizontalProgressWithProgress.setProgress(++progress);
            mCircleProgressWithProgress.setProgress(progress);
            if (progress >= 100){
                mHandler.removeMessages(MSG_UPDATE);
            }
            mHandler.sendEmptyMessageDelayed(MSG_UPDATE,100);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHorizontalProgressWithProgress = (HorizontalProgressWithProgress) findViewById(R.id.id_progress);
        mCircleProgressWithProgress = (CircleProgressWithProgress) findViewById(R.id.id_progress_circle);

        mHandler.sendEmptyMessageDelayed(MSG_UPDATE,100);

    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,302评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,563评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,433评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,628评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,467评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,354评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,777评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,419评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,725评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,768评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,543评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,387评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,794评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,032评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,305评论 1 252
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,741评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,946评论 2 336

推荐阅读更多精彩内容