Created
November 17, 2020 16:24
-
-
Save pantos27/04d4587174cc29a5219ae99ebac6e368 to your computer and use it in GitHub Desktop.
ConstraintLayout with an option to grey out the entire content (disabled like)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.grayscaleexperiment | |
import android.content.Context | |
import android.graphics.Canvas | |
import android.graphics.ColorMatrix | |
import android.graphics.ColorMatrixColorFilter | |
import android.graphics.Paint | |
import android.util.AttributeSet | |
import androidx.constraintlayout.widget.ConstraintLayout | |
//origin: https://github.com/iabhishek1041/grayscale_experiment/blob/master/app/src/main/java/com/example/grayscaleexperiment/ConstraintLayoutWithDisableSupport.kt | |
class ConstraintLayoutWithDisableSupport @JvmOverloads constructor( | |
context: Context, | |
attrs: AttributeSet? = null, | |
defStyleAttr: Int = 0 | |
) : ConstraintLayout(context, attrs, defStyleAttr) { | |
var disabled = false | |
set(value) { | |
field = value | |
requestLayout() | |
} | |
private val paint = Paint() | |
init { | |
val cm = ColorMatrix() | |
cm.set( | |
floatArrayOf( | |
0.33f, 0.33f, 0.33f, 0f, 0f, | |
0.33f, 0.33f, 0.33f, 0f, 0f, | |
0.33f, 0.33f, 0.33f, 0f, 0f, | |
0f, 0f, 0f, 1f, 0f | |
) | |
) | |
paint.colorFilter = ColorMatrixColorFilter(cm) | |
} | |
override fun dispatchDraw(canvas: Canvas?) { | |
if (disabled) { | |
canvas?.saveLayer(null, paint) | |
} | |
super.dispatchDraw(canvas) | |
if (disabled) { | |
canvas?.restore() | |
} | |
} | |
override fun draw(canvas: Canvas?) { | |
if (disabled) { | |
canvas?.saveLayer(null, paint) | |
} | |
super.draw(canvas) | |
if (disabled) { | |
canvas?.restore() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment