横向的进度条HorizontalProgressBar

android系统自带的进度条实在是不实用,就自己定义了一个横向的进度条,话不多说,看代码 就明白了

import com.sitemap.stm.R;
import android.annotation.SuppressLint;
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.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

public class HorizontalProgressBar extends View {

/** 画笔 */
private Paint paint;


/** 长方形的颜色 */
private int horizontalColor;


/** 长方形进度的颜色 */
private int horizontalProgressColor;


/** 中间进度百分比的字符串的颜色 */
private int textColor;


/** 中间进度百分比的字符串的字体 */
private float textSize;


/** 长方形的宽度 */
private float horizontalWidth;


/** 最大进度 */
private int max;


/** 当前进度 */
private int progress;


/** 是否显示百分比 */
private boolean isShowText;


/** 是否 是圆角矩形 */
private boolean isRoundRect;


/** 圆角半径 */
private int radius;


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


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


public HorizontalProgressBar(Context context, AttributeSet attrs,
        int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    paint = new Paint();
    TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
            R.styleable.HorizontalProgressBar);
    // 获取自定义属性和默认值
    horizontalColor = mTypedArray.getColor(
            R.styleable.HorizontalProgressBar_horizontalColor, Color.GRAY);
    horizontalProgressColor = mTypedArray.getColor(
            R.styleable.HorizontalProgressBar_horizontalProgressColor,
            Color.GREEN);
    textColor = mTypedArray.getColor(
            R.styleable.HorizontalProgressBar_horizontalTextColor,
            Color.BLACK);
    textSize = mTypedArray.getDimension(
            R.styleable.HorizontalProgressBar_horizontalTextSize, 20);
    horizontalWidth = mTypedArray.getDimension(
            R.styleable.HorizontalProgressBar_horizontalWidth, 20);
    max = mTypedArray.getInteger(
            R.styleable.HorizontalProgressBar_horizontalMax, 100);
    isShowText = mTypedArray.getBoolean(
            R.styleable.HorizontalProgressBar_isShowText, true);
    isRoundRect = mTypedArray.getBoolean(
            R.styleable.HorizontalProgressBar_isRoundRect, true);
    radius = mTypedArray.getInteger(
            R.styleable.HorizontalProgressBar_radius, 10);
    mTypedArray.recycle();
}


@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    paint.setColor(horizontalColor); // 设置长方形的颜色
    paint.setStyle(Paint.Style.FILL); // 设置空心
    paint.setStrokeWidth(horizontalWidth); // 设置长方形宽度
    paint.setAntiAlias(true); // 消除锯齿
    drawRect(canvas, paint, 0, 0, getWidth(), horizontalWidth);


    /**
     * 画进度
     */
    paint.setStrokeWidth(horizontalWidth); // 设置圆环的宽度
    paint.setColor(horizontalProgressColor); // 设置进度的颜色
    drawRect(canvas, paint, 0, 0, getWidth() * progress / max,
            horizontalWidth);


    /**
     * 画进度百分比
     */     
    paint.setStrokeWidth(0);
    paint.setColor(textColor);
    paint.setTextSize(textSize);
    paint.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体
    int percent = (int) (((float) progress / (float) max) * 100); // 中间的进度百分比,先转换成float在进行除法运算,不然都为0
    float textWidth = paint.measureText(percent + "%"); // 测量字体宽度

    if (isShowText && percent != 0) {           
        canvas.drawText(percent + "%", (getWidth() - textWidth) / 2,
                (horizontalWidth + textSize) / 2, paint); // 画出进度百分比
    }
}


/**
 * 绘制矩形
 * 
 * @param canvas
 * @param paint
 * @param left
 *            左
 * @param top
 *            上
 * @param right
 *            右
 * @param bottom
 *            下
 */
private void drawRect(Canvas canvas, Paint paint, float left, float top,
        float right, float bottom) {
    RectF rectF = new RectF(left, top, right, bottom);
    if (isRoundRect) {
        canvas.drawRoundRect(rectF, radius, radius, paint);
    } else {
        canvas.drawRect(rectF, paint);
    }
}


public synchronized int getMax() {
    return max;
}


/**
 * 设置进度的最大值
 * 
 * @param max
 */
public void setMax(int max) {
    if (max < 0) {
        throw new IllegalArgumentException("max not less than 0");
    }
    this.max = max;
}


/**
 * 获取进度.需要同步
 * 
 * @return
 */
public synchronized int getProgress() {
    return progress;
}


/**
 * 设置进度
 * 
 * @param progress
 */
public void setProgress(int progress) {
    if (progress < 0) {
        throw new IllegalArgumentException("progress not less than 0");
    }
    if (progress > max) {
        progress = max;
    }
    if (progress <= max) {
        this.progress = progress;
        postInvalidate();
    }
}


public int getHorizontalColor() {
    return horizontalColor;
}


public void setHorizontalColor(int horizontalColor) {
    this.horizontalColor = horizontalColor;
}


public int getHorizontalProgressColor() {
    return horizontalProgressColor;
}


public void setHorizontalProgressColor(int horizontalProgressColor) {
    this.horizontalProgressColor = horizontalProgressColor;
}


public int getTextColor() {
    return textColor;
}


public void setTextColor(int textColor) {
    this.textColor = textColor;
}


public float getTextSize() {
    return textSize;
}


public void setTextSize(float textSize) {
    if(textSize>horizontalWidth){
        textSize = horizontalWidth;
    }
    this.textSize = textSize;
}


public float getHorizontalWidth() {
    return horizontalWidth;
}


public void setHorizontalWidth(float horizontalWidth) {
    this.horizontalWidth = horizontalWidth;
}


public boolean isShowText() {
    return isShowText;
}


public void setShowText(boolean isShowText) {
    this.isShowText = isShowText;
}


public boolean isRoundRect() {
    return isRoundRect;
}


public void setRoundRect(boolean isRoundRect) {
    this.isRoundRect = isRoundRect;
}


public int getRadius() {
    return radius;
}


public void setRadius(int radius) {
    if(radius>horizontalWidth){
        radius = (int) (horizontalWidth/2);
    }
    this.radius = radius;
}

}

在配置文件中atter如下:

<declare-styleable name="HorizontalProgressBar">
    <attr name="horizontalColor" format="color" />
    <attr name="horizontalProgressColor" format="color" />
    <attr name="horizontalWidth" format="dimension"></attr>
    <attr name="horizontalTextColor" format="color" />
    <attr name="horizontalTextSize" format="dimension" />
    <attr name="horizontalMax" format="integer"></attr>
    <attr name="isShowText" format="boolean"></attr>
    <attr name="radius" format="dimension"></attr>
    <attr name="isRoundRect" format="boolean"></attr>
</declare-styleable>

在activity中设置属性即可:

    hBar.setHorizontalColor(getResources().getColor(R.color.red));
    hBar.setHorizontalWidth(30);
    hBar.setTextSize(50);
    hBar.setTextColor(getResources().getColor(R.color.black));
    hBar.setMax(100);
    hBar.setRadius(40);
    hBar.setRoundRect(true);
    hBar.setShowText(true);                        hBar.setHorizontalProgressColor(getResources().getColor(R.color.lan));                                       
    new Thread(new Runnable() {

        @Override
        public void run() {
            while(hProgress <= 100){
                hProgress += 1;

                System.out.println(hProgress);

                hBar.setProgress(hProgress);


                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }).start();

这就是一个很基本的自定义view,体会下就懂了!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值