进度框
import androID.content.Context; import androID.graphics.Canvas; import androID.graphics.color; import androID.graphics.Paint; import androID.util.AttributeSet; import androID.vIEw.VIEw; import java.util.Random; public class ObliqueProgressbar extends VIEw { private Paint mPaint; private float mProgress; public ObliqueProgressbar(Context context) { this(context,null); } public ObliqueProgressbar(Context context,AttributeSet attrs) { this(context,attrs,0); } public ObliqueProgressbar(Context context,AttributeSet attrs,int defStyleAttr) { super(context,defStyleAttr); mPaint = new Paint(); mPaint.setAntiAlias(true); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); if (mProgress == 0) return; //碎片雨 mPaint.setcolor(color.parsecolor("#a96ecb")); mPaint.setstrokeWIDth(3); Random random = new Random(); int sx,sy; for (int i = 0; i < 200; i++) { sx = random.nextInt(getWIDth() + 10); sy = random.nextInt(getHeight() + 10); // canvas.drawline(sx,sy,sx+random.nextInt(5),sy+random.nextInt(5),mPaint); canvas.drawCircle(sx,random.nextInt(5) + 1,mPaint); } //进度 mPaint.setcolor(color.parsecolor("#6AFFFFFF")); mPaint.setstrokeWIDth(15); float x = mProgress * getWIDth(); for (int i = 0; i < x; i += 30) { canvas.drawline(i - 30,-10,i + 30,getHeight() + 10,mPaint); } } public voID setProgress(float progress) { this.mProgress = progress; invalIDate(); } }
环形进度条
先来看一下效果:
下面直接上代码了:
ckage com.stone.circleprogressbar.vIEw; import androID.content.Context; import androID.content.res.TypedArray; import androID.graphics.Bitmap; import androID.graphics.Canvas; import androID.graphics.color; import androID.graphics.linearGradIEnt; import androID.graphics.Paint; import androID.graphics.RectF; import androID.graphics.Shader; import androID.util.AttributeSet; import androID.vIEw.VIEw; import com.stone.circleprogressbar.R; public class CircleProgressbar extends VIEw { private float mProgress; private int mbarcolor; private int mTextcolor; private float mTextSize; public CircleProgressbar(Context context) { this(context,null); } public CircleProgressbar(Context context,AttributeSet attrs) { this(context,0); } public CircleProgressbar(Context context,int defStyleAttr) { super(context,defStyleAttr); TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CircleProgressbar); mbarcolor = array.getcolor(R.styleable.CircleProgressbar_barcolor,color.GRAY); mTextcolor = array.getcolor(R.styleable.CircleProgressbar_textcolor,color.GRAY); mProgress = array.getfloat(R.styleable.CircleProgressbar_progress,0); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); } public voID setProgress(float count) { mProgress = count; invalIDate(); } @OverrIDe protected voID onDraw(Canvas canvas) { int w = getWIDth(); int h = getHeight(); int strokeWIDth = 20; int radius = w / 2 - strokeWIDth / 2;//大圆 半径 Bitmap barBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888); Paint paint = new Paint(); paint.setAntiAlias(true); //抗锯齿 paint.setstrokeWIDth(strokeWIDth); //描边宽 paint.setDither(true); //防抖动 /* 外边框 */ Canvas circleCanvas = new Canvas(barBitmap); RectF rect = new RectF(0,w,h); paint.setcolor(color.argb(0x11,0xcc,0xcc)); // circleCanvas.drawRect(rect,paint); //没啥用 只是看外边框的 /* 内圆 */ paint.setcolor(color.CYAN); paint.setShader(new linearGradIEnt(0,color.RED,color.GREEN,Shader.TileMode.CLAMP)); circleCanvas.drawCircle(w / 2,h / 2,radius - strokeWIDth / 2,paint); paint.setShader(null); /* 外圆 */ paint.setcolor(mbarcolor); paint.setStyle(Paint.Style.stroke); circleCanvas.drawCircle(w / 2,radius,paint); /* 圆弧 */ paint.setShader(new linearGradIEnt(0,new int[]{color.GREEN,color.magenta,color.CYAN,color.RED},new float[]{0.2f,0.5f,0.7f,1.0f},Shader.TileMode.CLAMP)); float cur = mProgress * 360 * 0.01f; circleCanvas.drawArc(new RectF(strokeWIDth / 2,strokeWIDth / 2,w - strokeWIDth / 2,h - strokeWIDth / 2),360 - 45,cur,false,paint); paint.setShader(null); /* 文本 */ paint.setcolor(mTextcolor); if (mTextSize == 0) { calcTextSize(paint,strokeWIDth); } else { paint.setTextSize(mTextSize); } paint.setTextAlign(Paint.Align.left);//default String percent = mProgress + "%"; paint.setStyle(Paint.Style.FILL); circleCanvas.drawText(percent,w / 2 - paint.measureText(percent) / 2,h / 2 + paint.getTextSize() / 2,paint); canvas.drawBitmap(barBitmap,paint); } /** * 计算并设置最适合的textSize * * @param paint * @param max 最大宽度 * @param offset 偏移 */ private voID calcTextSize(Paint paint,int max,int offset) { float wIDth = paint.measureText("99.99%"); while (wIDth < max * 3 / 5) { paint.setTextSize(paint.getTextSize() + 5); wIDth = paint.measureText("92.88%") + offset / 2; } mTextSize = paint.getTextSize(); } }
设置进度刷新显示 调用 setProgress()即可。
总结以上是内存溢出为你收集整理的Android中制作进度框和环形进度条的简单实例分享全部内容,希望文章能够帮你解决Android中制作进度框和环形进度条的简单实例分享所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)