自定义圆形进度条

在此写一个自定义的圆形进度条来进一步巩固自定义控件的基本用法

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>
步骤二、获取自定义属性,并初始化画笔等控件

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();//初始化画笔}
步骤三、在ondraw方法中进行绘制

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"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Android上自定义圆形进度条,可以按照以下步骤进行: 1. 首先,在XML布局文件中添加一个半圆形的View。可以使用自定义的View继承自View类,或者使用现有的绘图类如Canvas和Path来绘制半圆形。 2. 在自定义的View中,重写onDraw方法,通过Canvas对象绘制半圆形。可以使用drawArc方法来画出一个弧线,指定起点、终点和角度,来绘制半圆形。 3. 在自定义的View中添加一个属性来表示当前进度。可以使用属性动画或者定时器来不断更新进度,并调用invalidate方法来重绘View。 4. 在自定义的View中添加一些自定义属性,如颜色、宽度等,来实现进度条的样式定制。 5. 在Activity中使用这个自定义的View,可以通过findViewById找到它,并设置进度条的属性和监听器等。 通过以上步骤,就可以实现一个自定义的半圆形进度条。根据具体需求和设计,可以进行更多的样式和功能定制,如添加文本显示、动画效果等。 ### 回答2: 要实现Android自定义圆形进度条,可以采用以下步骤: 1. 首先,在布局文件中创建一个自定义View,用于绘制半圆形进度条。可以使用Canvas来进行绘制。在View的构造函数中初始化一些必要的参数,如进度条的颜色、背景颜色等。 2. 在自定义View的onDraw()方法中,使用Canvas绘制一个半圆形的背景,可以使用drawArc()方法,并设置起始角度和扫描角度。 3. 接着,绘制进度条的一段弧线。根据进度的值计算出起始和结束的角度,并使用drawArc()方法进行绘制。 4. 在Activity中,实例化自定义View,并将其添加到布局中。可以使用setProgress()方法来设置进度条的进度值。 5. 如果需要实现动画效果,可以使用ValueAnimator来改变进度值,并在动画过程中不断调用invalidate()方法来刷新视图。 总结:自定义圆形进度条的关键是使用Canvas的drawArc()方法进行绘制,根据进度值来计算起始和结束的角度,并使用invalidate()方法来进行视图的刷新。通过这些步骤,即可实现Android自定义圆形进度条的效果。 ### 回答3: 要实现Android自定义圆形进度条,可以通过自定义View来实现。 首先,创建一个继承自View的自定义View类,并重写其中的三个方法:onMeasure()、onDraw()和onSizeChanged()。 在onMeasure()方法中,设置View的宽度和高度,可以根据需求来设置,比如设置为200dp,然后将测量的结果保存。 在onSizeChanged()方法中,获取View的宽度和高度,用于后续计算绘制进度条的位置。 在onDraw()方法中,绘制半圆形进度条的背景和进度。首先,使用Paint类创建两个画笔,一个用于绘制背景,一个用于绘制进度。然后,通过Path类创建一个半圆形的路径,使用drawPath()方法绘制出半圆形。接着,根据进度计算出进度条的结束位置,使用drawArc()方法绘制出进度条。最后,使用drawText()方法绘制出进度的文字。 在Activity或Fragment中使用自定义View,可以通过布局文件或代码的方式进行添加。如果使用布局文件,可以在XML文件中使用自定义的命名空间,并设置View的属性。如果使用代码,可以在onCreate()方法中使用addView()方法添加。 以上就是实现Android自定义圆形进度条的大致步骤。根据具体需求,还可以添加其他功能,比如添加动画效果,改变进度条的颜色等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值