Android 自定义进度条

2.png

github:https://github.com/HarryXR/android

  • 水平进度条
    测量
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    
 int textHeight = ViewUtils.getTextHeight(mAlertTextPaint);   
 int height = 0; 
 if (!TextUtils.isEmpty(mLeftAlert) ||     
              !TextUtils.isEmpty(mRightAlert))
  {  
      height += textHeight;  //加上文字高度
  } 
 height += LINE_MARGIN;   
 height += STROKE_WIDTH * 2;   
 height += LINE_MARGIN;
 if (!TextUtils.isEmpty(mLeftContent) || !TextUtils.isEmpty(mRightContent))
 {      
  height += textHeight;   
 }   
 setMeasuredDimension(widthMeasureSpec, height);}

ViewUtils测量文本高度

public static int getTextHeight(Paint paint) {  
  Paint.FontMetrics fontMetrics = paint.getFontMetrics(); 
  return (int) Math.ceil(fontMetrics.descent - fontMetrics.ascent);}

画进度

private void drawProgress(Canvas canvas) {
    //画灰色进度  
  Paint bgPaint = getProgressPaint();   
  bgPaint.setColor(mProgressBg);   
 //左右扣除半个圆距离  

  canvas.drawLine(STROKE_WIDTH / 2, mHeight / 2, mWidth -    
   STROKE_WIDTH / 2, mHeight / 2, bgPaint);
    //画绿色进度   
  Paint progressPaint = getProgressPaint();      
  progressPaint.setColor(mProgressColor); 
  int stopX = (int) ((mWidth - STROKE_WIDTH) * mProgress);    
canvas.drawLine(STROKE_WIDTH / 2, mHeight / 2, stopX, mHeight / 2, progressPaint);  
  //画指示   
   Paint textPaint = getIndicatorPaint(); 
   int textWidth = ViewUtils.getTextWidth(textPaint, mAlert);
   Paint indicatorPaint = getProgressPaint();    
indicatorPaint.setColor(mIndicatorBg);    
indicatorPaint.setStrokeWidth(ALERT_STROKE_WIDTH);    
canvas.drawLine(stopX - textWidth / 2, mHeight / 2, stopX + textWidth / 2, mHeight / 2, indicatorPaint);  

  //画指示文字  
  Paint.FontMetrics fm = textPaint.getFontMetrics();

  int alertY = mHeight / 2 + (int) (Math.abs(fm.bottom + fm.top)) / 2;   
  canvas.drawText(mAlert, stopX - textWidth / 2, alertY, textPaint);}
  • 圆环
    大家看源码吧,涉及到一些数学公式的计算,原理类似
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值