1.Android阴影
-
Android中阴影制作通常有以下几种
1.使用layer-list使用不同层级实现
2.使用Paint的setShadowLayer绘制阴影
3.使用BlurMasKFilter对象 -
特点
第一种:使用layer-list
通过分层叠加方式实现,无发散效果,并且对于图片等不能处理第二种:使用Paint的setShadowLayer
这种方式使用SDK提供API方法,绘制视图时调用setShadowLayer方法,可以设置阴影模糊半径,X,Y偏移量。 多用在设置绘制背景时候。第三种:使用BlurMasKFilter对象 这种方式同样是使用Paint的API调用,相比setShadowLayer可以处理图片阴影问题,功能比较强大
注意:主要讲解自定义Drawable和使用第二种方式绘制阴影
2.定义阴影Drawable
- 1.定义在styleable,一般定义在res/values/attrs.xml中
<declare-styleable name="shade_widget">
<attr name="top" format="dimension"/>
<attr name="left" format="dimension"/>
<attr name="right" format="dimension"/>
<attr name="bottom" format="dimension"/>
<attr name="shadeX" format="dimension"/>
<attr name="shadeY" format="dimension"/>
<attr name="solid_color" format="color"/>
<attr name="stroke_color" format="color"/>
<attr name="shade_corner" format="dimension"/>
<attr name="shade_radius" format="integer"/>
</declare-styleable>
属性作用:
top left right bottom分别是主体显示以外部分防止View设置过小对阴影切割
shadeX shadeY 为阴影偏移量向X,Y轴偏移,主体部分不会动阴影会根据X,Y移动。X>0向右移动,否则向左移动。Y>0向下移动,否则向上移动
solid_color设置主体部分颜色。stroke_color阴影部分颜色
shade_corner 绘制的drawable的圆角
shade_radius阴影部分的发散程度,越大越模糊
- 2.自定义Drawable代码
package com.kt.view.draw
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import androidx.annotation.RequiresApi
import com.kt.view.R
import org.xmlpull.v1.XmlPullParser
class ShadeWidget(): Drawable() {
private var paint: Paint? = null
private var mRect: Rect? = null
private var newPaint = Paint()
private var shadeTop = 0
private var shadeBottom = 0
private var shadeRight = 0
private var shadeLeft = 0
private var shadeCorner = 0
private var shadeRadius = 0
private var shadeX = 0
private var shadeY = 0
private var solidColor = Color.TRANSPARENT
private var strokeColor = Color.TRANSPARENT
init {
paint = Paint()
mRect = Rect()
}
override fun inflate(
r: Resources,
parser: XmlPullParser,
attrs: AttributeSet,
theme: Resources.Theme?
) {
super.inflate(r, parser, attrs, theme)
val attributes = r.obtainAttributes(attrs, R.styleable.shade_widget)
shadeTop = attributes.getDimensionPixelSize(R.styleable.shade_widget_top, 0)
shadeLeft = attributes.getDimensionPixelSize(R.styleable.shade_widget_left, 0)
shadeRight = attributes.getDimensionPixelSize(R.styleable.shade_widget_right, 0)
shadeBottom = attributes.getDimensionPixelSize(R.styleable.shade_widget_bottom, 0)
solidColor = attributes.getColor(R.styleable.shade_widget_solid_color, Color.TRANSPARENT)
strokeColor = attributes.getColor(R.styleable.shade_widget_stroke_color, Color.BLACK)
shadeX = attributes.getColor(R.styleable.shade_widget_shadeX, 0)
shadeY = attributes.getColor(R.styleable.shade_widget_shadeY, 0)
shadeCorner = attributes.getDimensionPixelSize(R.styleable.shade_widget_shade_corner,0)
shadeRadius = attributes.getInt(R.styleable.shade_widget_shade_radius,0)
attributes.recycle()
}
@RequiresApi(Build.VERSION_CODES.Q)
override fun draw(canvas: Canvas) {
newPaint.setColor(solidColor)
mRect!!.set(bounds.left+shadeLeft, bounds.top+shadeTop, bounds.right-shadeRight, bounds.bottom-shadeBottom)
newPaint.setShadowLayer(shadeRadius.toFloat(), 100F, 100F, strokeColor)
canvas.drawRoundRect将Drawable绘制出来。
+ 3(RectF(mRect), shadeCorner.toFloat(), shadeCorner.toFloat(), newPaint)
}
override fun setAlpha(alpha: Int) {
paint!!.alpha = alpha
}
override fun setColorFilter(colorFilter: ColorFilter?) {
paint!!.setColorFilter(colorFilter)
}
override fun getOpacity(): Int {
return PixelFormat.TRANSLUCENT
}
}
上面代码核心为draw方法,在此处调用setShadowLayer设置阴影,最终由canvas.drawRoundRect将Drawable绘制出来。
- 3 drawble使用 demo
<?xml version="1.0" encoding="utf-8"?>
<drawable class="com.kt.view.draw.ShadeWidget"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
app:top="50dp"
app:bottom="50dp"
app:left="50dp"
app:right="50dp"
app:shade_corner="10dp"
app:shade_radius="130"
app:solid_color="#000000"
app:stroke_color="#666666">
</drawable>
上面是一个简单的使用demo,显示效果如下:
-
知识点:
1.自定义Drawable
2.阴影绘制
3.绘制Drawable流程,主要是draw()中形式。
推荐《Android自定义控件入门与实战》