Android_(自定义view_4:圆形加载框)


上图演示


package com.skytea.circleview;

import javax.crypto.spec.OAEPParameterSpec;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * @Title:  CircleProgressBarView.java 
 * @Copyright:  Copyright 2015年12月14日 下午4:29:55 ,  All rights reserved 
 * @Description:  TODO 
 * @author:  SkyTea 
 * @data:  2015年12月14日 下午4:29:55 
 * @version:  V1.0 
 */
public class CircleProgressBarView  extends View{


    private int progress=20;
    private int max=100;
    private Paint paint;
    private RectF oval;



    public CircleProgressBarView(Context context, AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
        paint=new Paint();
        oval=new RectF();
        new Thread(new Runnable() {

            @Override
            public void run() {
                while(true){
                     progress++;
                     postInvalidate();
                     if (progress==100) {
                        break;
                    }
                     try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    public CircleProgressBarView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
        // TODO Auto-generated constructor stub
    }

    public CircleProgressBarView(Context context) {
        this(context,null);
        // TODO Auto-generated constructor stub
    }



    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        paint.setAntiAlias(true);// 设置是否抗锯齿
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);// 帮助消除锯齿
        paint.setColor(Color.GRAY);// 设置画笔灰色
        paint.setStrokeWidth(10);// 设置画笔宽度
        paint.setStyle(Paint.Style.STROKE);// 设置中空的样式

        canvas.drawCircle(getWidth()/2, getHeight()/2, 55, paint);
        paint.setColor(Color.GREEN);// 设置画笔为绿色

        oval.set(getWidth()/2-55,  getHeight()/2-55, getWidth()/2+55, getHeight()/2+55);
        //  canvas.drawRect(oval, paint);
        canvas.drawArc(oval, -90, ((float) progress / max) * 360, false, paint);
        paint.reset();// 将画笔重置
        paint.setStrokeWidth(3);// 再次设置画笔的宽度
        paint.setTextSize(35);// 设置文字的大小
        paint.setColor(Color.BLACK);// 设置画笔颜色
        if (progress == max) {
            canvas.drawText("完成", getWidth()/2-25, getHeight()/2, paint);
        } else {
            canvas.drawText(progress + "%", getWidth()/2-15, getHeight()/2+10, paint);
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值