Android自定义View绘制圆形

自定义View绘画一个圆形

实现步骤:

步骤一:

  创建一个类circle继承View


public class cicle extends View {

    //    定义画笔
    Paint paint;

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

    //    重写draw方法
    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

//        实例化画笔对象
        paint = new Paint();
//        给画笔设置颜色
        paint.setColor(Color.RED);
//        设置画笔属性
//        paint.setStyle(Paint.Style.FILL);//画笔属性是实心圆
        paint.setStyle(Paint.Style.STROKE);//画笔属性是空心圆
        paint.setStrokeWidth(8);//设置画笔粗细

        /*四个参数:
                参数一:圆心的x坐标
                参数二:圆心的y坐标
                参数三:圆的半径
                参数四:定义好的画笔
                */
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, 200, paint);

    }


}

步骤二:

  将自定义好的类circle在主类的布局文件中引用

扫描二维码关注公众号,回复: 1452573 查看本文章
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_height="match_parent"
    tools:context="com.contentprovide.liuliu.clock.MainActivity">

<com.contentprovide.liuliu.clock.cicle
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

上两种实现效果:

猜你喜欢

转载自www.cnblogs.com/lyd447113735/p/9133776.html