简单进度条的实现
来源:互联网 发布:appserv是什么软件 编辑:程序博客网 时间:2024/06/06 07:03
- 测量高度 宽度 获取配置文件中设定的值
- 设置前景色
- 设置背景色 根据percent 百分比参数
- 设置文本描述
- 设置动画效果
工程地址 https://github.com/gacmy/GacProgressBar
public class GacProgressBar extends View { private int maxWidth; private int maxHeight; private Paint textPaint; private int mBackgroudColor; private int mForegroundColor; private int textColor; private int textSize; private float mPercent; private String mDescriptionText; ValueAnimator animator; private int currentWidth; public GacProgressBar(Context context) { super(context); init(); } public GacProgressBar(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public GacProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init(){ if(mBackgroudColor == 0){ mBackgroudColor = Color.parseColor("#eeeeee"); } if(mForegroundColor == 0){ mForegroundColor = Color.parseColor("#feaaaa"); } textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textColor = Color.parseColor("#000000"); mDescriptionText = "剩余20%"; textSize = 40; mPercent = 0.2f; } private void setBackgroundPaint(){ textPaint.setColor(mBackgroudColor); textPaint.setStyle(Paint.Style.FILL); } private void setForegroundPaint(){ textPaint.setColor(mForegroundColor); textPaint.setStyle(Paint.Style.FILL); } /* 设置动画效果 */ private void setAnimation(){ animator = ValueAnimator.ofFloat(0,1); //animator.ofFloat(0,1); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float)animation.getAnimatedValue(); // float width = (int)maxWidth*mPercent*value; currentWidth =(int)(maxWidth* value*mPercent); // Log.e("gac","value"+mPercent); invalidate(); } }); animator.setDuration(1000); animator.start(); } private void setTextPaint(){ textPaint.setColor(textColor); textPaint.setTextSize(textSize); } public void setProgressBarConfig(ProgressBarConfig config){ mDescriptionText = config.getDescriptionText(); if(config.getForegroundColor() == 0){ mForegroundColor = Color.parseColor("#feaaaa"); }else{ mForegroundColor = convertColor(config.getForegroundColor()); } if(config.getBackgroudColor() == 0){ mBackgroudColor = Color.parseColor("#eeeeee"); }else{ mBackgroudColor = convertColor(config.getBackgroudColor()); } mPercent = config.getPercent(); if(config.getTextColor() == 0){ textColor = Color.parseColor("000000"); }else{ textColor = convertColor(config.getTextColor()); } if(config.getTextSize() == 0){ textSize = convertsize(16); }else{ textSize = convertsize(config.getTextSize()); } //invalidate(); if(config.isAnimation()){ setAnimation(); }else{ invalidate(); Log.e("maxWidth","maxWidth:"+maxWidth+" curWidth:"+currentWidth+" percent:"+mPercent); } } //资源id convert color rgb private int convertColor(int color){ return ContextCompat.getColor(getContext(), color); } //dp to px private int convertsize(int size){ return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics()); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); maxWidth = measureWidth(widthMeasureSpec); maxHeight = measureHeight(heightMeasureSpec); setMeasuredDimension(maxWidth, maxHeight); currentWidth = (int)(maxWidth *mPercent); } private int measureWidth(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); //设置一个默认值,就是这个View的默认宽度为500,这个看我们自定义View的要求 int result = 500; if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content result = specSize; } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值 result = specSize; } return result; } private int measureHeight(int measureSpec) { int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); int result = 500; if (specMode == MeasureSpec.AT_MOST) { result = specSize; } else if (specMode == MeasureSpec.EXACTLY) { result = specSize; } return result; } @Override protected void onDraw(Canvas canvas) { drawBackground(canvas); drawForeground(canvas); drawText(canvas); } private void drawBackground(Canvas canvas){ setBackgroundPaint(); canvas.drawRect(0,0,maxWidth,maxHeight,textPaint); } private void drawForeground(Canvas canvas){ setForegroundPaint(); canvas.drawRect(0,0,currentWidth,maxHeight,textPaint); } private void drawText(Canvas canvas){ if(TextUtils.isEmpty(mDescriptionText)){ return; } setTextPaint(); canvas.drawText(mDescriptionText,40,maxHeight,textPaint); } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); if(animator != null){ animator.cancel(); } }}
阅读全文
0 0
- 进度条的简单实现
- 进度条的简单实现
- 简单进度条的实现
- 简单进度条的实现
- LINUX实现简单的进度条
- 环形进度条的简单实现
- Linux实现简单的进度条
- 实现一个简单的进度条
- shell实现简单的进度条
- 环形进度条的简单实现
- 一个简单的进度条,applet实现
- 用Ajax实现一个简单的进度条
- 用Ajax实现一个简单的进度条
- jsp + servlet简单实现进度条的滚动
- jquery 简单的进度条实现代码
- 用CSS实现简单的进度条
- C语言简单的进度条实现
- 简单实现一个文件上传的进度条
- activiti之流程变量值Business_key
- 在jquery.ajax()的回调函数中使用发送请求时的data中的参数
- POJ 2299 Ultra-QuickSort【归并排序】
- 消除ScrollView 惯性
- parted分区工具使用
- 简单进度条的实现
- static和extern简单使用(要使用一个东西,先了解其作用)
- 文件aidl的使用方法
- something useful about cmd
- 使用大于 16MB 的闪存时, Zynq 和 QSPI 的复位要求
- 关于java中输出流flush()方法
- Jzoj4699 Password
- 专有云,激发政企大脑的潜能
- DPDK-实战之helloworld