自定义控件进度条加载

//布局文件

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="开始下载"
    android:onClick="start" />

<com.bw.u.jindutiaojiazai.MyView
    android:id="@+id/circleView"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginLeft="126dp"
    android:layout_marginStart="126dp"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="131dp" />
//MainAcivity代码

public class MainActivity extends AppCompatActivity {
    private MyView circleView;
    int progress = 0;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        circleView = (MyView) findViewById(R.id.circleView);
    }
    public void start(View v) {
        // 1000公里
        circleView.setMax(100);
        progress=0;
        new Thread() {
            public void run() {
                while (true) {
                    progress = progress + 1;
                    String text = progress + "%";
                    circleView.setProgressAndText(progress, text);
                    try {
                        sleep(30);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (progress == 100) {
                        break;
                    }
                }
            };
        }.start();
    }
}
//自定义控件的类

public class MyView extends View{
    int x;
    int y;
    int progress = 0;
    private String text="0%";
    private int max = 100;
    public MyView(Context context) {
        super(context);
    }
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 对于画笔
        Paint paint = new Paint();
        // 设置抗锯齿
        paint.setAntiAlias(true);
        // 设置画笔颜色
        // 三种样式--Stroke 只要描边 Fill 填充 FILL_AND_STROKE和既有描边又有填充
        paint.setStyle(Paint.Style.STROKE);
       // canvas.drawArc(oval, 0, 360 * progress / max, false, paint);
        //设置描边宽度
        paint.setStrokeWidth(2);
        //定义外圈员的颜色
        paint.setColor(Color.RED);
        //绘制圆形进度条--获取当前控件多大,正好让进度条在这个控件区间内
        canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, getMeasuredWidth()/2, paint);
        //重新设置描边宽度,这个宽度最好能完全盖过圆形
        paint.setStrokeWidth(3);
        //定义限制圆弧的矩形,当前这样定义正好让圆弧和圆重合
        RectF oval = new RectF(0, 0, getMeasuredWidth(), getMeasuredWidth());
        //设置进度条(圆弧的颜色)
        paint.setColor(Color.GREEN);
        //绘制,设置进度条的度数从0开始,结束值是个变量,可以自己自由设置,来设置进度
        //true和false 代表是否使用中心点,如果true,代表连接中心点,会出现扇形的效果
        canvas.drawArc(oval, 0, 360 * progress / max, false, paint);
        //文字的绘制
        paint.setTextSize(40);
        //设置文字宽度
        paint.setStrokeWidth(1.0f);
        //测量文字大小-提前准备个矩形
        Rect bounds = new Rect();
        //测量文字的宽和高,测量的值可以根据矩形获取
        paint.getTextBounds(text, 0, text.length(), bounds);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        //绘制文字,计算文字的宽高进行设置
        canvas.drawText(text, getMeasuredWidth()/2 - bounds.width() / 2,
                getMeasuredWidth()/2 + bounds.height() / 2, paint);
    }
    /**
     * 初始设置当前进度的最大值-默认100
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
    }

    public void setProgressAndText(int progress, String text) {
        this.progress = progress;
        this.text = text;
        //重新绘制
        postInvalidate();
    }
    public boolean onTouchEvent(MotionEvent event) {
        this.x= (int) event.getX();
        this.y= (int) event.getY();
        this.invalidate();
        return true;
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值