自定义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在主类的布局文件中引用
<?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>
上两种实现效果: