Android打造炫酷进度条效果
本文实例为大家分享了Android炫酷进度条效果的具体代码,供大家参考,具体内容如下
学习:视频地址
HorizontalProgressbarWithProgress的代码
importandroid.content.Context; importandroid.content.res.TypedArray; importandroid.graphics.Canvas; importandroid.graphics.Paint; importandroid.os.Build; importandroid.support.annotation.RequiresApi; importandroid.util.AttributeSet; importandroid.util.TypedValue; importandroid.widget.ProgressBar; importtrunk.doi.base.R; /** *作者:Mr.Leeon2017-10-1715:51 *邮箱:569932357@qq.com */ publicclassHorizontalProgressbarWithProgressextendsProgressBar{ privatestaticfinalintDEFAULT_TEXT_SIZE=10;//sp privatestaticfinalintDEFAULT_TEXT_COLOR=0xFFFC00D1; privatestaticfinalintDEFAULT_COLOR_UNREACH=0xFFD3D6DA; privatestaticfinalintDEFAULT_HEIGHT_UNREACH=2;//dp privatestaticfinalintDEFAULT_COLOR_REACH=DEFAULT_TEXT_COLOR; privatestaticfinalintDEFAULT_HEIGHT_REACH=2; privatestaticfinalintDEFAULT_TEXT_OFFSET=10; protectedintmTextSize=sp2px(DEFAULT_TEXT_SIZE); protectedintmTextColor=DEFAULT_TEXT_COLOR; protectedintmUnReachColor=DEFAULT_COLOR_UNREACH; protectedintmUnReachHeigh=dp2px(DEFAULT_HEIGHT_UNREACH); protectedintmReachHeigh=dp2px(DEFAULT_HEIGHT_REACH); protectedintmReachColor=DEFAULT_COLOR_REACH; protectedintmTextOffset=dp2px(DEFAULT_TEXT_OFFSET); protectedPaintmPaint=newPaint(); protectedintmRealWidth; publicHorizontalProgressbarWithProgress(Contextcontext){ super(context); init(null); } publicHorizontalProgressbarWithProgress(Contextcontext,AttributeSetattrs){ super(context,attrs); init(attrs); } publicHorizontalProgressbarWithProgress(Contextcontext,AttributeSetattrs,intdefStyleAttr){ super(context,attrs,defStyleAttr); init(attrs); } @RequiresApi(api=Build.VERSION_CODES.LOLLIPOP) publicHorizontalProgressbarWithProgress(Contextcontext,AttributeSetattrs,intdefStyleAttr,intdefStyleRes){ super(context,attrs,defStyleAttr,defStyleRes); init(attrs); } privatevoidinit(AttributeSetattrs){ /** *获取dimension的方法有几种,区别不大 *共同点是都会将dp,sp的单位转为px,px单位的保持不变 * *getDimension()返回float, *getDimensionPixelSize返回int小数部分四舍五入 *getDimensionPixelOffset返回int,但是会抹去小数部分 */ TypedArrayarray=getContext().obtainStyledAttributes(attrs,R.styleable.HorizontalProgressbarWithProgress); mTextSize=(int)array.getDimension(R.styleable.HorizontalProgressbarWithProgress_progress_text_size,mTextSize); mTextColor=array.getColor(R.styleable.HorizontalProgressbarWithProgress_progress_text_color,mTextColor); mUnReachColor=array.getColor(R.styleable.HorizontalProgressbarWithProgress_progress_unreach_color,mUnReachColor); mUnReachHeigh=(int)array.getDimension(R.styleable.HorizontalProgressbarWithProgress_progress_unreach_height,mUnReachHeigh); mReachHeigh=(int)array.getDimension(R.styleable.HorizontalProgressbarWithProgress_progress_reach_height,mReachHeigh); mTextOffset=(int)array.getDimension(R.styleable.HorizontalProgressbarWithProgress_progress_text_offset,mTextOffset); mReachColor=array.getColor(R.styleable.HorizontalProgressbarWithProgress_progress_reach_color,mReachColor); array.recycle(); mPaint.setTextSize(mTextSize); } @Override protectedsynchronizedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){ //intwidthMode=MeasureSpec.getMode(widthMeasureSpec); intwidth=MeasureSpec.getSize(widthMeasureSpec); intheigh=measureHeight(heightMeasureSpec); setMeasuredDimension(width,heigh); mRealWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight(); } privateintmeasureHeight(intheightMeasureSpec){ intresult=0; intmode=MeasureSpec.getMode(heightMeasureSpec); intsize=MeasureSpec.getSize(heightMeasureSpec); if(mode==MeasureSpec.EXACTLY){ result=size; }else{ inttextHeigh=(int)(mPaint.descent()-mPaint.ascent()); result=getPaddingTop()+getPaddingBottom()+Math.max(Math.max(mReachHeigh,mUnReachHeigh),Math.abs(textHeigh)); if(mode==MeasureSpec.AT_MOST){ result=Math.min(result,size); } } returnresult; } @Override protectedsynchronizedvoidonDraw(Canvascanvas){ canvas.save(); canvas.translate(getPaddingLeft(),getHeight()/2); booleannoNeedUnReach=false; Stringtext=getProgress()+"%"; inttextWidth=(int)mPaint.measureText(text); floatradio=getProgress()*1.0f/getMax(); floatprogressX=radio*mRealWidth; if(progressX+textWidth>mRealWidth){ progressX=mRealWidth-textWidth; noNeedUnReach=true; } floatendX=progressX-mTextOffset/2; if(endX>0){ mPaint.setColor(mReachColor); mPaint.setStrokeWidth(mReachHeigh); canvas.drawLine(0,0,endX,0,mPaint); } //drawtext mPaint.setColor(mTextColor); inty=(int)(-(mPaint.descent()+mPaint.ascent())/2); canvas.drawText(text,progressX,y,mPaint); //drawunreachbar if(!noNeedUnReach){ floatstartX=progressX+mTextOffset/2+textWidth; mPaint.setColor(mUnReachColor); mPaint.setStrokeWidth(mUnReachHeigh); canvas.drawLine(startX,0,mRealWidth,0,mPaint); } canvas.restore(); } protectedintdp2px(intdpVal){ return(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal,getResources().getDisplayMetrics()); } protectedintsp2px(intspVal){ return(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getResources().getDisplayMetrics()); } }
RoundProgressBarWithProgress的代码
importandroid.content.Context; importandroid.content.res.TypedArray; importandroid.graphics.Canvas; importandroid.graphics.Paint; importandroid.graphics.RectF; importandroid.os.Build; importandroid.support.annotation.RequiresApi; importandroid.util.AttributeSet; importtrunk.doi.base.R; /** *作者:Mr.Leeon2017-10-1810:48 *邮箱:569932357@qq.com */ publicclassRoundProgressBarWithProgressextendsHorizontalProgressbarWithProgress{ privateintmRadius=dp2px(30); privateintmMaxPaintWidth; publicRoundProgressBarWithProgress(Contextcontext){ super(context); init(null); } publicRoundProgressBarWithProgress(Contextcontext,AttributeSetattrs){ super(context,attrs); init(attrs); } publicRoundProgressBarWithProgress(Contextcontext,AttributeSetattrs,intdefStyleAttr){ super(context,attrs,defStyleAttr); init(attrs); } @RequiresApi(api=Build.VERSION_CODES.LOLLIPOP) publicRoundProgressBarWithProgress(Contextcontext,AttributeSetattrs,intdefStyleAttr,intdefStyleRes){ super(context,attrs,defStyleAttr,defStyleRes); init(attrs); } privatevoidinit(AttributeSetattrs){ mReachHeigh=(int)(mUnReachHeigh*2.5f); TypedArrayarray=getContext().obtainStyledAttributes(attrs,R.styleable.RoundProgressBarWithProgress); mRadius=(int)array.getDimension(R.styleable.RoundProgressBarWithProgress_radius,mRadius); array.recycle(); mPaint.setStyle(Paint.Style.STROKE); mPaint.setAntiAlias(true); mPaint.setDither(true); mPaint.setStrokeCap(Paint.Cap.ROUND); } @Override protectedsynchronizedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){ mMaxPaintWidth=Math.max(mReachHeigh,mUnReachHeigh); //默认4个padding一致 intexcept=mRadius*2+mMaxPaintWidth+getPaddingLeft()+getPaddingRight(); intwidth=resolveSize(except,widthMeasureSpec); intheight=resolveSize(except,heightMeasureSpec); intrealWidth=Math.min(width,height); mRadius=(realWidth-getPaddingLeft()-getPaddingRight()-mMaxPaintWidth)/2; setMeasuredDimension(realWidth,realWidth); } @Override protectedsynchronizedvoidonDraw(Canvascanvas){ Stringtext=getProgress()+"%"; floattextWidth=mPaint.measureText(text); floattextHeight=(mPaint.ascent()+mPaint.descent())/2; canvas.save(); canvas.translate(getPaddingLeft(),getPaddingTop()); mPaint.setStyle(Paint.Style.STROKE); //drawunreachbar mPaint.setColor(mUnReachColor); mPaint.setStrokeWidth(mUnReachHeigh); canvas.drawCircle(mRadius,mRadius,mRadius,mPaint); //drawreachbar mPaint.setColor(mReachColor); mPaint.setStrokeWidth(mReachHeigh); floatsweepAngle=getProgress()*1.0f/getMax()*360; canvas.drawArc(newRectF(0,0,mRadius*2,mRadius*2),0,sweepAngle,false,mPaint); //drawtext mPaint.setColor(mTextColor); mPaint.setStyle(Paint.Style.FILL); canvas.drawText(text,mRadius-textWidth/2,mRadius-textHeight,mPaint); canvas.restore(); } }
activity_view_mv代码
ViewMvActivity代码
importandroid.os.Bundle; importandroid.support.annotation.Nullable; importandroid.support.v7.widget.AppCompatSeekBar; importandroid.view.MotionEvent; importandroid.view.View; importandroid.widget.RelativeLayout; importandroid.widget.SeekBar; importbutterknife.BindView; importtrunk.doi.base.R; importtrunk.doi.base.base.BaseActivity; publicclassViewMvActivityextendsBaseActivity{ //没有集成Butterknife的findviewbyid() @BindView(R.id.progress_bar) HorizontalProgressbarWithProgressprogress_bar; @BindView(R.id.progress_bar2) RoundProgressBarWithProgressprogress_bar2; @BindView(R.id.seekbar) AppCompatSeekBarseekbar; privatefloatmTouchStartY; privatestaticfinalfloatTOUCH_MOVE_MAX_Y=600; @Override protectedintinitLayoutId(){ returnR.layout.activity_view_mv; } @Override protectedvoidinitView(@NullableBundlesavedInstanceState){ } @Override protectedvoidsetListener(){ seekbar.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener(){ @Override publicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){ progress_bar.setProgress(progress); progress_bar2.setProgress(progress); } @Override publicvoidonStartTrackingTouch(SeekBarseekBar){ } @Override publicvoidonStopTrackingTouch(SeekBarseekBar){ } }); } @Override protectedvoidinitData(){ } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。