android 进度条自动增长,[Android][自定义进度条]③--绘制条形进度条

增加代码

@Override

protected synchronized void onDraw(Canvas canvas) {

canvas.save();

canvas.translate(getPaddingLeft(),getHeight()/2);

boolean noNeedUnreach=false;

//draw reach bar

String text=getProgress()+"%";

int textWidth= (int) mPaint.measureText(text);

float radio=getProgress()*1.0f/getMax();

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 bar

if(!noNeedUnreach)

{

float start=progressX+mTextOffset/2+textWidth;

mPaint.setColor(mUnReachColor);

mPaint.setStrokeWidth(mUnReachHeight);

canvas.drawLine(start,0,mRealWidth,0,mPaint);

}

canvas.restore();

}

完整代码

package myapplication4.xt.com.myapplication4;

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;

/**

* Created by TONG on 2017/3/23.

*/

public class HorizontalProgressbarWithProgress extends ProgressBar{

private static final int DEFAULT_TEXT_SIZE=10;//sp

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;//dp

private static final int DEFAULT_TEXT_OFFSET=10;//dp

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();

protected int mRealWidth;

public HorizontalProgressbarWithProgress(Context context) {

this(context,null);

}

public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

obtainStyledAttrs(attrs);

}

/**

* 获取自定义属性

* @param attrs

*/

private void obtainStyledAttrs(AttributeSet attrs) {

TypedArray ta=getContext().obtainStyledAttributes

(attrs,R.styleable.HorizontalProgressbarWithProgress);

mTextSize= (int) ta.getDimension

(R.styleable.HorizontalProgressbarWithProgress_progress_text_size,mTextSize);

mTextColor=ta.getColor

(R.styleable.HorizontalProgressbarWithProgress_progress_text_color,mTextColor);

mTextOffset= (int) ta.getDimension

(R.styleable.HorizontalProgressbarWithProgress_progress_text_offset,mTextOffset);

mUnReachColor=ta.getColor

(R.styleable.HorizontalProgressbarWithProgress_progress_unreach_color,mUnReachColor);

mUnReachHeight= (int) ta.getDimension

(R.styleable.HorizontalProgressbarWithProgress_progress_unreach_height,mUnReachHeight);

mReachColor=ta.getColor

(R.styleable.HorizontalProgressbarWithProgress_progress_reach_color,mReachColor);

mReachHeight= (int) ta.getDimension

(R.styleable.HorizontalProgressbarWithProgress_progress_reach_height,mReachHeight);

ta.recycle();

mPaint.setTextSize(mTextSize);

}

@Override

protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthVal=MeasureSpec.getSize(widthMeasureSpec);

int height=measureHeight(heightMeasureSpec);

setMeasuredDimension(widthVal,height);

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

}

private int measureHeight(int heightMeasureSpec) {

int result;

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()

+Math.max(Math.max(mReachHeight,mUnReachHeight),

Math.abs(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 bar

String text=getProgress()+"%";

int textWidth= (int) mPaint.measureText(text);

float radio=getProgress()*1.0f/getMax();

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 bar

if(!noNeedUnreach)

{

float start=progressX+mTextOffset/2+textWidth;

mPaint.setColor(mUnReachColor);

mPaint.setStrokeWidth(mUnReachHeight);

canvas.drawLine(start,0,mRealWidth,0,mPaint);

}

canvas.restore();

}

protected int dp2px(int dpval){

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpval,

getResources().getDisplayMetrics());

}

protected int sp2px(int spValue){

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,

getResources().getDisplayMetrics());

}

}

activity_main.xml

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:hyman="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

>

android:id="@+id/id_progress01"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:progress="50"

hyman:progress_reach_color="#ffff0000"

hyman:progress_text_color="#ffff0000"

hyman:progress_unreach_color="#ff000000"

android:padding="15dp"

android:background="#44ff0000"

android:layout_marginTop="30dp"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:progress="100"

android:padding="5dp"

hyman:progress_text_color="#44ff0000"

hyman:progress_unreach_color="#ffff0000"

android:layout_marginTop="30dp"

/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:progress="30"

android:padding="5dp"

hyman:progress_text_color="#44ff0000"

hyman:progress_unreach_color="#ffff0000"

android:layout_marginTop="30dp"

/>

MainActivity.java

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

private HorizontalProgressbarWithProgress mHProgress;

private static final int MSG_UPDATE=0x110;

private Handler mHandler=new Handler(){

@Override

public void handleMessage(Message msg) {

int progress=mHProgress.getProgress();

mHProgress.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);

mHProgress= (HorizontalProgressbarWithProgress) findViewById(R.id.id_progress01);

mHandler.sendEmptyMessage(MSG_UPDATE);

}

}

ae1f4089ffcc

Paste_Image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值