Android studio圆形进度条 百分数跟随变化

Android studio圆形进度条 百分数跟随变化,第1张

概述本文实例为大家分享了Androidstudio圆形进度条展示的具体代码,供大家参考,具体内容如下

本文实例为大家分享了AndroID studio圆形进度条展示的具体代码,供大家参考,具体内容如下

MainActivity

import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{ private Gua mGua1; private button play; private button resele; private button dao; @OverrIDe protected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_main);  mGua1 = (Gua) findVIEwByID(R.ID.Circle);  mGua1.setTargetPercent(0);  play=(button)findVIEwByID(R.ID.play);  resele=(button)findVIEwByID(R.ID.resele);  dao=(button)findVIEwByID(R.ID.dao);  play.setonClickListener(this);  resele.setonClickListener(this);  dao.setonClickListener(this); } @OverrIDe public voID onClick(VIEw vIEw) {  switch (vIEw.getID()){   case R.ID.play:    //设置目标百分比为100    mGua1.setTargetPercent(100);    mGua1.invalIDate();    break;   case R.ID.resele:    //设置目标百分比为0    mGua1.setTargetPercent(0);    mGua1.invalIDate();    break;   case R.ID.dao:    //设置目标百分比为100    mGua1.setTargetPercent(0);    mGua1.invalIDate();    break;  } }}

Gua

import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.graphics.Paint.Align;import androID.graphics.RectF;import androID.util.AttributeSet;import androID.vIEw.VIEw;public class Gua extends VIEw{ private Paint mCirclePaint; private Paint mTextPaint; private Paint marcPaint; private int mCircleX; private int mCircleY; private float mCurrentAngle; private RectF marcRectF; private float mStartSweepValue; private float mTargetPercent; private float mCurrentPercent; private int mRadius; private int mCircleBackground; private int mRingcolor; private int mTextSize; private int mTextcolor; public Gua(Context context,AttributeSet attrs,int defStyle) {  super(context,attrs,defStyle);  init(context); } public Gua(Context context,AttributeSet attrs) {  super(context,attrs);  //自定义属性 values/attr  TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.PercentageRing);  //中间圆的背景颜色 默认为浅紫色  mCircleBackground = typedArray.getcolor(R.styleable.PercentageRing_circleBackground,0xffafb4db);  //外圆环的颜色 默认为深紫色  mRingcolor = typedArray.getcolor(R.styleable.PercentageRing_ringcolor,0xff6950a1);  //中间圆的半径 默认为60  mRadius = typedArray.getInt(R.styleable.PercentageRing_radius,60);  //字体颜色 默认为白色  mTextcolor = typedArray.getcolor(R.styleable.PercentageRing_textcolor,0xffffffff);  //最后一定要调用这个 释放掉TypedArray  typedArray.recycle();  //初始化数据  init(context); } public Gua(Context context) {  super(context);  init(context); } private voID init(Context context){  //圆环开始角度 -90° 正北方向  mStartSweepValue = -90;  //当前角度  mCurrentAngle = 0;  //当前百分比  mCurrentPercent = 0;  //设置中心园的画笔  mCirclePaint = new Paint();  mCirclePaint.setAntiAlias(true);  mCirclePaint.setcolor(mCircleBackground);  mCirclePaint.setStyle(Paint.Style.FILL);  //设置文字的画笔  mTextPaint = new Paint();  mTextPaint.setcolor(mTextcolor);  mTextPaint.setAntiAlias(true);  mTextPaint.setStyle(Paint.Style.FILL);  mTextPaint.setstrokeWIDth((float) (0.025*mRadius));  mTextPaint.setTextSize(mRadius/2);  mTextPaint.setTextAlign(Align.CENTER);  //设置外圆环的画笔  marcPaint = new Paint();  marcPaint.setAntiAlias(true);  marcPaint.setcolor(mRingcolor);  marcPaint.setStyle(Paint.Style.stroke);  marcPaint.setstrokeWIDth((float) (0.075*mRadius));  //获得文字的字号 因为要设置文字在圆的中心位置  mTextSize = (int) mTextPaint.getTextSize(); } //主要是测量wrap_content时候的宽和高,因为宽高一样,只需要测量一次宽即可,高等于宽 @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {  setMeasuredDimension(measure(wIDthMeasureSpec),measure(wIDthMeasureSpec));  //设置圆心坐标  mCircleX = getMeasureDWIDth()/2;  mCircleY = getMeasuredHeight()/2;  //如果半径大于圆心横坐标,需要手动缩小半径的值,否则就画到外面去了  if (mRadius>mCircleX) {   //设置半径大小为圆心横坐标到原点的距离   mRadius = mCircleX;   mRadius = (int) (mCircleX-0.075*mRadius);   //因为半径改变了,所以要重新设置一下字体宽度   mTextPaint.setstrokeWIDth((float) (0.025*mRadius));   //重新设置字号   mTextPaint.setTextSize(mRadius/2);   //重新设置外圆环宽度   marcPaint.setstrokeWIDth((float) (0.075*mRadius));   //重新获得字号大小   mTextSize = (int) mTextPaint.getTextSize();  }  //画中心园的外接矩形,用来画圆环用  marcRectF = new RectF(mCircleX-mRadius,mCircleY-mRadius,mCircleX+mRadius,mCircleY+mRadius); } //当wrap_content的时候,vIEw的大小根据半径大小改变,但最大不会超过屏幕 private int measure(int measureSpec){  int result=0;  int specMode = MeasureSpec.getMode(measureSpec);  int specsize = MeasureSpec.getSize(measureSpec);  if (specMode == MeasureSpec.EXACTLY) {   result = specsize;  }else {   result =(int) (1.075*mRadius*2);   if (specMode == MeasureSpec.AT_MOST) {    result = Math.min(result,specsize);   }  }  return result; } //开始画中间圆、文字和外圆环 @OverrIDe protected voID onDraw(Canvas canvas) {  super.onDraw(canvas);  //画中间圆  canvas.drawCircle(mCircleX,mCircleY,mRadius,mCirclePaint);  //画圆环  canvas.drawArc(marcRectF,mStartSweepValue,mCurrentAngle,false,marcPaint);  //画文字  canvas.drawText(String.valueOf(mCurrentPercent)+"%",mCircleX,mCircleY+mTextSize/4,mTextPaint);  //判断当前百分比是否小于设置目标的百分比  if (mCurrentPercent<mTargetPercent) {   //当前百分比+1   mCurrentPercent+=1;   //当前角度+360   mCurrentAngle+=3.6;   //每10ms重画一次   postInvalIDateDelayed(10);  }/*else if(mCurrentPercent==mTargetPercent){   //当前百分比-1   mCurrentPercent=0;   //当前角度+360   mCurrentAngle=0;   //每10ms重画一次   postInvalIDateDelayed(10);  }*/else if(mCurrentPercent>mTargetPercent){   //当前百分比-1   mCurrentPercent-=1;   //当前角度+360   mCurrentAngle-=3.6;   //每10ms重画一次   postInvalIDateDelayed(10);  } } //设置目标的百分比 public voID setTargetPercent(int percent){  this.mTargetPercent = percent; }}

Xml文件

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:orIEntation="vertical"> <button  androID:ID="@+ID/but1"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:text="改变外层圆环颜色"  /> <com.bwIE.test.wuxiaorui1508a20171009.Gua  androID:ID="@+ID/Circle"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  app:radius="90"  app:textcolor="#ffffffff"  /> <linearLayout  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:orIEntation="horizontal"  androID:gravity="center"  >  <button   androID:ID="@+ID/play"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:text="开始"   />  <button   androID:ID="@+ID/resele"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:text="重置"   />  <button   androID:ID="@+ID/dao"   androID:layout_wIDth="wrap_content"   androID:layout_height="wrap_content"   androID:text="暂停"   /> </linearLayout></linearLayout>

values文件中的attrs

<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="PercentageRing">  <attr name="radius" format="integer"/>  <attr name="circleBackground" format="color"/>  <attr name="ringcolor" format="color"/>  <attr name="textcolor" format = "color"/> </declare-styleable></resources>

效果展示:

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

总结

以上是内存溢出为你收集整理的Android studio圆形进度条 百分数跟随变化全部内容,希望文章能够帮你解决Android studio圆形进度条 百分数跟随变化所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144004.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存