• 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在主类的布局文件中引用

    <?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>

    上两种实现效果:

  • 相关阅读:
    2.pt-table-checksum工具
    Mysql8.0新特性01
    12.redis 之阅读大佬文章随笔
    4.Mysql之Mysqldump命令
    5. 关于高负载服务器Kernel的TCP参数优化
    Mysql Oracle 备份表数据、批量删除表数据
    Mysql limit用法
    Java 字符串数组转字符串
    Layui 自定义年份下拉框并且可输入
    Mysql 生成UUID
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/9133776.html
Copyright © 2020-2023  润新知