本文实例为大家分享了Android实现圆形进度条的具体代码,供大家参考,具体内容如下
实现的效果图如下所示:
第一步:绘制下方有缺口的空心圆,称为外围大弧吧
anvas.clipRect(0, 0, mWidth, mHeight / 2 + radius - textHeight * 3 / 4);
第二步:计算绘制圆弧进度条时的起始角度,设置为外围大弧的左端点为进度值得起点,扫过的角度所占外围大弧的百分比就是进度值
第三步:绘制数字、文字、百分号
第四步:使用Handler Runnable 和DecelerateInterpolator是进度条和数字动起来
测试代码:
final CustomCircleBar circle=(CustomCircleBar)findViewById(R.id.win_home); circle.setPercent(10); circle.setCustomText("呵呵"); circle.setProgessColor(getResources().getColor(R.color.blue)); final Random random=new Random(); circle.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ circle.setPercent(random.nextInt(100)); } });
完成代码如下:
public class CustomCircleBar extends View { private Context context; /** * 进度值 */ private int percent; /** * 颜色值 */ private int mProgessColor; /** * 下边的文字名称 */ private String mCustomText; /** * 外圈圆环的画笔 */ private Paint paintBar = new Paint(); /** * 下边文字的画笔 */ private Paint paintText = new Paint(); /** * 动态获取属性值 */ private TypedValue typedValue; /** * 先加速后减速 */ DecelerateInterpolator mDecelerateInterpolator = new DecelerateInterpolator(); /** * 动画持续时间 */ private int duration = 10; private int curTime = 0; public CustomCircleBar(Context context) { super(context); this.cOntext=context; init(); } public CustomCircleBar(Context context, AttributeSet attrs) { super(context, attrs); this.cOntext=context; init(); } public CustomCircleBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.cOntext=context; init(); } public void setPercent(int percent) { this.percent = percent; /*isShown():Returns the visibility of this view and all of its ancestors*/ if (isShown()) { /** * 设置进度后重新开始一次动画 */ curTime=0; this.invalidate(); } } public void setProgessColor(int mProgessColor) { this.mProgessColor = mProgessColor; if (isShown()) { this.invalidate(); } } public void setCustomText(String mCustomText) { this.mCustomText = mCustomText; } private Handler mHandler = new Handler(); private Runnable mAnimation = new Runnable() { @Override public void run() { if (curTime
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。