d3a17b241d7964254a54c8184414ea48.gif

a36bc4a21e4b6276a9ecd5066e5e65b2.gif

7bbe1f13d7ba0071d872989bf329b761.gif

public class AminProgressBar extends View implements Runnable {

private static final int MODE_TYPE_ROUND = 1;

private static final int MODE_TYPE_SQUARE = 2;

private int mWidth;

private int mHeight;

private Paint paint;

private int MODE = MODE_TYPE_ROUND ;

private RectF rect;

private float borderSize = 20.0f;

private Bitmap progressBitmap;

private int mBoxPadding = 30;

private int mBoxSize = 30;

private Xfermode xfermode;

private int MAX_PROGRESS = 100;

private final List rectPoints = Collections.synchronizedList(new ArrayList());

private float itemHeight;

private int mProgress = 50;

public AminProgressBar(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

public AminProgressBar(Context context) {

this(context,null);

}

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

super(context, attrs, defStyleAttr);

initCanvas();

}

private void initCanvas() {

paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.STRIKE_THRU_TEXT_FLAG);

paint.setStrokeMiter(0f);

paint.setDither(true);

paint.setAntiAlias(true); //设置画笔为无锯齿

paint.setColor(0xffF3841A); //设置画笔颜色

paint.setStrokeWidth(borderSize); //线宽

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeCap(Paint.Cap.ROUND);

paint.setFilterBitmap(true);

xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);

setBackgroundColor(Color.TRANSPARENT);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//super.onMeasure(widthMeasureSpec, heightMeasureSpec);

/**

* 设置宽度

*/

int specMode = MeasureSpec.getMode(widthMeasureSpec);

int specSize = MeasureSpec.getSize(widthMeasureSpec);

if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate

{

mWidth = specSize;

}

else

{

// 由图片决定的宽

int desireByImg = getPaddingLeft() + getPaddingRight()

+ 380;

if (specMode == MeasureSpec.AT_MOST)// wrap_content

{

mWidth = Math.min(desireByImg, specSize);

} else

mWidth = desireByImg;

}

/***

* 设置高度

*/

specMode = MeasureSpec.getMode(heightMeasureSpec);

specSize = MeasureSpec.getSize(heightMeasureSpec);

if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate

{

mHeight = specSize;

} else

{

int desire = getPaddingTop() + getPaddingBottom()

+ 50;

if (specMode == MeasureSpec.AT_MOST)// wrap_content

{

mHeight = Math.min(desire, specSize);

} else

mHeight = desire;

}

setMeasuredDimension(mWidth, mHeight);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

if(rect==null){

rect = new RectF(borderSize/2,borderSize/2,getWidth()-borderSize/2,getHeight()-borderSize/2);

}

if(MODE==MODE_TYPE_SQUARE) {

int color = paint.getColor();

paint.setColor(Color.GRAY);

Paint.Style style = paint.getStyle();

paint.setStyle(Paint.Style.FILL);

int radius = getHeight() / 2;

canvas.drawRoundRect(rect, radius, radius, paint);

paint.setStyle(style);

paint.setColor(0xff333333);

canvas.drawRoundRect(rect, radius, radius, paint);

paint.setColor(color);

}

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

if(rectPoints!=null){

rectPoints.clear();

}

}

@Override

protected void dispatchDraw(Canvas canvas) {

super.dispatchDraw(canvas);

int progressWidth = (int)(getWidth()*mProgress*1.0f/MAX_PROGRESS);

if(progressWidth==0){

progressWidth = 1;

}

progressBitmap = Bitmap.createBitmap(progressWidth, (int)(getHeight()), Bitmap.Config.ARGB_8888);

Canvas progressCanvas = new Canvas(progressBitmap);

if(progressCanvas!=null){

progressCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

}

Paint.Style style = paint.getStyle();

int color = paint.getColor();

paint.setColor(0xffF3841A);

paint.setStyle(Paint.Style.FILL);

int radius = (int) ((getHeight() -borderSize)/2);

RectF prect = null;

int canvasHeight = (int) (getHeight() - borderSize);

if(MODE==MODE_TYPE_SQUARE){

prect = new RectF(borderSize, borderSize, getWidth() - borderSize, canvasHeight);

}else {

if(progressWidth<2*radius) {

radius = (int) ((progressWidth)/2);

int maxCanvasheight = canvasHeight;

if(canvasHeight/2>radius){

canvasHeight = radius*2;

}

int canvasBottom = (maxCanvasheight - canvasHeight)/2;

if(progressWidth<2*borderSize){

progressWidth = (int) borderSize*2;

}

prect = new RectF(borderSize, borderSize+canvasBottom, progressWidth - borderSize, canvasBottom+canvasHeight);

}else{

prect = new RectF(borderSize, borderSize, progressWidth - borderSize, canvasHeight);

}

}

progressCanvas.drawRoundRect(prect, radius, radius, paint);

paint.setXfermode(xfermode);

paint.setColor(Color.RED);

if(rectPoints.isEmpty())

{

int num = (int) Math.ceil((getWidth()-2*borderSize)*(1.0f)/(mBoxPadding+mBoxSize))+1;

for (int i = 0; i <= num; i++)

{

rectPoints.add(new Point((mBoxPadding+mBoxSize)*i, 0));

}

startMoving();

}

for (Point p : rectPoints)

{

Path path = new Path();

path.moveTo(p.x, borderSize/2);

itemHeight = progressCanvas.getHeight()-borderSize/2;

path.lineTo((float) (p.x-itemHeight/Math.tan(Math.toRadians(60))), itemHeight);

path.lineTo((float) (p.x-itemHeight/Math.tan(Math.toRadians(60))+mBoxSize), progressCanvas.getHeight()-borderSize/2);

path.lineTo(p.x+mBoxSize, borderSize/2);

path.close();

progressCanvas.drawPath(path, paint);

}

paint.setColor(color);

paint.setXfermode(null);

paint.setStyle(style);

paint.setColor(color);

canvas.drawBitmap(progressBitmap, 0, 0, paint);

progressBitmap.recycle();

}

private void startMoving() {

postDelayed(this, 20);

}

@Override

public void run() {

for (Point p : rectPoints)

{

if((p.x-itemHeight/Math.tan(Math.toRadians(60)))>getWidth())

{

int minPosX = rectPoints.get(0).x;

for (int i = 1; i < rectPoints.size(); i++) {

minPosX = Math.min(minPosX, rectPoints.get(i).x);

}

p.x =(int) (minPosX-mBoxSize-mBoxPadding);

}

else{

p.x +=2;

}

}

postDelayed(this, 20);

invalidate();

}

public void setProgress(int progress) {

this.mProgress = progress;

}

public void setMODE(int MODE) {

this.MODE = MODE;

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐