欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

仿华为天气预报圆形刻度图的代码实现

程序员文章站 2023-01-23 08:03:27
仿华为天气预报圆形刻度图的代码实现 public class scaleprogressview extends view { private int len; private fina...

仿华为天气预报圆形刻度图的代码实现

public class scaleprogressview extends view {
 private int len;
 private final int startangle = 40;
 private final int sweepangle = 280;
 private final int rotateanglenum = 70;
 private paint bgpaint;
 private paint linepaint;
 private paint arrowpaint;
 private int percentrotateanglenum;
 private float currentprogress;
 private int arrowwidth = densityutil.dp2px(6);
 private int arrowheight = densityutil.dp2px(10);
 private int radius;
 private int scalewidth = densityutil.dp2px(3);
 private int scaleheight = densityutil.dp2px(8);
 private float rotateangle;
 private valueanimator animator;

 public scaleprogressview(context context) {
  this(context, null, 0);
 }

 public scaleprogressview(context context, @nullable attributeset attrs) {
  this(context, attrs, 0);
 }

 public scaleprogressview(context context, @nullable attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  init();
 }

 private void init() {
  bgpaint = new paint();
  bgpaint.setcolor(color.parsecolor("#66ffffff"));
  bgpaint.setantialias(true);
  bgpaint.setstyle(paint.style.stroke);
  bgpaint.setstrokewidth(scalewidth);
  bgpaint.setstrokecap(paint.cap.round);
  linepaint = new paint();
  linepaint.setcolor(color.green);
  linepaint.setantialias(true);
  linepaint.setstyle(paint.style.stroke);
  linepaint.setstrokewidth(scalewidth);
  linepaint.setstrokecap(paint.cap.round);
  arrowpaint = new paint();
  arrowpaint.setcolor(color.parsecolor("#ffe400"));
  arrowpaint.setantialias(true);
  arrowpaint.setstyle(paint.style.fill);
  rotateangle = sweepangle / rotateanglenum;
 }

 private void startanimator() {
  if (animator == null) {
animator = valueanimator.offloat(0, 1);
  } else {
animator.cancel();
  }
  animator.addupdatelistener(new valueanimator.animatorupdatelistener() {
@override
public void onanimationupdate(valueanimator animation) {
 currentprogress = (float) animation.getanimatedvalue();
 postinvalidate();
}
  });
  animator.setduration(2000);
  animator.start();
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
  int width = measurespec.getsize(widthmeasurespec);
  int height = measurespec.getsize(heightmeasurespec);
  len = math.min(width, height);
  radius = len / 2 - arrowheight;
  setmeasureddimension(len, len);
 }

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

 private void drawbackgroundline(canvas canvas) {
  canvas.save();
  canvas.translate(len / 2, len / 2);
  canvas.rotate(startangle);
  canvas.drawline(0, radius, 0, radius - scaleheight, bgpaint);
  for (int i = 0; i < rotateanglenum; i++) {
canvas.rotate(rotateangle);
canvas.drawline(0, radius, 0, radius - scaleheight, bgpaint);
  }
  canvas.restore();
 }

 private void drawcurrentline(canvas canvas) {
  canvas.save();
  canvas.translate(len / 2, len / 2);
  canvas.rotate(startangle);
  canvas.drawline(0, radius, 0, radius - scaleheight, linepaint);
  for (int i = 0, size = (int) (percentrotateanglenum * currentprogress); i < size; i++) {
canvas.rotate(rotateangle);
canvas.drawline(0, radius, 0, radius - scaleheight, linepaint);
  }
  path path = new path();
  path.moveto(arrowwidth / 2, radius + arrowheight);
  path.lineto(-arrowwidth / 2, radius + arrowheight);
  path.lineto(-arrowwidth / 2, radius + densityutil.dp2px(3));
  path.lineto(0, radius);
  path.lineto(arrowwidth / 2, radius + densityutil.dp2px(3));
  path.close();
  canvas.drawpath(path, arrowpaint);
  canvas.restore();
 }

 public void setprogress(string progress) {
  if (textutils.isempty(progress)) {
percentrotateanglenum = rotateanglenum;
  } else {
percentrotateanglenum = (int) (float.parsefloat(progress) * rotateanglenum / 100);
  }
  startanimator();
 }
}