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.效果