Skip to content
Permalink
Browse files

Update header to use MaterialShapeDrawable

  • Loading branch information...
chrisbanes committed Sep 8, 2019
1 parent 10dcc35 commit 36d5a56fd82df3a873bcddf89f6dce4bbf6cf2b7
@@ -48,7 +48,6 @@
android:id="@+id/header_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorSurface"
android:paddingHorizontal="4dp"
app:dividerMarginEnd="@dimen/spacing_normal"
app:dividerMarginStart="@dimen/spacing_normal" />
@@ -24,6 +24,7 @@ import android.view.View
import android.widget.FrameLayout
import androidx.core.content.res.use
import app.tivi.common.ui.R
import com.google.android.material.shape.MaterialShapeDrawable

class HeaderHolderFrameLayout @JvmOverloads constructor(
context: Context,
@@ -53,16 +54,22 @@ class HeaderHolderFrameLayout @JvmOverloads constructor(
}

init {
context.obtainStyledAttributes(attrs, R.styleable.HeaderHolderFrameLayout, defStyle, 0).use {
context.obtainStyledAttributes(attrs, R.styleable.HeaderHolderFrameLayout,
defStyle, R.style.Widget_Tivi_HeaderHolderFrameLayout).use {
dividerDrawable = it.getDrawable(R.styleable.HeaderHolderFrameLayout_dividerDrawable)
dividerMarginStart = it.getDimensionPixelSize(
R.styleable.HeaderHolderFrameLayout_dividerMarginStart, 0)
dividerMarginEnd = it.getDimensionPixelSize(
R.styleable.HeaderHolderFrameLayout_dividerMarginEnd, 0)
background = MaterialShapeDrawable.createWithElevationOverlay(context, elevation).apply {
fillColor = it.getColorStateList(R.styleable.HeaderHolderFrameLayout_materialBackgroundColor)
}
}
}

override fun draw(canvas: Canvas) {
updateElevationRelativeToParentSurface()

super.draw(canvas)

dividerDrawable?.run {
@@ -78,4 +85,55 @@ class HeaderHolderFrameLayout @JvmOverloads constructor(
}
dividerDrawable?.draw(canvas)
}

override fun setBackground(background: Drawable?) {
super.setBackground(background)

if (background is MaterialShapeDrawable) {
background.elevation = elevation
background.translationZ = translationZ
}
}

override fun setElevation(elevation: Float) {
super.setElevation(elevation)

val bg = background
if (bg is MaterialShapeDrawable) {
bg.elevation = elevation
}
}

override fun setTranslationZ(translationZ: Float) {
super.setTranslationZ(translationZ)

val bg = background
if (bg is MaterialShapeDrawable) {
bg.translationZ = translationZ
}
}

private fun updateElevationRelativeToParentSurface() {
val bg = background
if (bg is MaterialShapeDrawable) {

var v = parent
var cumulativeElevation = elevation

// Iterate through our parents, until we find a view with a MaterialShapeDrawable
// background (the 'parent surface'). We then update our background, based on the
// 'parent surface's elevation
while (v is View) {
val vBg = v.background
if (vBg is MaterialShapeDrawable) {
bg.elevation = vBg.elevation.coerceAtLeast(v.elevation) + cumulativeElevation
break
} else {
cumulativeElevation += v.elevation
}

v = v.getParent()
}
}
}
}
@@ -40,5 +40,6 @@
<attr name="dividerDrawable" format="reference" />
<attr name="dividerMarginStart" format="dimension" />
<attr name="dividerMarginEnd" format="dimension" />
<attr name="materialBackgroundColor" />
</declare-styleable>
</resources>
@@ -30,4 +30,9 @@
<item name="fastScrollVerticalTrackDrawable">@drawable/recyclerview_scroll_track</item>
</style>

<style name="Widget.Tivi.HeaderHolderFrameLayout" parent="">
<item name="dividerDrawable">?android:attr/listDivider</item>
<item name="materialBackgroundColor">?attr/colorSurface</item>
</style>

</resources>
@@ -51,8 +51,6 @@
android:id="@+id/header_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorSurface"
app:dividerDrawable="?android:attr/listDivider"
app:dividerMarginEnd="@dimen/spacing_normal"
app:dividerMarginStart="@dimen/spacing_normal" />

@@ -44,10 +44,8 @@
android:id="@+id/header_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorSurface"
app:dividerMarginStart="@dimen/spacing_normal"
app:dividerMarginEnd="@dimen/spacing_normal"
app:dividerDrawable="?android:attr/listDivider" />
app:dividerMarginEnd="@dimen/spacing_normal" />

</FrameLayout>

@@ -44,10 +44,8 @@
android:id="@+id/header_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorSurface"
app:dividerMarginStart="@dimen/spacing_normal"
app:dividerMarginEnd="@dimen/spacing_normal"
app:dividerDrawable="?android:attr/listDivider" />
app:dividerMarginStart="@dimen/spacing_normal" />

</FrameLayout>

0 comments on commit 36d5a56

Please sign in to comment.
You can’t perform that action at this time.