android实现环形进度条
2013-12-10 16:45
323 查看
package cn.zyt.widgets; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; import cn.likeit.solar.R; public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 95; private int progressStrokeWidth = 4; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); if (width != height) { int min = Math.min(width, height); width = min; height = min; } canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(Color.parseColor("#f0f0f0")); // 设置画笔颜色 paint.setStrokeWidth(progressStrokeWidth * 3); // 线宽 paint.setStyle(Style.STROKE); // 线中心与四周的距离 oval.left = progressStrokeWidth * 1.5f; // 左上角x oval.top = progressStrokeWidth * 1.5f; // 左上角y oval.right = width - progressStrokeWidth * 1.5f; // 左下角x oval.bottom = height - progressStrokeWidth * 1.5f; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 paint.setColor(Color.parseColor("#238f31")); paint.setStrokeWidth(progressStrokeWidth); oval.left = progressStrokeWidth * 1.5f; // 左上角x oval.top = progressStrokeWidth * 1.5f; // 左上角y oval.right = width - progressStrokeWidth * 1.5f; // 左下角x oval.bottom = height - progressStrokeWidth * 1.5f; // 右下角y canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 String text = progress + "%"; int textHeight = height / 4; paint.setStrokeWidth(1); paint.setTextSize(textHeight); paint.setColor(Color.parseColor("#969696")); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); canvas.drawText(text, width / 2 - textWidth / 2, height / 2, paint); String status = getResources().getString(R.string.smu_status); textHeight = height / 6; paint.setColor(Color.parseColor("#c8c8c8")); paint.setTextSize(textHeight); textWidth = (int) paint.measureText(status, 0, status.length()); canvas.drawText(status, width / 2 - textWidth / 2, height / 2 + textHeight * 4 / 3, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } public void setProgress(int progress) { this.progress = progress; this.invalidate(); } /** * 非UI线程调用,setProgress方法只能在UI线程种进行工作,若是其他线程则调用此方法更新状态。 */ public void setProgressNotInUiThread(int progress) { this.progress = progress; this.postInvalidate(); } }
相关文章推荐
- Android 实现环形进度按钮circular-progress-button
- Android自定义控件实现环形播放进度条
- Android自定义View之实现环形进度条
- Android实现计步进度的环形Progress
- Android自定义控件实现环形播放进度条
- Android实现环形进度条代码
- android 利用shape实现环形进度条
- Android自定义控件实现环形播放进度条
- Android 实现环形进度按钮circular-progress-button
- AndroidUI系列 - View实现圆形进度条
- Android实现圆弧形渐变色进度条
- Android 使用 DowanloadManager 实现下载并获取下载进度实例代码
- Android自定义View——使用贝塞尔曲线实现流量进度条
- Android自定义View——从零开始实现水波浪进度框
- 使用 circular-progress-button 实现环形进度按钮
- Android中自定义Adapter实现ListView动态刷新进度条
- Android使用自定义View实现360手机卫士波浪球进度的效果
- Android文件下载进度条的实现
- 【Android TV端】RecyclewView中局部更新(实现item下载进度实时更新)
- 安卓自动更新,静默更新,替换友盟更新可以在通知栏里显示更新下载进度,几行代码快速实现Android下载更新