Android自定义View绘制圆形、方形、弧形、球形四种形态的模仿下载进度条

本文介绍了如何在Android中创建自定义View,通过继承View并使用Path和Canvas来绘制四种不同形态的进度条:弧形、方形、圆形和球形。实现过程中,首先创建Java类并重写构造器,设置画布尺寸,接着分别绘制四种形状的图形,最后在Activity中利用Handler更新下载进度,达到模仿下载进度条的效果。
摘要由CSDN通过智能技术生成

绘制步骤

1.首先创建以JAVA类继承与View,然后调用前两个构造器

2.然后设置画布的宽度和高度

3.绘制图形(见代码)

4.在XML文件布局中添加该View并附上id

5.为了模仿下载进度条,需要在Activity中找到该View,利用Handler提交数据即提交下载完成度,并不断更新View(可以设置延迟来模仿)

6.代码实现

弧形进度条

自定义View

public class ProgressArcView extends View {
   
    private int width;
    private int heigth;
    private int progressCurrent;
    private int progressMax=100;
    private Paint mPaintMax;
    private Paint mPaintCurrent;
    private Paint mPaintText;
    public void setProgressCurrent(int progressCurrent) {
        this.progressCurrent = progressCurrent;
        invalidate();
    }
    public ProgressArcView(Context context) {
        super(context);
    }

    public ProgressArcView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaintMax=new Paint();
        mPaintMax.setAntiAlias(true);
        mPaintMax.setColor(Color.GRAY);
        mPaintMax.setStyle(Paint.Style.STROKE);
        mPaintMax.setStrokeWidth(100);

        mPaintCurrent=new Paint();
        mPaintCurrent.setAntiAlias(true);
        mPaintCurrent.setColor(Color.BLUE);
        mPaintCurrent.setStyle(Paint.Style.STROKE);
        mPaintCurrent.setStrokeWidth(100);

        mPaintText=new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setColor(Color.BLACK);
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mPaintText.setTextSize(80);
    }
    //设置画布的宽度和高度
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        heigth=getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width,heigth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(width/2,heigth/2,300</
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值