android - 以编程方式更改图像的颜色Android

标签 android android-studio gradle colorfilter

我知道那里有几个答案,但是我没有尝试过。我有以前使用过的代码,当使用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”颜色。

enter image description here

正确运行时,它应如下所示:

enter image description here

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

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

最佳答案

答案是上面的代码很好。问题出在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);
    }

关于android - 以编程方式更改图像的颜色Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46106468/

相关文章:

android - 当我在 android studio 中运行我的程序时出现错误 "unfortunately, my application has stopped"

Android studio - 更改 .gradle 文件夹位置

Android NDK 构建

android - 本项目使用AndroidX依赖,但 'android.useAndroidX'属性未启用

Android:每个线程有多个处理程序可以吗

android - 在 Android 2.2 上使用 FTS3 的 SQLite 不正确的 RowId

android - Google Play 热门应用是如何评估的?

android - 内部使用 intent.getextras()

java - gdx-setup.jar 中没有主要 list 属性

flutter - 在flutter中同步gradle文件时收到java.lang.StackOverflowError