packagecom.demo.demomutiprogress;importjava.util.ArrayList;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Bitmap;importandroid.graphics.Bitmap.Config;importandroid.graphics.BitmapFactory;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.Point;importandroid.graphics.Rect;importandroid.graphics.drawable.Drawable;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.View;/*** 多节点进度条自定义视图

*@authorhuqiang

**/

public class MutiProgress extendsView{private int nodesNum ; //节点数量

private Drawable progressingDrawable; //进行中的图标

privateDrawable unprogressingDrawable;private Drawable progresFailDrawable; //失败的节点

private Drawable progresSuccDrawable; //成功的节点

private int nodeRadius; //节点的半径

private int processingLineColor; //进度条的颜色//private int progressLineHeight;//进度条的高度

private int currNodeNO; //当前进行到的节点编号。从0开始计算

private int currNodeState; //当前进行到的节点编号所对应的状态 0:失败 1:成功//private int textSize;//字体大小

Context mContext;intmWidth,mHeight;privatePaint mPaint;privateCanvas mCanvas;private Bitmap mBitmap; //mCanvas绘制在这上面

private ArrayListnodes;private int DEFAULT_LINE_COLOR =Color.BLUE;publicMutiProgress(Context context) {this(context,null);

}publicMutiProgress(Context context, AttributeSet attrs) {this(context,attrs,0);

}public MutiProgress(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);

mContext=context;

TypedArray mTypedArray=context.obtainStyledAttributes(attrs,R.styleable.MutiProgress);

nodesNum= mTypedArray.getInteger(R.styleable.MutiProgress_nodesNum, 1); //默认一个节点

nodeRadius = mTypedArray.getDimensionPixelSize(R.styleable.MutiProgress_nodeRadius, 10); //节点半径

progressingDrawable =mTypedArray.getDrawable(R.styleable.MutiProgress_progressingDrawable);

unprogressingDrawable=mTypedArray.getDrawable(R.styleable.MutiProgress_unprogressingDrawable);

progresFailDrawable=mTypedArray.getDrawable(R.styleable.MutiProgress_progresFailDrawable);

progresSuccDrawable=mTypedArray.getDrawable(R.styleable.MutiProgress_progresSuccDrawable);

processingLineColor=mTypedArray.getColor(R.styleable.MutiProgress_processingLineColor, DEFAULT_LINE_COLOR);

currNodeState= mTypedArray.getInt(R.styleable.MutiProgress_currNodeState, 1);

currNodeNO= mTypedArray.getInt(R.styleable.MutiProgress_currNodeNO, 1);

}

@Overrideprotected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mWidth=getMeasuredWidth();

mHeight=getMeasuredHeight();

mBitmap=Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);

mPaint= newPaint();

mPaint.setColor(processingLineColor);

mPaint.setAntiAlias(true);

mPaint.setStrokeJoin(Paint.Join.ROUND);//圆角

mPaint.setStrokeCap(Paint.Cap.ROUND); //圆角

mCanvas = newCanvas(mBitmap);

nodes= new ArrayList();float nodeWidth = ((float)mWidth)/(nodesNum-1);for(int i=0;i

{

Node node= newNode();if(i==0)

node.mPoint= new Point(((int)nodeWidth*i),mHeight/2-nodeRadius);else if(i==(nodesNum-1))

node.mPoint= new Point(((int)nodeWidth*i)-nodeRadius*2,mHeight/2-nodeRadius);elsenode.mPoint= new Point(((int)nodeWidth*i)-nodeRadius,mHeight/2-nodeRadius);if(currNodeNO ==i)

node.type= 1; //当前进度所到达的节点

elsenode.type= 0; //已完成

nodes.add(node);

}

}

@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);

DrawProgerss();

Log.v("ondraw", "mBitmap="+mBitmap);if(mBitmap!=null)

{

canvas.drawBitmap(mBitmap,new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight()), new Rect(0,0,mBitmap.getWidth(),mBitmap.getHeight()), mPaint);

}for(int i=0;i

{

Node node=nodes.get(i);

Log.v("ondraw", node.mPoint.x +";y="+node.mPoint.y);if(i

{

progressingDrawable.setBounds(node.mPoint.x, node.mPoint.y , node.mPoint.x+ nodeRadius*2,node.mPoint.y + nodeRadius*2);

progressingDrawable.draw(canvas);

}else if(i==currNodeNO) //当前所到达的进度节点(终点)

{if(currNodeState == 1) //判断是成功还是失败 0 :失败 1:成功

{

progresSuccDrawable.setBounds(node.mPoint.x, node.mPoint.y , node.mPoint.x+ nodeRadius*2,node.mPoint.y + nodeRadius*2);

progresSuccDrawable.draw(canvas);

}else{

progresFailDrawable.setBounds(node.mPoint.x, node.mPoint.y , node.mPoint.x+ nodeRadius*2,node.mPoint.y + nodeRadius*2);

progresFailDrawable.draw(canvas);

}

}else //未完成的进度节点

{

unprogressingDrawable.setBounds(node.mPoint.x, node.mPoint.y , node.mPoint.x+ nodeRadius*2,node.mPoint.y + nodeRadius*2);

unprogressingDrawable.draw(canvas);

}

}

}private voidDrawProgerss()

{//先画背景

Paint bgPaint = newPaint();

bgPaint.setColor(Color.parseColor("#f0f0f0"));

mCanvas.drawRect(0, 0, mWidth, mHeight, bgPaint);//先画线段,线段的高度为nodeRadius/2

mPaint.setStrokeWidth(nodeRadius/2);//前半截线段//mCanvas.drawLine(nodeRadius, mHeight/2, mWidth-nodeRadius, mHeight/2, mPaint);//线段2端去掉nodeRadius

mCanvas.drawLine(nodeRadius, mHeight/2, nodes.get(currNodeNO).mPoint.x + nodeRadius, nodes.get(currNodeNO).mPoint.y + nodeRadius, mPaint); //线段2端去掉nodeRadius//后半截线段

mPaint.setColor(Color.parseColor("#dddddd"));

mCanvas.drawLine(nodes.get(currNodeNO).mPoint.x+nodeRadius, nodes.get(currNodeNO).mPoint.y + nodeRadius, mWidth-nodeRadius, mHeight/2, mPaint); //线段2端去掉nodeRadius

}classNode

{

Point mPoint;int type; //0:已完成 1:当前到达的进度节点

}

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐