当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现简洁的APP更新dialog数字进度条

Android实现简洁的APP更新dialog数字进度条

2019年07月24日  | 移动技术网IT编程  | 我要评论

革命传统教育材料,九台一中,玉林一中

前言:现在一般的android软件都是需要不断更新的,当你打开某个app的时候,如果有新的版本,它会提示你有新版本需要更新。当有更新时,会弹出一个提示框,点击下载,则在通知来创建一个数字进度条进行下载,下载成功后才到安装界面。

效果: 

 

开发环境:androidstudio2.2.1+gradle-2.14.1

涉及知识:

    1.handler机制 

    2.自定义控件+canvas绘画 

    3.自定义dialog 

部分代码: 

public class numberprogressbar extends view {


 /**
  * 右侧未完成进度条的颜色
  */
 private int paintstartcolor = 0xffe5e5e5;

 /**
  * contxt
  */
 private context context;

 /**
  * 主线程传过来进程 0 - 100
  */
 private int progress;

 /**
  * 得到自定义视图的宽度
  */
 private int viewwidth;


 private rectf pieoval;

 private rectf pieovalin;

 /**
  * 得到自定义视图的y轴中心点
  */
 private int viewcentery;

 /**
  * 已完成的画笔
  */
 private paint paintinit = new paint();


 /**
  * 未完成进度条画笔的属性
  */
 private paint paintstart = new paint();

 /**
  * 大圆的画笔
  */
 private paint paintendbig = new paint();

 /**
  * 小圆的画笔
  */
 private paint paintsmall = new paint();


 /**
  * 画中间的百分比文字的画笔
  */
 private paint painttext = new paint();

 /**
  * 要画的文字的宽度
  */
 private int textwidth;

 /**
  * 画文字时底部的坐标
  */
 private float textbottomy;

 private int smallr;//小圆的半径
 private int bigr;//大圆半径
 private float radius;
 private int jr;//气泡矩形

 /**
  * 文字总共移动的长度(即从0%到100%文字左侧移动的长度)
  */
// private int totalmovedlength;
 public numberprogressbar(context context, attributeset attrs) {
  super(context, attrs);
  this.context = context;
  // 构造器中初始化数据
  smallr = dip2px(context, 4);//小圆半径
  bigr = dip2px(context, 8);//大圆半径
  radius = dip2px(context, 10) / 2;//进度条高度
  jr = dip2px(context, 6);//矩形

  initdata();
 }

 /**
  * 初始化数据
  */
 private void initdata() {

  // 未完成进度条画笔的属性
  paintstart.setcolor(paintstartcolor);
  paintstart.setstrokewidth(dip2px(context, 1));
  paintstart.setdither(true);
  paintstart.setantialias(true);
  paintstart.setstyle(paint.style.fill);

  // 已完成进度条画笔的属性
  paintinit.setcolor(context.getresources().getcolor(r.color.blue));
  paintinit.setstrokewidth(dip2px(context, 1));
  paintinit.setantialias(true);
  paintinit.setdither(true);
  paintinit.setstyle(paint.style.fill);


  // 小圆画笔
  paintsmall.setcolor(color.white);
  paintsmall.setantialias(true);
  paintsmall.setstyle(paint.style.fill);

  // 大圆画笔
  paintendbig.setcolor(context.getresources().getcolor(r.color.blue));
  paintendbig.setantialias(true);
  paintendbig.setstyle(paint.style.fill);


  // 百分比文字画笔的属性
  int painttextsizepx = sp2px(context, 11); //设置百分比文字的尺寸
  painttext.setcolor(context.getresources().getcolor(r.color.blue));
  painttext.settextsize(painttextsizepx);
  painttext.setantialias(true);
  painttext.settypeface(typeface.default_bold);

 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
 }


 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);

  //得到float型进度
  float progressfloat = progress / 100.0f;
  int viewheight = getmeasuredheight();//得到控件的高度

  viewwidth = getmeasuredwidth() - 4 * jr;

  viewcentery = viewheight - bigr;

  float currentmovedlen = viewwidth * progressfloat + 2 * jr;

  string str = progress + "%";

  rect bounds = new rect();
  painttext.gettextbounds(str, 0, str.length(), bounds);
  textwidth = bounds.width();
  textbottomy = bounds.height();
/**
 * 1:绘画的文本
 * 2.距离x的位移
 * 3.距离y的位移
 * 4.画笔对象
 */
  canvas.drawtext(str, currentmovedlen - textwidth / 2,
    viewcentery - smallr / 2 - bigr / 2 - 2 * jr + textbottomy / 2,
    painttext);//文字


  //圆角矩形初始的
  canvas.drawroundrect(new rectf(2 * jr, viewcentery - radius, currentmovedlen,
      viewcentery + radius),
    radius, radius, paintinit);

  //圆角矩形--进行中
  canvas.drawroundrect(new rectf(currentmovedlen, viewcentery - radius, viewwidth + 2 * jr,
    viewcentery + radius), radius, radius, paintstart);

  pieoval = new rectf(currentmovedlen - bigr, viewcentery - bigr, currentmovedlen + bigr, viewcentery + bigr);

  pieovalin = new rectf(currentmovedlen - smallr, viewcentery - smallr, currentmovedlen + smallr, viewcentery + smallr);

  //大圆
  canvas.drawarc(pieoval, 0, 360, true, paintendbig);

  //小圆
  canvas.drawarc(pieovalin, 0, 360, true, paintsmall);
 }

 /**
  * @param progress 外部传进来的当前进度
  */
 public void setprogress(int progress) {
  this.progress = progress;
  invalidate();
 }

 public static int dip2px(context ctx, float dp) {
  float density = ctx.getresources().getdisplaymetrics().density;
  int px = (int) (dp * density + 0.5f);
  return px;
 }

 public static int sp2px(context context, float spvalue) {
  return (int) typedvalue.applydimension(typedvalue.complex_unit_sp, spvalue, context.getresources().getdisplaymetrics());
 }
}

源码下载:dialog数字进度条

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网