自定义View画圆,画扇形,进度条

博客主要介绍了在Android开发中自定义View的相关内容,包括如何使用自定义View来画圆、画扇形,还提及了进度条相关,属于移动开发领域的技术内容。

自定义View画圆,画扇形

创建一个类继承自View
public class lhjclass extends View {
    public lhjclass(Context context) {
        super(context);
    }

    public lhjclass(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public lhjclass(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //创建画笔
        Paint paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.WHITE);
        //设置画笔线条
        paint.setStrokeWidth(20);
        //设置样式STROKE(空心),FILL(实心)
        paint.setStyle(Paint.Style.STROKE);
        //画扇形(true,false表示扇形两边的线是否显示)
        canvas.drawArc(320,180,570,430,90,270,false,paint);

        //同心圆
       /*  paint.setColor(Color.WHITE);
           canvas.drawCircle(450,300,180,paint);//画圆
           paint.setColor(Color.GREEN);
           canvas.drawCircle(450,300,150,paint);*/
    }
}
布局直接使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


        <com.example.a1225.lhjclass
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:background="#1AFF00"
            ></com.example.a1225.lhjclass>

</LinearLayout>
效果图

在这里插入图片描述

进度条


public class HomeView extends View {
    private int count=0;
    public HomeView(Context context) {
        super(context);
    }

    public HomeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        //画一个空心圆
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(20);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(250,250,600,600,0,count,false,paint);
        //每秒发送一下作为延时
        handler.sendEmptyMessage(1);
    }
    private Handler handler=new Handler(){
        @Override
        public void dispatchMessage(@NonNull Message msg) {
            super.dispatchMessage(msg);
            if (count<=360){
                count=count+1;
                handler.sendEmptyMessageDelayed(1,1000);
                invalidate();
            }

        }
    };
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值