【问题标题】:Changing Color of Image programmatically Android以编程方式更改图像的颜色 Android
【发布时间】:2017-09-07 23:24:45
【问题描述】:

我知道那里有几个答案,但我尝试了它们但没有成功。我有以前工作的代码,当使用color.xml 文件中的预定义资源颜色加载时,它会更改图标的颜色。对于不同的口味,我有不同的color.xml 文件。我知道color.xml 文件正在被拾取并使用,因为其他颜色正在成功使用。正如我所说,这是以前在 Eclipse 中工作并使用 Ant 构建的。但是我已经升级了一些库,所以我怀疑其中一个可能发生了变化,导致了这个问题。更奇怪的是,它在调试中用完 Android Studio 时有效,但在我使用 Gradle 构建时无效。

这是之前工作的代码(怀疑 setColorFilter 是罪魁祸首?):

ImageButton btnYes = new ImageButton(mContext);
btnYes.setPadding(0, 15, 15, 15);
btnYes.setTag(1);
Drawable yesDrawable = Utilities.getAndroidDrawable("form_ratingyes", mContext);
btnYes.setImageDrawable(yesDrawable);
btnYes.setFocusableInTouchMode(false);
btnYes.setBackgroundColor(Color.WHITE);
btnYes.setColorFilter(ContextCompat.getColor(mContext,R.color.rating_off));

color.xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Bullet Icon Color for (Forms, In Progress, Sync and Submitted Status)-->
    <color name="bullet">#0378AF</color>

    <!-- Navigation Bar Color-->
    <color name="navigation_bar">#F68A33</color>

    <!-- Menu Icon Color-->
    <color name="menu_tint">#F68A33</color> 
    <color name="form_icons">#F68A33</color>

    <!-- Rating Field Button Color -->
    <color name="rating_on">#F68A33</color>
    <color name="rating_off">#A2AAAD</color>

    <!-- Action Button Color -->
    <color name="login_button_default">#F68A33</color>
    <color name="login_button_selected">#B0540B</color>
</resources>

图像看起来像这样:

基本上是一个带有透明“Y”和外圆的白色圆圈。这个想法是白色将被颜色“rating_on”替换。

正常运行时应该是这样的:

如果怀疑是 gradle 文件,我可以添加它,但 Android Studio 不也使用它来构建调试版本吗?

我尝试了许多不同的方法来加载图标颜色,但都没有成功。我还尝试在调用setColorFilter 之前添加.getDrawable()。遗憾的是,它很难调试,因为在 Android Studio 中它可以工作 - 只有当我生成实际的 APK 时它才会失败。

【问题讨论】:

    标签: android android-studio gradle colorfilter


    【解决方案1】:

    答案是上面的代码没问题。问题出在Utilities.getAndroidDrawable("form_ratingyes", mContext); 调用的内部,它看起来像这样:

    public static Drawable getAndroidDrawable(String drawableName, Context context){
        int resourceId= context.getResources().getIdentifier(drawableName, "drawable", imagepackage);
        if(resourceId==0){
            return null;
        } else {
            return ContextCompat.getDrawable(context, resourceId);
        }
    

    图像包是:

    public final static String imagePackage = "com.mycompany.myapp";
    

    如此明显,但我从未深入研究过这种方法。真丢人。 不知何故,在旧的 Ant 构建中,它包含了旧包名(硬编码)下的所有图像 - 而不是新包名。是的,调试起来很有趣(不是我的代码,所以不知道它在做什么)。

    只需要动态调用包:

    public static Drawable getAndroidDrawable(String drawableName, Context context){
        int resourceId= context.getResources().getIdentifier(drawableName, "drawable", context.getPackageName());
        if(resourceId==0){
            return null;
        } else {
            return ContextCompat.getDrawable(context, resourceId);
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2011-01-16
      • 2015-04-02
      • 1970-01-01
      • 2015-12-06
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多