这期内容当中小编将会给大家带来有关Android中怎么实现一个炫酷进度条效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
创新互联建站是一家集网站建设,汉南企业网站建设,汉南品牌网站建设,网站定制,汉南网站建设报价,网络营销,网络优化,汉南网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
HorizontalProgressbarWithProgress的代码
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Build; import android.support.annotation.RequiresApi; import android.util.AttributeSet; import android.util.TypedValue; import android.widget.ProgressBar; import trunk.doi.base.R; public class HorizontalProgressbarWithProgress extends ProgressBar{ private static final int DEFAULT_TEXT_SIZE=10;//sp private static final int DEFAULT_TEXT_COLOR=0xFFFC00D1; private static final int DEFAULT_COLOR_UNREACH=0xFFD3D6DA; private static final int DEFAULT_HEIGHT_UNREACH=2;//dp private static final int DEFAULT_COLOR_REACH=DEFAULT_TEXT_COLOR; private static final int DEFAULT_HEIGHT_REACH=2; private static final int DEFAULT_TEXT_OFFSET=10; protected int mTextSize=sp2px(DEFAULT_TEXT_SIZE); protected int mTextColor=DEFAULT_TEXT_COLOR; protected int mUnReachColor=DEFAULT_COLOR_UNREACH; protected int mUnReachHeigh=dp2px(DEFAULT_HEIGHT_UNREACH); protected int mReachHeigh=dp2px(DEFAULT_HEIGHT_REACH); protected int mReachColor=DEFAULT_COLOR_REACH; protected int mTextOffset=dp2px(DEFAULT_TEXT_OFFSET); protected Paint mPaint=new Paint(); protected int mRealWidth; public HorizontalProgressbarWithProgress(Context context) { super(context); init(null); } public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(attrs); } private void init(AttributeSet attrs) { /** * 获取dimension的方法有几种,区别不大 * 共同点是都会将dp,sp的单位转为px,px单位的保持不变 * * getDimension() 返回float, * getDimensionPixelSize 返回int 小数部分四舍五入 * getDimensionPixelOffset 返回int,但是会抹去小数部分 */ TypedArray array=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 protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // int widthMode=MeasureSpec.getMode(widthMeasureSpec); int width=MeasureSpec.getSize(widthMeasureSpec); int heigh=measureHeight(heightMeasureSpec); setMeasuredDimension(width,heigh); mRealWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight(); } private int measureHeight(int heightMeasureSpec) { int result=0; int mode=MeasureSpec.getMode(heightMeasureSpec); int size=MeasureSpec.getSize(heightMeasureSpec); if(mode==MeasureSpec.EXACTLY){ result=size; }else{ int textHeigh= (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); } } return result; } @Override protected synchronized void onDraw(Canvas canvas) { canvas.save(); canvas.translate(getPaddingLeft(),getHeight()/2); boolean noNeedUnReach=false; String text=getProgress()+"%"; int textWidth= (int) mPaint.measureText(text); float radio =getProgress()*1.0f/getMax(); float progressX=radio*mRealWidth; if(progressX+textWidth>mRealWidth){ progressX=mRealWidth-textWidth; noNeedUnReach=true; } float endX=progressX-mTextOffset/2; if(endX>0){ mPaint.setColor(mReachColor); mPaint.setStrokeWidth(mReachHeigh); canvas.drawLine(0,0,endX,0,mPaint); } //draw text mPaint.setColor(mTextColor); int y = (int) (-(mPaint.descent()+mPaint.ascent())/2); canvas.drawText(text,progressX,y,mPaint); //draw unreach bar if(!noNeedUnReach){ float startX=progressX+ mTextOffset/2+textWidth; mPaint.setColor(mUnReachColor); mPaint.setStrokeWidth(mUnReachHeigh); canvas.drawLine(startX,0,mRealWidth,0,mPaint); } canvas.restore(); } protected int dp2px(int dpVal){ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpVal,getResources().getDisplayMetrics()); } protected int sp2px(int spVal){ return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spVal,getResources().getDisplayMetrics()); } }
RoundProgressBarWithProgress的代码
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.os.Build; import android.support.annotation.RequiresApi; import android.util.AttributeSet; import trunk.doi.base.R; /** * 作者:Mr.Lee on 2017-10-18 10:48 * 邮箱:569932357@qq.com */ public class RoundProgressBarWithProgress extends HorizontalProgressbarWithProgress { private int mRadius=dp2px(30); private int mMaxPaintWidth; public RoundProgressBarWithProgress(Context context) { super(context); init(null); } public RoundProgressBarWithProgress(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public RoundProgressBarWithProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public RoundProgressBarWithProgress(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(attrs); } private void init(AttributeSet attrs){ mReachHeigh= (int) (mUnReachHeigh*2.5f); TypedArray array=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 protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mMaxPaintWidth=Math.max(mReachHeigh,mUnReachHeigh); //默认4个padding一致 int except=mRadius*2+mMaxPaintWidth+getPaddingLeft()+getPaddingRight(); int width=resolveSize(except,widthMeasureSpec); int height=resolveSize(except,heightMeasureSpec); int realWidth=Math.min(width,height); mRadius=(realWidth-getPaddingLeft()-getPaddingRight()-mMaxPaintWidth)/2; setMeasuredDimension(realWidth,realWidth); } @Override protected synchronized void onDraw(Canvas canvas) { String text=getProgress()+"%"; float textWidth=mPaint.measureText(text); float textHeight=(mPaint.ascent()+mPaint.descent())/2; canvas.save(); canvas.translate(getPaddingLeft(),getPaddingTop()); mPaint.setStyle(Paint.Style.STROKE); // draw unreachbar mPaint.setColor(mUnReachColor); mPaint.setStrokeWidth(mUnReachHeigh); canvas.drawCircle(mRadius,mRadius,mRadius,mPaint); //draw reach bar mPaint.setColor(mReachColor); mPaint.setStrokeWidth(mReachHeigh); float sweepAngle=getProgress()*1.0f/getMax()*360; canvas.drawArc(new RectF(0,0,mRadius*2,mRadius*2),0,sweepAngle,false,mPaint); //draw text mPaint.setColor(mTextColor); mPaint.setStyle(Paint.Style.FILL); canvas.drawText(text,mRadius-textWidth/2,mRadius-textHeight,mPaint); canvas.restore(); } }
activity_view_mv代码
ViewMvActivity代码
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.widget.AppCompatSeekBar; import android.view.MotionEvent; import android.view.View; import android.widget.RelativeLayout; import android.widget.SeekBar; import butterknife.BindView; import trunk.doi.base.R; import trunk.doi.base.base.BaseActivity; public class ViewMvActivity extends BaseActivity { //没有集成Butterknife的findviewbyid() @BindView(R.id.progress_bar) HorizontalProgressbarWithProgress progress_bar; @BindView(R.id.progress_bar2) RoundProgressBarWithProgress progress_bar2; @BindView(R.id.seekbar) AppCompatSeekBar seekbar; private float mTouchStartY; private static final float TOUCH_MOVE_MAX_Y=600; @Override protected int initLayoutId() { return R.layout.activity_view_mv; } @Override protected void initView(@Nullable Bundle savedInstanceState) { } @Override protected void setListener() { seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { progress_bar.setProgress(progress); progress_bar2.setProgress(progress); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } @Override protected void initData() { } }
上述就是小编为大家分享的Android中怎么实现一个炫酷进度条效果了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。