Skip to content

Instantly share code, notes, and snippets.

View ricknout's full-sized avatar
🌚
Probably coding/designing

Nick Rout ricknout

🌚
Probably coding/designing
View GitHub Profile
@ricknout
ricknout / mdc_shape_shapeable_image_view.xml
Created March 17, 2021 13:31
MDC-Android ShapeableImageView
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
<!-- In res/values/shape.xml -->
<style name="ShapeAppearanceOverlay.App.Image" parent="">
<item name="cornerSize">0dp</item>
<item name="cornerSizeTopLeft">50%</item>
</style>
<!-- In layout -->
@ricknout
ricknout / mdc_shape_elevation_custom_views.kt
Created July 31, 2020 20:28
MDC-Android shape elevation in custom views
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
class AppCustomView ... {
...
private lateinit var materialShapeDrawable: MaterialShapeDrawable
override fun setElevation(elevation: Float) {
@ricknout
ricknout / mdc_shape_custom_views.xml
Created July 31, 2020 20:23
MDC-Android shape in custom views
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
<!-- In res/values/attrs.xml -->
<declare-styleable name="AppCustomView">
<attr name="shapeAppearance" />
...
</declare-styleable>
<!-- In res/values/styles.xml -->
@ricknout
ricknout / mdc_msd_corner_interpolation.kt
Created July 31, 2020 20:04
MDC-Android MaterialShapeDrawable corner interpolation
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Set corner interpolation to half of current cornerSize(s)
msd.setInterpolation(0.5f)
@ricknout
ricknout / mdc_msd_shadow_rendering.kt
Created July 31, 2020 20:02
MDC-Android MaterialShapeDrawable shadow rendering
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
/**
* Set shadow compat mode to be one of:
* - SHADOW_COMPAT_MODE_DEFAULT: Use platform rendering on API 21+, else compat rendering
* - SHADOW_COMPAT_MODE_NEVER: Use platform rendering always
* - SHADOW_COMPAT_MODE_ALWAYS: Use compay rendering always
*/
msd.setShadowCompatibilityMode(shadowMode)
@ricknout
ricknout / mdc_msd_elevation_overlays.kt
Created July 31, 2020 19:55
MDC-Android MaterialShapeDrawable elevation and overlays
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Initialize elevation overlays
msd.initializeElevationOverlay(context)
// Pass elevation value to MSD to apply overlay (in dark theme)
msd.setElevation(elevation)
@ricknout
ricknout / mdc_msd_fill_stroke.kt
Last active August 25, 2020 18:04
MDC-Android MaterialShapeDrawable fill and stroke
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Fill color
msd.setFillColor(fillColorStateList)
// Stroke color
msd.setStrokeColor(strokeColorStateList)
// Stroke width
msd.setStrokeWidth(strokeWidthDimension)
@ricknout
ricknout / mdc_sam_instantiation.kt
Last active August 25, 2020 18:04
MDC-Android ShapeAppearanceModel instantiation
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Default builder
val sam = ShapeAppearanceModel.builder()
.setAllCorners(CornerFamily.CUT, cornerSize)
// Also setTopRightCorner, setAllEdges, etc.
.build()
// Style/attr resources builder (reads shapeAppearance and shapeAppearanceOverlay)
val samFromStyles = ShapeAppearanceModel.builder(context, attrs, defStyleAttr, defStyleRes)
@ricknout
ricknout / mdc_msd_instatiation.kt
Last active August 25, 2020 18:04
MDC-Android MaterialShapeDrawable instantiation
/* Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
// Default constructor
val msd = MaterialShapeDrawable()
// ShapeAppearanceModel constructor
val msdFromSam = MaterialShapeDrawable(shapeAppearanceModel)
// Style/attr resources constructor (reads shapeAppearance and shapeAppearanceOverlay)
val msdFromStyles = MaterialShapeDrawable(context, attrs, defStyleAttr, defStyleRes)
// Cast from widget background
@ricknout
ricknout / mdc_shape_theme_attrs.xml
Created July 31, 2020 14:24
MDC-Android shape theme attributes
<!-- Copyright 2020 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
<!-- In res/values/themes.xml -->
<style name="Theme.App.Base" parent="Theme.MaterialComponents.*">
...
<item name="shapeAppearanceSmallComponent">
@style/ShapeAppearance.App.SmallComponent
</item>
<item name="shapeAppearanceMediumComponent">