<!-- 阴影颜色 -->
<attr name="round_circle_shadowColor" format="color" />
<!-- 阴影水平偏移 -->
<attr name="round_circle_shadowOffsetX" format="dimension" />
<!-- 阴影垂直偏移 -->
<attr name="round_circle_shadowOffsetY" format="dimension" />
这里对属性的作用做了注释,很方便理解了。
接下来我们在基类中取出属性值
internal abstract class AbsRoundCirclePolicy(
view: View,
context: Context,
attributeSet: AttributeSet?,
attrs: IntArray,
attrIndex: IntArray
) : IRoundCirclePolicy {
...
var mShadowSize = 0
var mShadowColor = 0
var mShadowOffsetX = 0
var mShadowOffsetY = 0
private fun initialize(context: Context, attributeSet: AttributeSet?, attrs: IntArray, attrIndexs: IntArray) {
val typedArray = context.obtainStyledAttributes(attributeSet, attrs)
...
mShadowSize = typedArray.getDimensionPixelSize(attrIndexs[9], 0)
mShadowColor = typedArray.getColor(attrIndexs[10], 0x10000000)
mShadowOffsetX = typedArray.getDimensionPixelSize(attrIndexs[11], 0)
mShadowOffsetY = typedArray.getDimensionPixelSize(attrIndexs[12], 0)
}
然后我们在具体的策略裁剪类中拿到对应的值,内部我们需要在layout的时候去确定绘制内容的大小。
override fun onLayout(left: Int, top: Int, right: Int, bottom: Int) {
setupRect()
setupBG()
setupShadow()
}
先确定内容的大小,阴影的大小,再初始化绘制对象,初始化阴影对象
//设置Rect
private fun setupRect() {
val rectF = calculateBounds()
val let: Float = rectF.left + mShadowSize
val top: Float = rectF.top + mShadowSize
val right: Float = rectF.right - mShadowSize
val bottom: Float = rectF.bottom - mShadowSize
mDrawableRect.set(let, top, right, bottom)
//阴影的Rect
val shadowLet: Float
val shadowTop: Float
val shadowRight: Float
val shadowBottom: Float
if (mShadowOffsetX > 0) {
shadowLet = let + mShadowOffsetX
shadowRight = right
} else {
shadowLet = let
shadowRight = right + mShadowOffsetX
}
if (mShadowOffsetY > 0) {
shadowTop = top + mShadowOffsetY
shadowBottom = bottom
} else {
shadowTop = top
shadowBottom = bottom + mShadowOffsetY
}
mShadowRect.set(shadowLet, shadowTop, shadowRight, shadowBottom)
//设置画笔和BitmapShader等
private fun setupBG() {
if (mRoundBackgroundDrawable != null && mRoundBackgroundBitmap != null) {
mBitmapWidth = mRoundBackgroundBitmap!!.width
mBitmapHeight = mRoundBackgroundBitmap!!.height
mBitmapShader = BitmapShader(mRoundBackgroundBitmap!!, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
if (mRoundBackgroundBitmap!!.width != 2) {
updateShaderMatrix()
}
mBitmapPaint.isAntiAlias = true
mBitmapPaint.shader = mBitmapShader
}
//阴影的设置与绘制准备
private fun setupShadow() {
if (mShadowSize > 0) {
mShadowPaint.color = Color.TRANSPARENT
mShadowPaint.style = Paint.Style.STROKE
mShadowPaint.strokeWidth = (mShadowSize / 4).toFloat()
// 如果阴影不带透明度,强制给它设置一点透明度
if (ColorUtils.setAlphaComponent(mShadowColor, 255) == mShadowColor) {
mShadowColor = ColorUtils.setAlphaComponent(mShadowColor, 254)
}
mShadowPaint.color = mShadowColor
mShadowPaint.maskFilter = BlurMaskFilter(mShadowSize / 1.2f, BlurMaskFilter.Blur.NORMAL)
} else {
mShadowPaint.clearShadowLayer()
}
当我们全部的对象都初始化之后,总共是分两个步骤,一个是裁剪,一个是绘制,绘制又分背景内容的绘制和阴影的绘制。
在钩子函数中我们是在绘制完成之后再裁剪。
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun beforeDispatchDraw(canvas: Canvas?) {
//5.0版本以上,采用ViewOutlineProvider来裁剪view
mContainer.clipToOutline = true
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
override fun afterDispatchDraw(canvas: Canvas?) {
//5.0版本以上,采用ViewOutlineProvider来裁剪view
mContainer.outlineProvider = object : ViewOutlineProvider() {
override fun getOutline(view: View, outline: Outline) {
if (isCircleType) {
//如果是圆形裁剪圆形
val bounds = Rect()
calculateBounds().roundOut(bounds)
outline.setRoundRect(bounds, bounds.width() / 2.0f)
} else {
//如果是圆角-裁剪圆角
if (mTopLeft > 0 || mTopRight > 0 || mBottomLeft > 0 || mBottomRight > 0) {
//如果是单独的圆角
val path = Path()
path.addRoundRect(
calculateBounds(),
floatArrayOf(mTopLeft, mTopLeft, mTopRight, mTopRight, mBottomRight, mBottomRight, mBottomLeft, mBottomLeft),
Path.Direction.CCW
)
//不支持2阶的曲线
outline.setConvexPath(path)
} else {
//如果是统一圆角
outline.setRoundRect(0, 0, mContainer.width, mContainer.height, mRoundRadius)
}
}
}
}
}
而绘制则是在我们onDraw的钩子函数中实现,需要注意的是我们需要先绘制阴影再绘制内容,这样才能实现阴影在底部的效果。
override fun onDraw(canvas: Canvas?): Boolean {
if (isCircleType) {
if (mShadowSize > 0) {
//阴影的绘制
canvas?.drawOval(mShadowRect, mShadowPaint)
}
//绘制圆角背景图
canvas?.drawCircle(
mDrawableRect.centerX(), mDrawableRect.centerY(),
Math.min(mDrawableRect.height() / 2.0f, mDrawableRect.width() / 2.0f), mBitmapPaint
)
} else {
//自定义圆角
if (mTopLeft > 0 || mTopRight > 0 || mBottomLeft > 0 || mBottomRight > 0) {
if (mShadowSize > 0) {
//阴影的绘制
mShadowPath.reset()
mShadowPath.addRoundRect(
mShadowRect, floatArrayOf(mTopLeft, mTopLeft, mTopRight, mTopRight, mBottomRight, mBottomRight, mBottomLeft, mBottomLeft),
Path.Direction.CW
)
canvas?.drawPath(mShadowPath, mShadowPaint)
}
//使用单独的圆角背景