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

所属分类: 软件编程 / Android 阅读数: 52
收藏 0 赞 0 分享

本文实例为大家分享了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"/>

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

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

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多