Android自定义水平和圆形的progressBar

这篇博文介绍了如何在Android中自定义水平和圆形的ProgressBar,遵循imooc课程,通过继承并重写测量和绘制代码实现。文章包括attrs属性、源码展示、XML引用及MainActivity的进度更新操作。
摘要由CSDN通过智能技术生成

本篇博文是按照http://www.imooc.com/learn/657教程中的学习过程,代码是按照视频中原作者的思路来敲的,虽然只是模仿,但我觉得这是学习自定义view的必经之路,所以还是把我所学到的东西拿出来与大家一起分享。

先贴出一张progressBar的gif图,其中有水平的进度条,和圆形的进度条:


这里我们的实现的思路是继承的progressBar,然后重新去测量和绘制相关代码,我们直接贴出源码:

attrs相关属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="progress_unreach_color" format="color" />
    <attr name="progress_unreach_height" format="dimension" />
    <attr name="progress_reach_color" format="color" />
    <attr name="progress_reach_height" format="dimension" />
    <attr name="progress_text_color" format="color" />
    <attr name="progress_text_size" format="dimension" />
    <attr name="progress_text_offset" format="dimension" />

    <declare-styleable name="HorizontalProgressbarWithProgress">
        <attr name="progress_unreach_color" />
        <attr name="progress_unreach_height" />
        <attr name="progress_reach_color" />
        <attr name="progress_reach_height" />
        <attr name="progress_text_color" />
        <attr name="progress_text_size" />
        <attr name="progress_text_offset" />
    </declare-styleable>

    <declare-styleable name="RoundProgressbarWithProgress">
        <attr name="radius" format="dimension" />
    </declare-styleable>
</resources>

水平进度条源码:

package com.example.asiatravel.learnprogressbar.view;

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.example.asiatravel.learnprogressbar.R;

/**
 * Created by kuangxiaoguo on 16/9/8.
 *
 * 水平进度条
 */
public class HorizontalProgressbarWithProgress extends ProgressBar {

    private static final int DEFAULT_TEXT_SIZE = 10;
    private static final int DEFAULT_TEXT_COLOR = 0xFFFC00D1;
    private static final int DEFAULT_COLOR_UNREACH = 0XFFD3D6DA;
    private static final int DEFAULT_HEIGHT_UNREACH = 2;
    private static final int DEFAULT_COLOR_REACH = DEFAULT_TEXT_COLOR;
    private static final int DEFAULT_HEIGHT_REACH = 2;
    private static final int DEFAULT_TEXT_OFFSET = 10;

    protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
    protected int mTextColor = DEFAULT_TEXT_COLOR;
    protected int mUnReachColor = DEFAULT_COLOR_UNREACH;
    protected int mUNReachHeight = dp2px(DEFAULT_HEIGHT_UNREACH);
    protected int mReachColor = DEFAULT_COLOR_REACH;
    protected int mReachHeight = dp2px(DEFAULT_HEIGHT_REACH);
    protected int mTextOffSet = dp2px(DEFAULT_TEXT_OFFSET);

    protected Paint mPaint = new Paint();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值