kotlin自定义一个简单的CircleView

1.自定义CircleView继承自View

package com.example.myapplication.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.example.myapplication.R
import kotlin.math.min

/**
 *@author: Administrator
 *@date: 2021/7/1
 *@description:圆形CircleView
 */
public class CircleView : View {
    //颜色
    private var mColor: Int = Color.RED

    //画笔-->支持抗锯齿
    private val mPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

    //三个构造
    constructor(context: Context) : super(context) {
        init()
    }


    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        //带自定义属性的自定义view需要在第二个构造方法里解析我们自定义的属性
        val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView)
        //没有指定的话默认红色
        mColor = typedArray.getColor(R.styleable.CircleView_circle_color,Color.RED)
        //释放资源
        typedArray.recycle()
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {

        init()
    }

    private fun init() {
        mPaint.color = mColor
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST
            && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST
        ) {
            //最大不超过300--绝对够用了
            setMeasuredDimension(300, 300)
        } else if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.AT_MOST) {
            setMeasuredDimension(300, heightMeasureSpec)
        } else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthMeasureSpec, 300)
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //必须要考虑到自身设置wrap_content属性的时候以及自身内边距padding是否生效的问题
        val width = width - paddingLeft - paddingRight
        val height = height - paddingTop - paddingBottom
        val radius = min(width, height) / 2
        canvas?.drawCircle(
            paddingLeft + width / 2.0f,
            paddingTop + height / 2.0f,
            radius.toFloat(),
            mPaint
        )
    }
}

2.在res下面的value资源文件夹下自定义属性标签attr资源

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CircleView">
        //给circleView中的圆形设置颜色
        <attr name="circle_color" format="color" />
    </declare-styleable>
</resources>

3.在layout中的使用

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <com.example.myapplication.widget.CircleView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#424242"
        android:padding="10dp"
        app:circle_color="#30DAC5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

4.效果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值