美文网首页
关于黑暗模式的一些适配

关于黑暗模式的一些适配

作者: 案玉璃青 | 来源:发表于2020-03-26 11:08 被阅读0次

检测是否为黑暗模式

fun Context.isNightMode() = with(this) {

    resources.configuration.uiModeand Configuration.UI_MODE_NIGHT_MASK ==         Configuration.UI_MODE_NIGHT_YES

}

Color 适配

直接在 res 目录下再新建一个 values-night/colors.xml 目录文件,与国际化适配方式一致。

黑暗模式颜色适配

图片适配

1. 同 Color,创建对应的 drawable-night 目录,将黑暗模式对应的图片放在里面

2. 灰阶颜色反转

/**

* 反转灰阶颜色

* @param isRecycle 是否将原 bitmap 回收

* @param chromatism 色差值

*/

fun Bitmap.reverseGray(isRecycle: Boolean = true, chromatism:Int = 18):Bitmap {

    var count= 0

    val mArrayColorLength:Int = width * height

    val mArrayColor= IntArray(mArrayColorLength)

    val bitmapW:Int = width

    val bitmapH:Int = height

    for (iin 0 until height) {

        for (jin 0 until width) {

            // 获得 Bitmap 中每一个点的 color 颜色值

            var color= getPixel(j, i)

            // 将颜色值存在一个数组中 方便后面修改

            // mArrayColor[count] = color;

            var r:Int = color.red

            var g:Int = color.green

            var b:Int = color.blue

            if (maxOf(r, g, b) - minOf(r, g, b) < chromatism) {

                r= 255 - r

                g= 255 - g

                b= 255 - b

            }

            color= Color.argb(color.alpha, r, g, b)

            mArrayColor[count] = color

            count++

        }

    }

    if (isRecycle) { recycle()}

    return Bitmap.createBitmap(mArrayColor, bitmapW, bitmapH, config)

}

使用(视图使用 AnKo):在 applyRecursively 方法中

applyRecursively {

    when (it) {

        is TextView -> {

            if (ctx.isNightMode() && !it.compoundDrawables.isNullOrEmpty()) {

                val drawableList= arrayListOf<Drawable?>(null, null, null, null)

                it.compoundDrawables.forEachIndexed { index, drawable ->

                    if (drawable != null && drawable is BitmapDrawable) {

                        drawableList[index] =

                            drawable.bitmap.reverseGray(false).toDrawable(ctx.resources)

                    } else {

                        drawableList[index] = drawable

                    }

                }

                it.setCompoundDrawablesWithIntrinsicBounds(

                    drawableList[0], drawableList[1], drawableList[2], drawableList[3]

                )

            }

        }

        is ImageView -> {

            if (it.tag != tagReallyImage&& ctx.isNightMode()) {

                it.post {

                    // 此处不知道什么原因,如果它的 layoutParams 宽高设置为 wrapContent 时,会显示不出,以后知道原因了会写明,有朋友知道的话也请留言告知,感谢。

                    val lp = it.layoutParams

                    it.layoutParams = ViewGroup.MarginLayoutParams(matchParent,                                                     matchParent)

                    it.imageBitmap = it.toBitmap()?.reverseGray()

                    it.layoutParams = lp

// toBitmap 方法看这  View 的一些扩展方法

                }

            }

        }

    }

}

全局黑白化(转自鸿洋大神的一篇公众文

val paint= Paint()

val cm= ColorMatrix()

cm.setSaturation(0f)

paint.colorFilter = ColorMatrixColorFilter(cm)

window.decorView.setLayerType(View.LAYER_TYPE_HARDWARE, paint)

相关文章

  • 关于黑暗模式的一些适配

    检测是否为黑暗模式 fun Context.isNightMode() = with(this) { resou...

  • iOS 13 黑暗模式(Dark Mode)的适配

    正文 记录适配黑暗模式中的注意点,若有不足和错误请指导,谢谢 目录 什么是黑暗模式,是否需要强制适配如何获取当前模...

  • 接口适配器模式-电压问题

    接口适配器模式介绍 一些书籍称为:适配器模式(Default Adapter Pattern)或缺省适配器模式。 ...

  • iOS 适配黑暗模式

    适配黑暗模式 一直在UIViewController或者UIView中做实验traitCollectionDidC...

  • iOS 黑暗模式适配

    苹果在iOS13中为iPhone引入了深色模式,所有 UIKit 本身所提供的 UI 控件(例如 UIView,U...

  • flutter黑暗模式适配

    其实就是简单的一句话: themeMode: 当点击暗黑按钮直接修改isDark变量就好了,我使用的是mobx状态...

  • iOS 黑暗模式的适配

    黑暗模式的适配其实很简单, 下面就讲一种最简单的场景, 无需增加代码。首先创建颜色和图片。 这里的颜色和图片有两种...

  • 暗黑模式(黑暗模式,深色模式)适配

    暗黑模式(黑暗模式,深色模式)适配 全局关闭 打开Info.plist 项目配置文件,添加UIUserInterf...

  • iOS13适配总结

    1.PHImageFileSandboxExtensionTokenKey 2.如果不想适配黑暗模式,在info....

  • iOS 13 黑暗模式适配

    iOS 13 黑暗模式适配 官方文档 一、启用黑暗模式 1.1 强行不使用 不推荐,明年可能就要不过审核,临时用用...

网友评论

      本文标题:关于黑暗模式的一些适配

      本文链接:https://www.haomeiwen.com/subject/pdyquhtx.html