在此写一个自定义的圆形进度条来进一步巩固自定义控件的基本用法
1、自定义属性
2、获取自定义属性,并初始化画笔等相关控件
3、通过onDraw方法进行绘制
步骤一、在values中创建attrs.xml编写自定义的属性
<declare-styleable name="RoundProgressBar"> <attr name="roundColor" format="color"/> <attr name="roundProgressColor" format="color"/> <attr name="roundWidth" format="dimension"></attr> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable>步骤二、获取自定义属性,并初始化画笔等控件
步骤三、在ondraw方法中进行绘制private Paint paint;//画笔 private int roundColor;//圆环的颜色 private int roundProgressColor;//进度条颜色 private int textColor;//进度显示颜色 private float textSize;//字体大小 private float roundWidth;//圆环宽度 private int max;//最大进度 private int progress = 50;//当前进度 private boolean textIsDisplayable;//是否显示中间进度值 private int style;//风格,空心还是实心
public RoundProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //加载自定义的属性 TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar); roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED); roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN); textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN); textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15); roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5); max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100); textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true); style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0); mTypedArray.recycle(); paint = new Paint();//初始化画笔}
protected void onDraw(Canvas canvas) { super.onDraw(canvas); //话最外侧大圆环 int centerX = getWidth()/2;//获取圆形x坐标 int centerY = getHeight()/2; int radius = (int) ((centerX - roundWidth)/2);//设置半径(圆心x坐标减去圆环宽度然后除以2) paint.setColor(roundColor);//设置圆环颜色 paint.setAntiAlias(true); //消除锯齿 paint.setStyle(Paint.Style.STROKE); //绘制空心圆或 空心矩形 paint.setStrokeWidth(roundWidth);//设置圆环宽度 canvas.drawCircle(centerX,centerY,radius,paint); //画圆环进度 paint.setColor(roundProgressColor); paint.setStrokeWidth(roundWidth); //设置圆环的宽度 paint.setColor(roundProgressColor); //设置进度的颜色 paint.setStyle(Paint.Style.STROKE); //绘制空心圆或 空心矩形 RectF rectF = new RectF(centerX - radius,centerY - radius,centerX+radius,centerY+radius);//圆环范围 canvas.drawArc(rectF,-90,360* progress / max,false,paint);//参数二表示开始起始位置,参数三表示进度 }圆环有了,同时需要在自定义的控件中添加响应的设置方法,(总进度,当前进度,进度颜色等)
public synchronized void setMax(int max){ this.max = max; } /** * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步 * 刷新界面调用postInvalidate()能在非UI线程刷新 * @param progress */ public synchronized void setProgress(int progress) { if(progress < 0){ throw new IllegalArgumentException("progress not less than 0"); } if(progress > max){ progress = max; } if(progress <= max){ this.progress = progress; postInvalidate(); } } public int getCricleColor() { return roundColor; } public void setCricleColor(int cricleColor) { this.roundColor = cricleColor; } public int getCricleProgressColor() { return roundProgressColor; } public void setCricleProgressColor(int cricleProgressColor) { this.roundProgressColor = cricleProgressColor; } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(float textSize) { this.textSize = textSize; } public float getRoundWidth() { return roundWidth; } public void setRoundWidth(float roundWidth) { this.roundWidth = roundWidth; }此时,在布局中引用即可
<com.example.administrator.myview.RoundProgressBar android:id="@+id/roundProgressBar2" android:layout_width="80dip" android:layout_height="80dip" android:layout_alignParentBottom="true" android:layout_marginBottom="78dp" android_custom:roundColor="#D1D1D1" android_custom:roundProgressColor="@android:color/black" android_custom:textColor="#9A32CD" android_custom:textIsDisplayable="false" android_custom:roundWidth="10dip" android_custom:textSize="18sp" />注意不要忘记添加自定义属性的命名空间
xmlns:android_custom="http://schemas.android.com/apk/res-auto"