Skip to content

Commit

Permalink
add effect for right and left sides #8
Browse files Browse the repository at this point in the history
  • Loading branch information
beigirad committed Oct 30, 2020
1 parent 8b4ee12 commit 4cc28ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
50 changes: 46 additions & 4 deletions library/src/main/java/ir/beigirad/zigzagview/ZigzagView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class ZigzagView @JvmOverloads constructor(
rectMain.bottom - zigzagPaddingBottom
)
rectContent.set(
rectZigzag.left + zigzagPaddingContent,
rectZigzag.left + zigzagPaddingContent + (if (containsSide(zigzagSides, ZIGZAG_LEFT)) zigzagHeight else 0f),
rectZigzag.top + zigzagPaddingContent + (if (containsSide(zigzagSides, ZIGZAG_TOP)) zigzagHeight else 0f),
rectZigzag.right - zigzagPaddingContent,
rectZigzag.right - zigzagPaddingContent - if (containsSide(zigzagSides, ZIGZAG_RIGHT)) zigzagHeight else 0f,
rectZigzag.bottom - zigzagPaddingContent - if (containsSide(zigzagSides, ZIGZAG_BOTTOM)) zigzagHeight else 0f
)
super.setPadding(
Expand All @@ -120,12 +120,18 @@ class ZigzagView @JvmOverloads constructor(
val top = rectZigzag.top
val bottom = rectZigzag.bottom
pathZigzag.moveTo(right, bottom)
pathZigzag.lineTo(right, top)
if (containsSide(zigzagSides, ZIGZAG_RIGHT) && zigzagHeight > 0)
drawVerticalSide(pathZigzag, top, right, bottom, isLeft = false)
else
pathZigzag.lineTo(right, top)
if (containsSide(zigzagSides, ZIGZAG_TOP) && zigzagHeight > 0)
drawHorizontalSide(pathZigzag, left, top, right, isTop = true)
else
pathZigzag.lineTo(left, top)
pathZigzag.lineTo(left, bottom)
if (containsSide(zigzagSides, ZIGZAG_LEFT) && zigzagHeight > 0)
drawVerticalSide(pathZigzag, top, left, bottom, isLeft = true)
else
pathZigzag.lineTo(left, bottom)
if (containsSide(zigzagSides, ZIGZAG_BOTTOM) && zigzagHeight > 0)
drawHorizontalSide(pathZigzag, left, bottom, right, isTop = false)
else
Expand Down Expand Up @@ -184,12 +190,48 @@ class ZigzagView @JvmOverloads constructor(
}
}

private fun drawVerticalSide(path: Path, top: Float, x: Float, bottom: Float, isLeft: Boolean) {
val h = zigzagHeight
val seed = 2 * h
val width = bottom - top
val count: Int = (width / seed).toInt()
val diff = width - seed * count
val sideDiff = diff / 2
val halfSeed = seed / 2
val innerHeight = if (isLeft) x + h else x - h
if (!isLeft) {
for (i in count downTo 1) {
val startSeed = i * seed + sideDiff + top.toInt()
var endSeed = startSeed - seed
if (i == 1) {
endSeed -= sideDiff
}
path.lineTo(innerHeight, startSeed - halfSeed)
path.lineTo(x, endSeed)
}
} else {
for (i in 0 until count) {
var startSeed = i * seed + sideDiff + top.toInt()
var endSeed = startSeed + seed
if (i == 0) {
startSeed = top.toInt() + sideDiff
} else if (i == count - 1) {
endSeed += sideDiff
}
path.lineTo(innerHeight, startSeed + halfSeed)
path.lineTo(x, endSeed)
}
}
}

private fun containsSide(flagSet: Int, flag: Int): Boolean {
return flagSet or flag == flagSet
}

companion object {
private const val ZIGZAG_TOP = 1
private const val ZIGZAG_BOTTOM = 2 // default to be backward compatible.Like google ;)
private const val ZIGZAG_RIGHT = 4
private const val ZIGZAG_LEFT = 8
}
}
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<attr name="zigzagSides">
<flag name="top" value="1" />
<flag name="bottom" value="2" />
<flag name="right" value="4" />
<flag name="left" value="8" />
</attr>
<attr name="zigzagShadowAlpha" format="float" />
</declare-styleable>
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
app:zigzagHeight="7dp"
app:zigzagPaddingContent="16dp"
app:zigzagShadowAlpha="0.9"
app:zigzagSides="bottom">
app:zigzagSides="bottom|right">

<TextView
android:layout_width="match_parent"
Expand All @@ -35,7 +35,7 @@
app:zigzagHeight="10dp"
app:zigzagPaddingContent="16dp"
app:zigzagShadowAlpha="1"
app:zigzagSides="top|bottom">
app:zigzagSides="top|bottom|left|right">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down

0 comments on commit 4cc28ae

Please sign in to comment.