**第一步:
首先为自定义进度条使用的参数进行定义
文本:1)颜色 2)大小
已读进度条:3)颜色 4)大小
未读进度条:5)颜色 6)大小
(源码中的 Offset未使用)**
1、res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 声明 -->
<attr name="progress_unreach_color" format="color"></attr>
<attr name="progress_unreach_height" format="dimension"></attr>
<attr name="progress_reach_color" format="color"></attr>
<attr name="progress_reach_height" format="dimension"></attr>
<attr name="progress_text_color" format="color"></attr>
<attr name="progress_text_size" format="dimension"></attr>
<!-- 使用 -->
<declare-styleable name="HOneProgressBar">
<attr name="progress_unreach_color" ></attr>
<attr name="progress_unreach_height" ></attr>
<attr name="progress_reach_color" ></attr>
<attr name="progress_reach_height" ></attr>
<attr name="progress_text_color" ></attr>
<attr name="progress_text_size" ></attr>
</declare-styleable>
</resources>
———————————————————————
———————————————————————
**第二步:
在布局xml文件中添加自定义进度条控件(此处只添加一个,布局方式任意)**
2、res/layout/activity_progress_ty_one_main.xml
<ScrollView 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.iy.app.view.HOneProgressBar
android:id="@+id/progress_id2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:progress="0"
android:padding="15dp"/>
</LinearLayout>
</ScrollView>
———————————————————————
———————————————————————
———————————————————————
———————————————————————
**第三步:
自定义进度条类HProgressBarTyOne.java
1)参数定义
2)测量 onMeasure()方法
3)进度条绘制onDraw()方法**
3、com.example.progresstypeone.HProgressBarTyOne.java
package com.example.progresstypeone;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ProgressBar;
import com.example.progresstypeone.R;
public class HProgressBarTyOne extends ProgressBar{
private Paint mPaint = new Paint();
private int mRealWidth;
public HProgressBarTyOne(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
obtainStyledAttrs(attrs);
}
public HProgressBarTyOne(Context context, AttributeSet attrs) {
super(context, attrs,0);
}
public HProgressBarTyOne(Context context) {
super(context,null);
}
//尺寸计算
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
//传递仅widthMeasureSpec 获取其模式和值
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthVal = MeasureSpec.getSize(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(widthVal, height);
mRealWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
}
//第一层文本;第二层进度条
private int measureHeight(int heightMeasureSpec) {
int result = 0;
int mode = MeasureSpec.getMode(heightMeasureSpec);
int size = MeasureSpec.getSize(heightMeasureSpec);
if(mode == MeasureSpec.EXACTLY){ //如果是精确值(若是另外两种模式,及需要自己测量)
result = size;
}else{
int textHeight = (int) (mPaint.descent() - mPaint.ascent()); ///?获取字的高度
result = getPaddingTop() //获取高度最大值
+ getPaddingBottom()
+ mReachHeight
+ 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());
Log.e("Hei", "hei " + getHeight());
boolean noNeedUnReach = false;
mPaint.setTextSize(getmTextSize()); //设置字体的size
//已读取的进度条
//1、进度
float radio = getProgress() * 1.0f / getMax();
float progressX = radio * mRealWidth -5;
Log.e("progress", "progr " + getProgress());
if(progressX > 0){
mPaint.setColor(getmReachColor());
mPaint.setStrokeWidth(getmReachHeight());
canvas.drawLine(0, 0, progressX, 0, mPaint); //从0到endX的长度
}
//绘制未读取的进度条
if(progressX >= mRealWidth)
noNeedUnReach = true;
if(!noNeedUnReach){
mPaint.setColor(getmUnReachColor());
mPaint.setStrokeWidth(getmUnReachHeight());
canvas.drawLine(progressX + 5, 0, mRealWidth, 0, mPaint);
}
//绘制文本
mPaint.setColor(getmTextColor());
int y = (int) (-(mPaint.descent()) );
String text = getProgress() + "%";
int x = getWidth() / 2 - ((int) mPaint.measureText("100%") / 2);
canvas.drawText(text, x, y, mPaint);
canvas.restore();
}
/*
* 获取自定义属性
* @param attrs
* */
private void obtainStyledAttrs(AttributeSet attrs) {
TypedArray ta = getContext().obtainStyledAttributes(attrs,
R.styleable.HProgressBarTyOne);
mTextSize = (int) ta.getDimension(
R.styleable.HProgressBarTyOne_progress_text_size,
mTextSize);
mTextColor = ta.getColor(
R.styleable.HProgressBarTyOne_progress_text_color,
mTextColor);
mTextOffset = (int) ta.getDimension(
R.styleable.HProgressBarTyOne_progress_text_offset,
mTextOffset);
mUnReachColor = ta.getColor(
R.styleable.HProgressBarTyOne_progress_unreach_color,
mUnReachColor);
mReachColor = ta.getColor(
R.styleable.HProgressBarTyOne_progress_reach_color,
mReachColor);
mUnReachHeight = (int) ta.getDimension(
R.styleable.HProgressBarTyOne_progress_unreach_height,
mUnReachHeight);
mReachHeight = (int) ta.getDimension(
R.styleable.HProgressBarTyOne_progress_reach_height,
mReachHeight);
ta.recycle(); //
}
//默认值
private static final int DEFAULT_TEXT_SIZE = 15; //SP 字体大小--sp
private static final int DEFAULT_TEXT_COLOR = 0x55FF920C; //
private static final int DEFAULT_COLOR_UNREACH = 0xAA2FFC62; //
private static final int DEFAULT_HEIGHT_UNREACH = 2; //0
private static final int DEFAULT_COLOR_REACH = DEFAULT_TEXT_COLOR; //
private static final int DEFAULT_HEIGHT_REACH = 3; //宽高尺寸--dp
private static final int DEFAULT_TEXT_OFFSET = 8; //
private int mTextSize = sp2px(DEFAULT_TEXT_SIZE);
private int mTextColor = DEFAULT_TEXT_COLOR;
private int mTextOffset = dp2px(DEFAULT_TEXT_OFFSET);
private int mUnReachColor = DEFAULT_COLOR_UNREACH;
private int mUnReachHeight = dp2px(DEFAULT_HEIGHT_UNREACH);
private int mReachColor = DEFAULT_COLOR_REACH;
private int mReachHeight = dp2px(DEFAULT_HEIGHT_REACH);
public int getmTextSize() {
return mTextSize;
}
public void setmTextSize(int mTextSize) {
this.mTextSize = sp2px(mTextSize);
}
public int getmTextColor() {
return mTextColor;
}
public void setmTextColor(int mTextColor) {
this.mTextColor = mTextColor;
}
public int getmTextOffset() {
return mTextOffset;
}
public void setmTextOffset(int mTextOffset) {
this.mTextOffset = dp2px(mTextOffset);
}
public int getmUnReachColor() {
return mUnReachColor;
}
public void setmUnReachColor(int mUnReachColor) {
this.mUnReachColor = mUnReachColor;
}
public int getmUnReachHeight() {
return mUnReachHeight;
}
public void setmUnReachHeight(int mUnReachHeight) {
this.mUnReachHeight = dp2px(mUnReachHeight);
}
public int getmReachColor() {
return mReachColor;
}
public void setmReachColor(int mReachColor) {
this.mReachColor = mReachColor;
}
public int getmReachHeight() {
return mReachHeight;
}
public void setmReachHeight(int mReachHeight) {
this.mReachHeight = dp2px(mReachHeight);
}
dp 转换成px
private int dp2px(int dpVal)
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal,
getResources().getDisplayMetrics());
}
private int sp2px(int spVal)
{
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal,
getResources().getDisplayMetrics());
}
}
———————————————————————
———————————————————————
———————————————————————
———————————————————————
**第四步:
在MainActivity中应用(ProgressTyOneMainActivity.java)
1)创建自定义进度条对象
2)设置进度条6个相关参数
3)通过Handler进行进度设置**
4、com.example.progresstypeone.ProgressTyOneMainActivity.java
package com.example.progresstypeone;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
public class ProgressTyOneMainActivity extends Activity {
private HProgressBarTyOne mHProgressTyOne;
private static final int MSG_UPDATE = 0x110;
private Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg){
int progress = mHProgressTyOne.getProgress();
mHProgressTyOne.setProgress(++progress);
if(progress >= 100){
mHandler.removeMessages(MSG_UPDATE);
}
//延时100ms刷新
mHandler.sendEmptyMessageDelayed(MSG_UPDATE, 100);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_ty_one_main);
mHProgressTyOne = (HProgressBarTyOne)findViewById(R.id.progress_id2);
//设置文本颜色(蓝色)、文本大小(15sp)、已读取进度颜色(粉色)、已读取进度宽度(5sp)、未读取进度颜色(橙色)、未读取进度宽度(3sp)
mHProgressTyOne.setmTextColor(0xAA2FDAFC);
mHProgressTyOne.setmTextSize(15);
mHProgressTyOne.setmReachColor(0xAAFC2FC3);
mHProgressTyOne.setmReachHeight(5);
mHProgressTyOne.setmUnReachColor(0x33CC6600);
mHProgressTyOne.setmUnReachHeight(3);
mHandler.sendEmptyMessage(MSG_UPDATE);
}
}