黑白化主题实现 - SheTieJun/BaseKit GitHub Wiki

核心代码

private val mPaint = Paint()
private val mColorMatrix = ColorMatrix()

  // 设置饱和度为0: 黑白化  ,正常1   
fun getSatPaint(sat:Float = 1f): Paint {
        mColorMatrix.setSaturation(sat)
        mPaint.colorFilter = ColorMatrixColorFilter(mColorMatrix)
        return mPaint
}

如何快速全局修改

fun FragmentActivity.grayThemChange(isGrayTheme:Boolean){
    val decorView = window?.decorView
    val isMourn = (decorView?.getTag(R.id.isGrayTheme) as? Boolean )?:false
    if (!isMourn){
        if (isGrayTheme){
            // 黑白化
            decorView?.setTag(R.id.isGrayTheme,true)
            decorView?.setLayerType(View.LAYER_TYPE_HARDWARE, getSatPaint(0f))
        }
    }else{
        if (!isGrayTheme){
            //变化正常
            decorView?.setTag(R.id.isGrayTheme,false)
            decorView?.setLayerType(View.LAYER_TYPE_NONE, null)
        }
}