【问题标题】:Dynamically change SVG image color in android在android中动态改变SVG图像颜色
【发布时间】:2014-12-24 22:31:12
【问题描述】:

我知道使用第三方库,可以在 Android 中使用 SVG 图像。 图书馆喜欢:svg-android

加载SVG图像的代码如下:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Create a new ImageView
    ImageView imageView = new ImageView(this);
    // Set the background color to white
    imageView.setBackgroundColor(Color.WHITE);
    // Parse the SVG file from the resource
    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
    // Get a drawable from the parsed SVG and set it as the drawable for the ImageView
    imageView.setImageDrawable(svg.createPictureDrawable());
    // Set the ImageView as the content view for the Activity
    setContentView(imageView);
}

一切正常。我可以看到图像。但现在我想在运行时更改 svg 图像的颜色。 为此,我尝试了相同项目描述中提到的以下代码。

  // 0xFF9FBF3B is the hex code for the existing Android green, 0xFF1756c9 is the new blue color
    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android, 0xFF9FBF3B, 0xFF1756c9);

但是我看不到颜色的变化。所以我想知道如何在Java文件中动态改变颜色。

【问题讨论】:

  • 确保此颜色0xFF9FBF3B 存在于您的 svg 中(只需在文本编辑器中打开它并搜索此值)。请记住,颜色更改仅在加载文件时发生,而不是动态发生。因此,只需重新加载文件即可应用颜色更改。

标签: android svg imageview android-view svg-filters


【解决方案1】:

你可以使用:drawableTint来改变颜色

【讨论】:

    【解决方案2】:

    @Antlip Dev 所说的是正确的,但该方法现在已被弃用。 这是更新版本:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        drawable.setColorFilter(new BlendModeColorFilter(color, BlendMode.SRC_ATOP));
    } else {
       drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }
    

    【讨论】:

      【解决方案3】:

      Kotlin 中使用Antlip Dev 的答案。

      package com.example.... // Your package.
      
      import android.graphics.PorterDuff
      import android.widget.ImageView
      import androidx.annotation.ColorRes
      import androidx.core.content.ContextCompat
      
      fun ImageView.setSvgColor(@ColorRes color: Int) =
          setColorFilter(ContextCompat.getColor(context, color), PorterDuff.Mode.SRC_IN)
      

      用法:

      view.your_image.setSvgColor(R.color.gray)
      

      【讨论】:

      • 最好避免使用!!只要有可能,最好做一些类似 context?.let { setSvgColor(it, ... }
      • @crobicha,我同意你的看法。但是当你从viewcontext != null调用这些方法时。
      • @crobicha,感谢您的回答。我简化了方法。请查看更新。
      【解决方案4】:

      我知道这有点晚了,但我也遇到了这个问题,并且能够使用 setColorFilter(int color, PorterDuff.Mode mode) 方法解决这个问题。

      例子:

      imageView.setColorFilter(getResources().getColor(android.R.color.black), PorterDuff.Mode.SRC_IN);
      

      【讨论】:

        【解决方案5】:

        我知道问题出在哪里。 问题在于我在 svg 文件中使用的颜色代码。 它不完全是 0xFF9FBF3B 而是 #9FBF3B
        但是在 java 代码中,你必须用 ARGB 值(例如 0xFF9FBF3B)来编写它。 我已经更新了它,它现在工作正常。我可以使用相同的代码更改 svg 文件的颜色。

        希望这也有助于其他人在运行时更改 SVG 图像的颜色时识别实际情况。

        【讨论】:

        • 我无法在 imageview firebasestorage.googleapis.com/v0/b/… color 中更改此图像的颜色,您知道我该怎么做吗?
        • 您能否查看@Antlip Dev 的答案。那更准确。
        • 此外,我们在 XML 中为 Imageview 提供了 tintColor 选项。你也可以试试。
        • tint 选项只能更改具有 .xml 格式且我具有 .svg 格式 url 的图像
        • 您还可以动态更改SVG图像颜色。
        猜你喜欢
        • 2016-01-16
        • 2021-08-11
        • 2023-03-21
        • 2012-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-08
        • 2014-06-20
        相关资源
        最近更新 更多