当前位置: 移动技术网 > IT编程>移动开发>Android > Android自定义带圆点的半圆形进度条

Android自定义带圆点的半圆形进度条

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

盛大易购,黑暗圣经动漫下载,新超越极限2.08

本文实例为大家分享了android自定义带圆点的半圆形进度条,供大家参考,具体内容如下

仅限用于半圆形,如须要带圆点的圆形进度条,圆点会出现错位现象,此代码仅供,带圆点的圆形进度条有空研究一下!图片效果在下方,

import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.rectf;
import android.util.attributeset;
import android.view.view;

/**
 * 自定义带圆点的进度条
 */
public class halfprogressbar extends view{
 private int maxprogress = 100;
 //设置进度条背景宽度
 private float progressstrokewidth = 3;
 //设置进度条进度宽度
 private float marxarcstorkewidth = 6;
 //设置进度条圆点的宽度
 private float circulardotwidth=15;


 /**
  * 画笔对象的引用
  */
 private paint paint;

 public synchronized int getprogress() {
  return progress;
 }

 /**
  * android提供了invalidate方法实现界面刷新,但是invalidate不能直接在线程中调用,因为他是违背了单线程模型:android ui操作并不是线程安全的,并且这些操作必须在ui线程中调用。
  * 而postinvalidate()在工作者线程中被调用 使用postinvalidate则比较简单,不需要handler,直接在线程中调用postinvalidate即可。 
  * @param progress 传过来的进度
  */
 public void setprogress(int progress) {
  if (progress < 0) {
   progress = 0;
  }
  if (progress > maxprogress) {
   progress = maxprogress;
  }
  if (progress <= maxprogress) {
   this.progress = progress;
   postinvalidate();
  }
 }
 /**
  * 当前进度
  */
 private int progress = 99;

 private rectf oval;
 private int roundprogresscolor;
 private int roundcolor;
 private int circulardotcolor;
 public halfprogressbar(context context) {
  super(context);
 }

 public halfprogressbar(context context, attributeset attrs) {
  super(context, attrs);
  paint = new paint();
  oval = new rectf();
  //这是自定义view 必须要写的
  typedarray mtypedarray = context.obtainstyledattributes(attrs, r.styleable.halfprogressbar);
  roundprogresscolor = mtypedarray.getcolor(r.styleable.halfprogressbar_roundprogresscolor1, color.yellow);
  roundcolor=mtypedarray.getcolor(r.styleable.halfprogressbar_roundcolor1, color.yellow);
  circulardotcolor=mtypedarray.getcolor(r.styleable.halfprogressbar_circulardotcolor1, color.yellow);

 }

 public halfprogressbar(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  paint = new paint();
  oval = new rectf();
  typedarray mtypedarray = context.obtainstyledattributes(attrs, r.styleable.halfprogressbar);
  roundprogresscolor = mtypedarray.getcolor(r.styleable.halfprogressbar_roundprogresscolor1, color.yellow);
  roundcolor=mtypedarray.getcolor(r.styleable.halfprogressbar_roundcolor1, color.yellow);

 }

 @override
 protected void ondraw(canvas canvas) {
  // todo 自动生成的方法存根
  super.ondraw(canvas);
  float width = getwidth();
  float height = getheight();
  paint.setantialias(false); // 设置画笔为抗锯齿
  paint.setcolor(roundcolor); // 设置画笔颜色

  paint.setstrokewidth(progressstrokewidth); // 线宽
  paint.setstyle(paint.style.stroke);

  oval.left = marxarcstorkewidth / 2; // 左上角x
  oval.top = circulardotwidth; // 左上角y
  oval.right = width - circulardotwidth / 2; // 左下角x
  oval.bottom = width - circulardotwidth / 2; // 右下角y
  float bangjing = ((width - circulardotwidth/2) / 2);//半径


  //调整圆背景的大小
  canvas.drawarc(oval, 180, 180, false, paint); // 绘制红丝圆圈,即进度条背景
  //进度条颜色
  paint.setcolor(roundprogresscolor);
  paint.setstrokewidth(marxarcstorkewidth);
  canvas.drawarc(oval, 180, 180 * ((float) progress / (float) maxprogress), false, paint); // 绘制进度圆弧,这里是蓝色


  //画圆点
  paint.setcolor(circulardotcolor);
  paint.setantialias(true); // 设置画笔为抗锯齿
  paint.setstyle(paint.style.fill);
  paint.setstrokewidth(circulardotwidth);
  //当画笔样式为stroke或fill_or_stroke时,设置笔刷的图形样式,如圆形样式cap.round,或方形样式cap.square
  paint.setstrokecap(paint.cap.round);
  float jindu = ((float) progress * 1.8f);
  canvas.drawpoint(bangjing - ((float) (math.sin((math.pi / (double) 180) * (jindu <= 90 ? 90 - (jindu) : -jindu + 90))) * bangjing),
   bangjing+circulardotwidth - ((float) (math.cos((math.pi / (double) 180) * (double) (jindu <= 90 ? 90 - jindu : -jindu + 90))) * bangjing), paint);

 }

}

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <!--自定义半圆形加载进度条-->
 <declare-styleable name="halfprogressbar">
  <attr name="roundcolor1" format="color"/>
  <attr name="roundprogresscolor1" format="color"/>
  <attr name="circulardotcolor1" format="color"/>
 </declare-styleable>
</resources>

xml中

<com.jyc99.demo.halfprogressbar
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/view"
  android:layout_centerhorizontal="true"
  android:layout_margintop="42dp"
  android_custom:roundcolor1="#fc422b"
  android_custom:roundprogresscolor1="#fa432e"
  android_custom:circulardotcolor1="#246223"/>

由于截图的原因可能看不到圆点 , 大家自己试试调调颜色 调整一下高度宽度

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

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

相关文章:

验证码:
移动技术网