Android中怎么自定义view实现圆环进度条效果
这篇文章主要讲解了“Android中怎么自定义view实现圆环进度条效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android中怎么自定义view实现圆环进度条效果”吧!
核心代码自定义view的属性
<?xmlversion="1.0"encoding="utf-8"?><resources><declare-styleablename="RingProgressBar"><attrname="ringColor"format="color"/><attrname="ringProgressColor"format="color"/><attrname="ringWidth"format="dimension"></attr><attrname="textColor"format="color"/><attrname="textSize"format="dimension"/><attrname="max"format="integer"></attr><attrname="textIsDisplayable"format="boolean"></attr><attrname="style"><enumname="STROKE"value="0"></enum><enumname="FILL"value="1"></enum></attr></declare-styleable></resources>
自定义view
packagecom.czhappy.ringprogressdemo.view;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.graphics.RectF;importandroid.graphics.Typeface;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.View;importcom.czhappy.ringprogressdemo.R;/***Description:*User:chenzheng*Date:2017/2/100010*Time:15:11*/publicclassRingProgressBarextendsView{/***画笔对象的引用*/privatePaintringPaint;privatePaintringProgressPaint;privatePainttxtPaint;/***圆环的颜色*/privateintringColor;/***圆环进度的颜色*/privateintringProgressColor;/***中间进度百分比的字符串的颜色*/privateinttextColor;/***中间进度百分比的字符串的字体*/privatefloattextSize;/***圆环的宽度*/privatefloatringWidth;/***最大进度*/privateintmax;/***当前进度*/privateintprogress;/***是否显示中间的进度*/privatebooleantextIsDisplayable;privateContextmContext;/***进度的风格,实心或者空心*/privateintstyle;publicstaticfinalintSTROKE=0;publicstaticfinalintFILL=1;publicRingProgressBar(Contextcontext){this(context,null);}publicRingProgressBar(Contextcontext,AttributeSetattrs){this(context,attrs,0);}publicRingProgressBar(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);this.mContext=context;//获取自定义的属性initAttrs(context,attrs);initPaint();}privatevoidinitAttrs(Contextcontext,AttributeSetattrs){TypedArraymTypedArray=context.obtainStyledAttributes(attrs,R.styleable.RingProgressBar);//获取自定义属性和默认值ringColor=mTypedArray.getColor(R.styleable.RingProgressBar_ringColor,Color.GRAY);ringProgressColor=mTypedArray.getColor(R.styleable.RingProgressBar_ringProgressColor,Color.GREEN);textColor=mTypedArray.getColor(R.styleable.RingProgressBar_textColor,Color.GREEN);textSize=mTypedArray.getDimension(R.styleable.RingProgressBar_textSize,16);ringWidth=mTypedArray.getDimension(R.styleable.RingProgressBar_ringWidth,5);max=mTypedArray.getInteger(R.styleable.RingProgressBar_max,100);textIsDisplayable=mTypedArray.getBoolean(R.styleable.RingProgressBar_textIsDisplayable,true);style=mTypedArray.getInt(R.styleable.RingProgressBar_style,0);//资源回收mTypedArray.recycle();}/***初始化画笔*/privatevoidinitPaint(){//圆环画笔ringPaint=newPaint();ringPaint.setColor(ringColor);//设置圆环的颜色ringPaint.setStyle(Paint.Style.STROKE);//设置空心ringPaint.setStrokeWidth(ringWidth);//设置圆环的宽度ringPaint.setAntiAlias(true);//消除锯齿//圆环进度画笔ringProgressPaint=newPaint();ringProgressPaint.setColor(ringProgressColor);//设置圆环的颜色ringProgressPaint.setStrokeWidth(ringWidth);//设置圆环的宽度ringProgressPaint.setAntiAlias(true);//消除锯齿switch(style){caseSTROKE:ringProgressPaint.setStyle(Paint.Style.STROKE);break;caseFILL:ringProgressPaint.setStyle(Paint.Style.FILL_AND_STROKE);break;}//百分比文字画笔txtPaint=newPaint();txtPaint.setColor(textColor);txtPaint.setTextSize(textSize);txtPaint.setTypeface(Typeface.DEFAULT_BOLD);//设置字体}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);//圆心坐标intmXCenter=getWidth()/2;intmYCenter=getHeight()/2;intradius=(int)(mXCenter-ringWidth/2);//圆环的半径//绘制圆环canvas.drawCircle(mXCenter,mYCenter,radius,ringPaint);//绘制圆环进度RectFoval=newRectF(mXCenter-radius,mYCenter-radius,mXCenter+radius,mYCenter+radius);//用于定义的圆弧的形状和大小的界限switch(style){caseSTROKE:canvas.drawArc(oval,-90,360*progress/max,false,ringProgressPaint);//根据进度画圆弧break;caseFILL:if(progress!=0)canvas.drawArc(oval,-90,360*progress/max,true,ringProgressPaint);//根据进度画圆弧break;}//绘制百分比数字//文字绘制Stringtxt=progress+"%";//文字的长度floatmTxtWidth=txtPaint.measureText(txt,0,txt.length());Log.e("tag",textIsDisplayable+","+progress+(style==STROKE));if(textIsDisplayable&&progress!=0&&style==STROKE){canvas.drawText(txt,mXCenter-mTxtWidth/2,mYCenter+textSize/2,txtPaint);}}publicsynchronizedintgetMax(){returnmax;}/***设置进度的最大值*@parammax*/publicsynchronizedvoidsetMax(intmax){if(max<0){thrownewIllegalArgumentException("maxnotlessthan0");}this.max=max;}/***获取进度.需要同步*@return*/publicsynchronizedintgetProgress(){returnprogress;}/***设置进度,此为线程安全控件,由于考虑多线程的问题,需要同步*刷新界面调用postInvalidate()能在非UI线程刷新*@paramprogress*/publicsynchronizedvoidsetProgress(intprogress){if(progress<0){thrownewIllegalArgumentException("progressnotlessthan0");}if(progress>max){progress=max;}if(progress<=max){this.progress=progress;postInvalidate();}}publicintgetRingColor(){returnringColor;}publicvoidsetRingColor(intringColor){this.ringColor=ringColor;}publicintgetRingProgressColor(){returnringProgressColor;}publicvoidsetRingProgressColor(intringProgressColor){this.ringProgressColor=ringProgressColor;}publicintgetTextColor(){returntextColor;}publicvoidsetTextColor(inttextColor){this.textColor=textColor;}publicfloatgetTextSize(){returntextSize;}publicvoidsetTextSize(floattextSize){this.textSize=textSize;}publicfloatgetRingWidth(){returnringWidth;}publicvoidsetRingWidth(floatroundWidth){this.ringWidth=roundWidth;}}
MainActivity.java
packagecom.czhappy.ringprogressdemo.activity;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.view.View;importcom.czhappy.ringprogressdemo.R;importcom.czhappy.ringprogressdemo.view.RingProgressBar;publicclassMainActivityextendsAppCompatActivity{privateRingProgressBarmyProgress;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myProgress=(RingProgressBar)findViewById(R.id.myProgress);}publicvoidbeginAnim(Viewview){newThread(){@Overridepublicvoidrun(){super.run();for(inti=0;i<=60;i++){try{Thread.sleep(20);}catch(InterruptedExceptione){e.printStackTrace();}myProgress.setProgress(i);}}}.start();}}
布局文件
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:ring="http://schemas.android.com/apk/res-auto"android:id="@+id/activity_main"android:gravity="center_horizontal"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:onClick="beginAnim"android:layout_height="wrap_content"android:text="开始动画"/><com.czhappy.ringprogressdemo.view.RingProgressBarandroid:id="@+id/myProgress"android:layout_width="100dp"android:layout_height="100dp"android:layout_marginTop="20dp"ring:ringColor="@color/ring_color"ring:ringProgressColor="@color/ring_progress_color"ring:textColor="@color/ring_progress_color"ring:textIsDisplayable="true"ring:ringWidth="6dp"ring:textSize="16sp"/></LinearLayout>
感谢各位的阅读,以上就是“Android中怎么自定义view实现圆环进度条效果”的内容了,经过本文的学习后,相信大家对Android中怎么自定义view实现圆环进度条效果这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。