自定义圆形进度条(文字;动画{后面的动画会顶掉正在做的动画})

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import android.text.TextUtils;

import android.util.AttributeSet;

import android.view.View;

/**

* 圆形进度条,有动画效果.

*/

public classMainCircleProgressViewextendsView{

//最大进度

private doublemMaxProgress=100;

//目前进度

private doublemProgress=30;

// 画圆所在的距形区域

private finalRectFmRectF;

private finalPaintmPaint;

//文字变量

privateStringmTopText;

privateStringmCenterText;

privateStringmBottomText;

//管理线程,一个新动画线程进来,前面的动画线程会停止

private intmProgressManger=0;

publicMainCircleProgressView(Contextcontext,AttributeSetattrs) {

super(context,attrs);

mRectF=newRectF();

mPaint=newPaint();

mCenterText="";

}

@Override

protected voidonMeasure(intwidthMeasureSpec,intheightMeasureSpec) {

//谁比较宽就用谁

if(heightMeasureSpec<=widthMeasureSpec) {

super.onMeasure(heightMeasureSpec,heightMeasureSpec);

}else{

super.onMeasure(widthMeasureSpec,widthMeasureSpec);

}

}

@Override

protected voidonDraw(Canvascanvas) {

intcircleLineStrokeWidth=15;

inttextStrokeWidth=2;

super.onDraw(canvas);

intwidth=this.getWidth();

intheight=this.getHeight();

if(width!=height) {

intmin=Math.min(width,height);

width=min;

height=min;

}

// 设置画笔相关属性

mPaint.setAntiAlias(true);

mPaint.setColor(Color.rgb(83,88,88));

mPaint.setAlpha(50);

canvas.drawColor(Color.TRANSPARENT);

mPaint.setStrokeWidth(circleLineStrokeWidth);

mPaint.setStyle(Paint.Style.STROKE);

// 位置

mRectF.left=circleLineStrokeWidth/2;// 左上角x

mRectF.top=circleLineStrokeWidth/2;// 左上角y

mRectF.right=width-circleLineStrokeWidth/2;// 左下角x

mRectF.bottom=height-circleLineStrokeWidth/2;// 右下角y

// 绘制圆圈,进度条背景

canvas.drawArc(mRectF,-90,360,false,mPaint);

mPaint.setColor(Color.WHITE);

canvas.drawArc(mRectF,-90,(float) ((mProgress/mMaxProgress) *360),false,mPaint);

// 绘制中间文字

mPaint.setStrokeWidth(textStrokeWidth);

String text=mCenterText;

inttextHeight=height/4;

mPaint.setTextSize(textHeight);

inttextWidth= (int)mPaint.measureText(text,0,text.length());

mPaint.setStyle(Paint.Style.FILL);

canvas.drawText(text,width/2-textWidth/2,height/2+textHeight/2,mPaint);

// 绘制上面文字

textHeight=height/10;

if(!TextUtils.isEmpty(mTopText)) {

mPaint.setStrokeWidth(textStrokeWidth);

text=mTopText;

mPaint.setTextSize(textHeight);

mPaint.setColor(Color.WHITE);

textWidth= (int)mPaint.measureText(text,0,text.length());

mPaint.setStyle(Paint.Style.FILL);

canvas.drawText(text,width/2-textWidth/2,height/4+textHeight/2,mPaint);

}

// 绘制下面文字

if(!TextUtils.isEmpty(mBottomText)) {

mPaint.setStrokeWidth(textStrokeWidth);

text=mBottomText;

mPaint.setTextSize(textHeight);

textWidth= (int)mPaint.measureText(text,0,text.length());

mPaint.setStyle(Paint.Style.FILL);

canvas.drawText(text,width/2-textWidth/2,3*height/4+textHeight/2,mPaint);

}

}

//设置进程并刷新界面,UI线程实时刷新,干掉动画.

public voidsetProgress(doubleprogress) {

mProgressManger++;

this.mProgress= progress;

this.invalidate();

}

public voidsetProgressNotInUiThread(doubleprogress) {

this.mProgress= progress;

this.postInvalidate();

}

public voidsetMaxProgress(doublemaxProgress) {

this.mMaxProgress= maxProgress;

}

public voidsetBottomText(StringbottomText) {

this.mBottomText= bottomText;

}

public voidsetTopText(StringtopText) {

this.mTopText= topText;

}

public voidsetCenterText(StringcenterText) {

this.mCenterText= centerText;

}

public voidsetProgressAnimation(final doubleprogress){

final intnumber= ++mProgressManger;

newThread(newRunnable() {

@Override

public voidrun() {

if(progress==0) {

return;

}

doublefp=mMaxProgress*0.0025;

doublecurrentProgress=fp;

while(true) {

if(number!=mProgressManger){

return;

}

if(progress

setProgressNotInUiThread(progress);

return;

}else{

setProgressNotInUiThread(currentProgress);

currentProgress+=fp;

}

try{

Thread.sleep(5);

}catch(InterruptedExceptione) {

e.printStackTrace();

}

}

}

}).start();

}

}

你可能感兴趣的:(自定义圆形进度条(文字;动画{后面的动画会顶掉正在做的动画}))