【问题标题】:Android Dark Theme does not affect SVG imagesAndroid Dark Theme 不影响 SVG 图像
【发布时间】:2021-03-15 22:40:32
【问题描述】:

我当前的 Android 应用程序正在使用以下样式来尝试启用深色主题

<resources>
    <style name="AppTheme" parent="@style/Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorError">@color/design_default_color_error</item>
        <item name="android:textColor">?attr/colorOnBackground</item>
    </style>

    <style name="splashDialog" parent="Theme.AppCompat.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@null</item>
    </style>
</resources>

除了这个 SVG 图像在浅色和深色主题中似乎相同之外,UI 中的所有内容都可以正常工作

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="?attr/colorControlNormal"
    android:viewportWidth="24.0"
    android:viewportHeight="24.0">
    <path
        android:fillColor="@android:color/white"
        android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zM18,16v-5c0,-3.07 -1.63,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.64,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2zM16,17L8,17v-6c0,-2.48 1.51,-4.5 4,-4.5s4,2.02 4,4.5v6z" />
</vector>

如何在深色模式下实现“较亮”图像的预期效果?

【问题讨论】:

    标签: android android-vectordrawable android-dark-theme


    【解决方案1】:

    在我的项目中,我对可绘制对象使用了负过滤器,如下所示:

    private val negative = floatArrayOf(
        -1.0f,     .0f,     .0f,    .0f, 255.0f, // red
        .0f,     -1.0f,     .0f,    .0f, 255.0f, // green
        .0f,       .0f,   -1.0f,    .0f, 255.0f, // blue
        .0f,       .0f,     .0f,   1.0f,   .0f  // alpha
    )
    
    fun Drawable.toNegative() {
        this.colorFilter = ColorMatrixColorFilter(negative)
    }
    

    【讨论】:

      【解决方案2】:

      您可以应用灰度作为饱和度,并根据需要在图像视图对象上降低 alpha

      灰度图像视图:

      imageView.colorFilter =
                      ColorMatrixColorFilter(ColorMatrix().apply {
                          setSaturation(0f)
                      })
      

      降低 alpha:

      imageView.alpha = 0.5F
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-07
        • 2021-05-21
        • 2018-10-13
        • 2022-11-20
        相关资源
        最近更新 更多