我用的Eclipse,studio目前不熟悉,可能路径有些不一样。
1、在目录res/values/下新建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="lowColor" format="color" />
<attr name="minColor" format="color" />
<attr name="normalColor" format="color" />
<attr name="rate" format="string" />
<declare-styleable name="CustomProgressBar">
<attr name="lowColor" />
<attr name="minColor" />
<attr name="normalColor"/>
<attr name="rate"/>
</declare-styleable>
</resources>
2、新建类CustomProgressBar.java
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;
/**
* Created by Administrator on 2016/6/12.
*/
public class CustomProgressBar extends TextView {
private int lowColor;
private int midColor;
private int normalColor;
private Paint mPaint;
/** 百分比 */
private int mRate;
/*** 百分比的数值 */
private int mRateText;
/** 外边框的宽度 */
private int mBorder = 9;
/*** 柱体的宽 **/
private int mW;
/*** 间隔水平距离 */
private int mDividerW = 20;
/*** 柱体个数默认10 **/
private int mSum = 10;
/*** 分割后有一个未完整部分 均分到 柱体的左右两边,使柱体居中 */
private int mRemain;
/***
* 100%分割成十个柱体 每个代表 10% 那么有且最多只有一个是不满的 如76% 那么前7(mCount)个是满的都是 10% 第八个只有6成满
**/
private int mCount;
/*** 不满的那一格的数值 */
private int mBoundRate;
public CustomProgressBar(Context context) {
super(context);
}
public CustomProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public CustomProgressBar(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
/**
* 获得我们所定义的自定义样式属性
*/
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CustomProgressBar, defStyle, 0);
lowColor = a.getColor(R.styleable.CustomProgressBar_lowColor, Color.RED);
midColor = a.getColor(R.styleable.CustomProgressBar_minColor, Color.YELLOW);
normalColor = a.getColor(R.styleable.CustomProgressBar_normalColor, Color.BLUE);
String text = a.getString(R.styleable.CustomProgressBar_rate);
try {
mRate = Integer.parseInt(text);
} catch (NumberFormatException e) {
mRate = 0;
}
mCount = mRate / 10;
mBoundRate = mRate % 10;
mRateText = mRate;
a.recycle();
mPaint = new Paint();
mPaint.setTextSize(getTextSize());
mPaint.setColor(normalColor);
Log.e("zjs", "mCount==" + mCount + " mBoundRate=" + mBoundRate);
}
public int getRate(){
return mRate;
}
public void setRate(int rate) {
mRateText = rate;
mRate = rate;
mCount = mRate / 10;
mBoundRate = mRate % 10;
this.invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
drawRectBorder(canvas);
drawRects(canvas);
drawRate(canvas);
super.onDraw(canvas);
}
/*** 绘制10个柱体 */
private void drawRects(Canvas canvas) {
Paint paint = new Paint();
Paint paint1 = new Paint();
Paint paint2 = new Paint();
Paint paint3 = new Paint();
if (mCount==0){
paint.setColor(lowColor);
}else if(mCount==1){
paint.setColor(midColor);
}else{
paint.setColor(normalColor);
}
paint1.setColor(lowColor);
paint2.setColor(midColor);
paint3.setColor(normalColor);
mW = (getWidth() - mDividerW - mBorder) / mSum - mDividerW;
mRemain = mBorder / 2;
float x = 0f;
for (int i = 0; i < mCount; i++) {
x = getWidth() - ((mW + mDividerW) * i + mDividerW + mRemain);
if(i == 0)
canvas.drawRect(x - mW, mBorder, x, getHeight() - mBorder, paint1);
else if(i == 1)
canvas.drawRect(x - mW, mBorder, x, getHeight() - mBorder, paint2);
else if (i > 1)
canvas.drawRect(x - mW, mBorder, x, getHeight() - mBorder, paint3);
Log.e("zjs", "i==" + i);
}
x = getWidth() - ((mW + mDividerW) * mCount + 1 + mDividerW + mRemain);// (mW+mDividerW)*+mCount+1+mDividerW+mW+mRemain
int y = getHeight() - ((getHeight() - mBorder - mBorder) * mBoundRate / 10) - mBorder;// -mBorder
canvas.drawRect(x - mW, y, x, getHeight() - mBorder, paint);
}
/*** 绘制外边框 **/
private void drawRectBorder(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);
// 设置空心Style
mPaint.setStyle(Paint.Style.STROKE);
// 设置空心边框的宽度
mPaint.setStrokeWidth(mBorder);
// 绘制空心矩形
canvas.drawRect(0, 0, getWidth(), getHeight(), mPaint);
}
/*** 绘制百分比的文本 */
private void drawRate(Canvas canvas) {
Log.e("zjs", "mCount==" + mCount + " mBoundRate=" + mBoundRate);
Paint paint = new Paint();
// 去锯齿
paint.setAntiAlias(true);
// 设置颜色
paint.setColor(Color.WHITE);
paint.setTextSize(getTextSize());
// 绘制文本
canvas.drawText(mRateText + "%", getWidth() / 2, getHeight() / 2
+ mBorder, paint);
}
}
3、布局文件中引入此自定义进度条
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hzl.zhuxingprogressbar.MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:digits="1234567890-"
android:ems="10" >
<requestFocus />
</EditText>
<com.hzl.zhuxingprogressbar.CustomProgressBar
android:id="@+id/main_custom_pb1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignLeft="@+id/editText1"
android:layout_centerVertical="true"
android:digits="0123456789"
android:gravity="center"
android:padding="5dp"
android:textColor="@android:color/white"
android:textSize="16sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/main_custom_pb1"
android:layout_alignLeft="@+id/main_custom_pb1"
android:layout_marginBottom="52dp"
android:text="Button" />
</RelativeLayout>
4、在Activity中绑定使用
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private CustomProgressBar pb;
private EditText edt;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (CustomProgressBar) findViewById(R.id.main_custom_pb1);
edt = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int value = Integer.parseInt(edt.getText().toString());
setDownloadTaskProgress(value, pb.getRate(), handler);
}
});
}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
pb.setRate(msg.arg1);
}
};
// 模拟耗时操作
private void setDownloadTaskProgress(final int value, final int current,
final Handler handler) {
final int time;
if (Math.abs(current - value) < 10)
time = 300;
else if (Math.abs(current - value) > 10
&& Math.abs(current - value) < 20)
time = 600;
else
time = 1000;
new Thread(new Runnable() {
@Override
public void run() {
try {
if (value > current) {
for (int i = current; i <= value; i++) {
Message message = new Message();
message.arg1 = i;
handler.sendMessage(message);
Thread.sleep(time / (value - current));
}
} else if (value < current) {
for (int i = current; i >= value; i--) {
Message message = new Message();
message.arg1 = i;
handler.sendMessage(message);
Thread.sleep(time / (current - value));
}
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
运行。。。。