Android 自定义进度条
2017-08-15 19:01
253 查看
一、废话少说先上图
二、思路
主要是使用自定义view和自定义属性去实现刷新
三、代码
attr.xml
CustomProgressBar.java
然后获取自定义控件的对象,设置点击事件调用startDraw()就可以
二、思路
主要是使用自定义view和自定义属性去实现刷新
三、代码
attr.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomProgressBar"> <attr name="roundProgressColor" format="color" /> <attr name="roundColor" format="color" /> <attr name="roundWidth" format="dimension" /> <attr name="textSize" format="dimension" /> <attr name="textColor" format="color" /> <attr name="max" format="integer" /> <attr name="textShow" format="boolean" /> <attr name="sleepTime" format="integer" /> <attr name="style"> <enum name="STROKE" value="0" /> <enum name="FILL" value="1" /> </attr> </declare-styleable> </resources>
CustomProgressBar.java
public class CustomProgressBar extends View { private int roundProgressColor; private int roundColor; private float roundWidth; private float textSize; private int textColor; private int max; private boolean textShow; private int style; private Paint mPaint; private final int STOKE = 0; private final int FILL = 1; private int progress; private int sleepTime;//睡眠时间 public CustomProgressBar(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mPaint = new Paint(); //获取自定义属性数组 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressBar); roundProgressColor = typedArray.getColor(R.styleable.CustomProgressBar_roundProgressColor, Color.RED); roundColor = typedArray.getColor(R.styleable.CustomProgressBar_roundColor, Color.BLUE); roundWidth = typedArray.getDimension(R.styleable.CustomProgressBar_roundWidth, 50); textSize = typedArray.getDimension(R.styleable.CustomProgressBar_textSize, 25); textColor = typedArray.getColor(R.styleable.CustomProgressBar_textColor, Color.GREEN); max = typedArray.getInteger(R.styleable.CustomProgressBar_max, 100); textShow = typedArray.getBoolean(R.styleable.CustomProgressBar_textShow, false); style = typedArray.getInteger(R.styleable.CustomProgressBar_style, 0); sleepTime = typedArray.getInteger(R.styleable.CustomProgressBar_sleepTime, 100); //释放TypedArray实例,从而使其可被其他模块复用 typedArray.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //画默认大圆环 int center = getWidth() / 2;//中心坐标点 float radius = center - roundWidth / 2;//半径 mPaint.setColor(roundColor); mPaint.setStyle(Paint.Style.STROKE);//设置空心(描边) //设置圆环宽度 mPaint.setStrokeWidth(roundWidth); //设置抗锯齿 mPaint.setAntiAlias(true); //绘制圆 canvas.drawCircle(center, center, radius, mPaint); //绘制进度百分比 mPaint.setColor(textColor); //圆环的宽度 mPaint.setStrokeWidth(0); mPaint.setTextSize(textSize); mPaint.setTypeface(Typeface.DEFAULT_BOLD); int percent = (int) (progress / (float) max * 100); if (textShow && percent != 0 && style == STOKE) { //descent下基准线位置,ascent上基准线位置 canvas.drawText(percent + "%", (getWidth() - mPaint.measureText(percent + "%")) / 2f, getWidth() / 2f - (mPaint.descent() + mPaint.ascent()) / 2f, mPaint); } //画圆弧 //矩形区域,定义圆弧的形状大小 RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius); mPaint.setColor(roundProgressColor); mPaint.setStrokeWidth(roundWidth); switch (style) { case STOKE: mPaint.setStyle(Paint.Style.STROKE); /** * RectF 圆弧所在的椭圆对象, * startAngle圆弧的起始角度 * sweepAngle圆弧的角度 * useCenter 是否显示半径连线,true表示显示圆弧与圆心的半径连线,false表示不显示。 * Paint 画笔 */ canvas.drawArc(oval, 0, 360 * progress / max, false, mPaint); break; case FILL: mPaint.setStyle(Paint.Style.FILL); if (progress != 0) canvas.drawArc(oval, 0, 360 * progress / max, true, mPaint); break; } } public synchronized int getMax() { return max; } public synchronized void setMax() { if (max < 0) { throw new IllegalArgumentException("max不能小于0"); } this.max = max; } public synchronized int getProgress() { return progress; } public synchronized void setProgress(int progress) { if (progress < 0) { throw new IllegalArgumentException("progress不能小于0"); } if (progress > max) { progress = max; } if (progress <= max) { this.progress = progress; postInvalidate(); } } //开始绘制 public void startDraw() { new Thread(new Runnable() { @Override public void run() { while (progress <= 100) { progress += 2; setProgress(progress); try { Thread.sleep(sleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } public int getRoundColor() { return roundColor; } public void setRoundColor(int roundColor) { this.roundColor = roundColor; } public int getRoundProgressColor() { return roundProgressColor; } public void setRoundProgressColor(int roundProgressColor) { this.roundProgressColor = roundProgressColor; } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(int textSize) { this.textSize = textSize; } public float getRoundWidth() { return roundWidth; } public void setRoundWidth(int roundWidth) { this.roundWidth = roundWidth; } public boolean isTextShow() { return textShow; } public void setTextShow(boolean textShow) { this.textShow = textShow; } }main.xml
<com.test.view.CustomProgressBar android:id="@+id/progressbar" android:layout_width="100dp" android:layout_height="100dp" app:roundProgressColor="#ff00ff" app:roundWidth="15dp" app:textColor="#ff0000" app:textShow="true" app:textSize="20dp" />
然后获取自定义控件的对象,设置点击事件调用startDraw()就可以
相关文章推荐
- android 圆形进度条 自定义view
- Android编程基于自定义View实现绚丽的圆形进度条功能示例
- Android中自定义Adapter实现ListView动态刷新进度条
- Android自定义进度条
- Android那些事儿之自定义进度条
- Android自定义进度条
- Android自定义半圆进度条 半圆渐变色进度条带指示 半圆开口大小可自由修改
- Android 自定义进度条
- Android自定义View-圆形加载进度条
- Android 自定义View 圆形百分比进度条
- Android自定义View-圆形进度条
- Android自定义进度条
- android 通知栏中自定义进度条长方形样式
- Android 可拖动进度条:SeekBar之自定义进度条
- Android 自定义ProgressBar--进度自己设置图片
- Android 自定义View修炼-仿360手机卫士波浪球进度的实现
- Android自定义view实现水波进度条控件
- Android自定义进度条
- Android进阶练习-自定义视图实战之刷新等待进度条
- android自定义的弧形进度条和圆形进度条-SemicircleProgress