Skip to content

Commit

Permalink
新增阴影透明度属性,取值范围[0,1](如果阴影颜色带有透明度自动取颜色透明度,否则取shadowColorAlpha的值,默认是0.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
lygttpod committed Oct 10, 2019
1 parent f038fd1 commit 573a7a5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class AttributeSetData {
//////////阴影相关////////
var showShadow: Boolean = false
var shadowColor: Int = Color.GRAY
var shadowColorAlpha: Float = 0.2f
var shadowLeftWidth: Float = 0f
var shadowTopWidth: Float = 0f
var shadowRightWidth: Float = 0f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class AttributeSetHelper {

attributeSetData.showShadow = typedArray.getBoolean(R.styleable.ShapeView_showShadow, false)
attributeSetData.shadowColor = typedArray.getColor(R.styleable.ShapeView_shadowColor, Color.GRAY)
attributeSetData.shadowColorAlpha = typedArray.getFloat(R.styleable.ShapeView_shadowColorAlpha, 0.2f)
attributeSetData.shadowLeftWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shadowLeftWidth, 0).toFloat()
attributeSetData.shadowTopWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shadowTopWidth, 0).toFloat()
attributeSetData.shadowRightWidth = typedArray.getDimensionPixelSize(R.styleable.ShapeView_shadowRightWidth, 0).toFloat()
Expand Down
23 changes: 11 additions & 12 deletions library/src/main/java/com/allen/library/helper/ShadowHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ShadowHelper {

private var shadowColor = Color.GRAY

private var shadowColorAlpha = 0
private var shadowColorAlpha = 0f

private var shadowColorAlphaList: MutableList<Int> = mutableListOf()

Expand All @@ -70,9 +70,6 @@ class ShadowHelper {

private var maxRadius = 0f

companion object {
private const val DEFAULT_SHADOW_COLOR_ALPHA = 0.2
}

fun init(targetView: View?, attributeSetData: AttributeSetData) {
this.attributeSetData = attributeSetData
Expand Down Expand Up @@ -119,9 +116,11 @@ class ShadowHelper {
}

private fun setShadowColor() {
shadowColorAlpha = Color.alpha(shadowColor)
if (shadowColorAlpha == 255) {
shadowColorAlpha = (DEFAULT_SHADOW_COLOR_ALPHA * 255).toInt()
shadowColorAlpha = attributeSetData.shadowColorAlpha

val colorAlpha = Color.alpha(shadowColor)
if (colorAlpha != 255) {
shadowColorAlpha = colorAlpha / 255f
}
}

Expand Down Expand Up @@ -218,7 +217,7 @@ class ShadowHelper {
shadowColorRect.set(i + fixBorder, i + fixBorder, width - i - fixBorder, height - i - fixBorder)
shadowColorPath.addRoundRect(shadowColorRect, shadowRadii, Path.Direction.CW)
shadowColorPathList.add(shadowColorPath)
shadowColorAlphaList.add((shadowColorAlpha * i / shadowMaxWidth).toInt())
shadowColorAlphaList.add((shadowColorAlpha * 255 * i / shadowMaxWidth).toInt())
}
}

Expand Down Expand Up @@ -328,11 +327,11 @@ class ShadowHelper {
/**
* set the alpha component [0..1]
*/
fun setShadowColorAlpha(shadowColorAlphaPercent: Double): ShadowHelper {
fun setShadowColorAlpha(shadowColorAlphaPercent: Float): ShadowHelper {
when {
shadowColorAlpha < 0 -> this.shadowColorAlpha = 0
shadowColorAlpha > 1 -> this.shadowColorAlpha = 255
else -> this.shadowColorAlpha = (shadowColorAlphaPercent * 255).toInt()
shadowColorAlpha < 0 -> this.shadowColorAlpha = 0f
shadowColorAlpha > 1 -> this.shadowColorAlpha = 1f
else -> this.shadowColorAlpha = shadowColorAlphaPercent
}
return this
}
Expand Down
30 changes: 6 additions & 24 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@
<attr name="showShadow" format="boolean" />

<attr name="shadowColor" format="color" />
<attr name="shadowColorAlpha" format="float" />

<attr name="shadowLeftWidth" format="dimension" />
<attr name="shadowTopWidth" format="dimension" />
Expand Down Expand Up @@ -517,18 +518,6 @@
<attr name="shapeGradientUseLevel" />
<attr name="shapeUseSelector" />
<attr name="shapeGradientType" />

<attr name="showShadow" />
<attr name="shadowColor" />
<attr name="shadowLeftWidth" />
<attr name="shadowTopWidth" />
<attr name="shadowBottomWidth" />
<attr name="shadowRightWidth" />
<attr name="shadowCornersRadius" />
<attr name="shadowCornersTopLeftRadius" />
<attr name="shadowCornersTopRightRadius" />
<attr name="shadowCornersBottomLeftRadius" />
<attr name="shadowCornersBottomRightRadius" />
</declare-styleable>

<declare-styleable name="ShapeButton">
Expand Down Expand Up @@ -558,18 +547,6 @@
<attr name="shapeGradientUseLevel" />
<attr name="shapeUseSelector" />
<attr name="shapeGradientType" />

<attr name="showShadow" />
<attr name="shadowColor" />
<attr name="shadowLeftWidth" />
<attr name="shadowTopWidth" />
<attr name="shadowBottomWidth" />
<attr name="shadowRightWidth" />
<attr name="shadowCornersRadius" />
<attr name="shadowCornersTopLeftRadius" />
<attr name="shadowCornersTopRightRadius" />
<attr name="shadowCornersBottomLeftRadius" />
<attr name="shadowCornersBottomRightRadius" />
</declare-styleable>

<declare-styleable name="ShapeCardView">
Expand Down Expand Up @@ -602,6 +579,7 @@

<attr name="showShadow" />
<attr name="shadowColor" />
<attr name="shadowColorAlpha" />
<attr name="shadowLeftWidth" />
<attr name="shadowTopWidth" />
<attr name="shadowBottomWidth" />
Expand Down Expand Up @@ -643,6 +621,7 @@

<attr name="showShadow" />
<attr name="shadowColor" />
<attr name="shadowColorAlpha" />
<attr name="shadowLeftWidth" />
<attr name="shadowTopWidth" />
<attr name="shadowBottomWidth" />
Expand Down Expand Up @@ -684,6 +663,7 @@

<attr name="showShadow" />
<attr name="shadowColor" />
<attr name="shadowColorAlpha" />
<attr name="shadowLeftWidth" />
<attr name="shadowTopWidth" />
<attr name="shadowBottomWidth" />
Expand Down Expand Up @@ -725,6 +705,7 @@

<attr name="showShadow" />
<attr name="shadowColor" />
<attr name="shadowColorAlpha" />
<attr name="shadowLeftWidth" />
<attr name="shadowTopWidth" />
<attr name="shadowBottomWidth" />
Expand Down Expand Up @@ -766,6 +747,7 @@

<attr name="showShadow" />
<attr name="shadowColor" />
<attr name="shadowColorAlpha" />
<attr name="shadowLeftWidth" />
<attr name="shadowTopWidth" />
<attr name="shadowBottomWidth" />
Expand Down

0 comments on commit 573a7a5

Please sign in to comment.