how to detect accessibility settings on android is enabled/disabled

前端 未结 5 1363
清歌不尽
清歌不尽 2021-01-28 12:34

I\'m particularly interested in high contrast text, color correction, and magnification settings. I did some research online, couldn\'t fi

5条回答
  •  自闭症患者
    2021-01-28 12:54

    Though reflection solution helps with some devices I was unable to get "isHighTextContrastEnabled" via reflection on my OnePlus 5 device. So I ended up with another method when reflection fails. Just rendering text on a bitmap and then manually checking if there is a specific color (different from black and white) in the bitmap (cause in "High Contrast" mode it will be black and white).

    Here is the code in Kotlin:

    private fun isHighTextContrastEnabledWithBmp(): Boolean {
        val bmp = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bmp)
        val testColor = Color.GREEN
    
        val paint = Paint().apply {
            color = testColor
            textSize = 5f
        }
    
        canvas.drawText("1", 0f, 10f, paint)
        val pixels = IntArray(bmp.width * bmp.height)
        bmp.getPixels(
            pixels, 0, bmp.width,
            0, 0, bmp.height, bmp.width
        )
        val result = pixels.any { it == testColor }.not()
    
        return result
    }
    

提交回复
热议问题