zoukankan      html  css  js  c++  java
  • 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>

    上两种实现效果:

  • 相关阅读:
    2018 ACM-ICPC 中国大学生程序设计竞赛暨丝绸之路程序设计竞赛
    牛客小白月赛2
    求n个数中前k个数,按之前的顺序输出(HK IPM)
    js_复选框单选与复选
    js_判断字符串中出现最多的字符的和次数
    js_倒计时
    海通证券面试
    上海利莫面试
    中焯信息面试
    富途面试
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/9133776.html
Copyright © 2011-2022 走看看