自定义圆加载进度单点触控


//布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bw.u.day02dandianchukong.MainActivity">

    <com.bw.u.day02dandianchukong.MyView
    android:id="@+id/cv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="点击加载"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="startJia"
        android:text="点击加载加速"
        android:layout_above="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/button2" />
</RelativeLayout>
//代码部分
 
package com.bw.u.day02dandianchukong;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends AppCompatActivity {


    int max=360;
    int current=0;
    MyView cv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cv = (MyView) findViewById(R.id.cv);

    }
    public void start(View view){
        current=0;
        new Thread(new Runnable() {
            public void run() {
                while(current<=360){
                    current++;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    cv.addrest(max,current);
                }
            }
        }){}.start();
    }
    //点击加速
    public void startJia(View view){
        current+=2;
        new Thread(new Runnable() {
            public void run() {
                while(current<=360){
                    current++;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    cv.addrest(max,current);
                }
            }
        }){}.start();
    }
    }
//继承view
 
package com.bw.u.day02dandianchukong;

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.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class MyView extends View{
    //声明画笔
    Paint paint;
    //声明第二个画笔
    Paint cpaint;
    //声明进度画笔
    Paint jpaint;
    //最大值
    int max = 360;
    //当前进度
    int current;
    float textsize = 30;
    //创建方法发送线程
    public void addrest(int max, int current) {
        this.max = max;
        this.current = current;
        postInvalidate();
    }
    private float x=202;
    private float y=317;
    public MyView(Context context) {
        this(context,null);
    }
    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,R.style.AppTheme);
    }
    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        circleview(canvas);
    }

    public  void circleview(Canvas canvas){
        //创建画圆的画笔
        paint = new Paint();

        cpaint = new Paint();
        jpaint = new Paint();

        int width = getWidth();
        //圆的中心
        // int center=width/2;
        //获取圆的半径
        int radio = (width - 20) / 2;
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        //设置进度条的颜色或者格式
        cpaint.setColor(Color.BLUE);
        cpaint.setStrokeWidth(3);
        cpaint.setStyle(Paint.Style.STROKE);
        cpaint.setAntiAlias(true);
        //设置字体的颜色或格式
        jpaint.setColor(Color.GREEN);
        jpaint.setStrokeWidth(2);
        jpaint.setTextSize(textsize);
        //用画笔画出一个圆
        canvas.drawCircle(x, y, radio, paint);
        //设置进度条的方向或
        RectF rectF = new RectF(x - radio, y - radio, x + radio, y + radio);
        canvas.drawArc(rectF, 0, 360*current/max, false, cpaint);
        //进度条百分比
        int perent=(int) ((current*1.0/max*1.0)*100);
        float textWidth=paint.measureText(perent+"%");
        canvas.drawText(perent+"%",x-textWidth/2,y+textWidth/2,jpaint);

    }

    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Toast.makeText(getContext(),"点击圆内",Toast.LENGTH_SHORT).show();
            break;
            case MotionEvent.ACTION_MOVE:
                this.x=event.getX();
                this.y=event.getY();
                this.invalidate();
                break;
        }
        return true;
    }

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值