在 Android 中更改可绘制对象的颜色

Changing colors of drawables in Android

我在 android 中创建了一个函数来更改可绘制对象的颜色。问题是它只有 60-70% 的时间有效。我在我的应用程序的每个 activity 的 onCreate 方法上调用它。 (颜色保存在共享首选项中) As you can see in this image the color of the background drawable did not change 它大部分工作正常。但有时在一个奇数 activity 中只有一些可绘制对象的颜色发生变化,而其他的则保留其默认颜色。 这是函数的代码:

 public static void changeTheme(Context context) {
    int color = Color.parseColor(AppUtils.getColorSharedPreference(context, AppConstants.customCOLOR));
    if (color != -1) {
        Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.ic_untitled_4);
        Drawable wrappedDrawable = null;
        if (unwrappedDrawable != null) {
            wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
        }
        Drawable unwrappedDrawableExit = AppCompatResources.getDrawable(context, R.drawable.logout_img);
        Drawable wrappedDrawableExit = null;
        if (unwrappedDrawableExit != null) {
            wrappedDrawableExit = DrawableCompat.wrap(unwrappedDrawableExit);
        }
        Drawable unwrappedDrawableFocus = AppCompatResources.getDrawable(context, R.drawable.focus_img);
        Drawable wrappedDrawableFocus = null;
        if (unwrappedDrawableFocus != null) {
            wrappedDrawableFocus = DrawableCompat.wrap(unwrappedDrawableFocus);
        }
        Drawable unwrappedDrawableSettings = AppCompatResources.getDrawable(context, R.drawable.settings_img);
        Drawable wrappedDrawableSettings = null;
        if (unwrappedDrawableSettings != null) {
            wrappedDrawableSettings = DrawableCompat.wrap(unwrappedDrawableSettings);
        }
        Drawable unwrappedDrawableHome = AppCompatResources.getDrawable(context, R.drawable.home_img);
        Drawable wrappedDrawableHome = null;
        if (unwrappedDrawableHome != null) {
            wrappedDrawableHome = DrawableCompat.wrap(unwrappedDrawableHome);
        }
        Drawable unwrappedDrawableBackArrow = AppCompatResources.getDrawable(context, R.drawable.arrow);
        Drawable wrappedDrawableBackArrow = null;
        if (unwrappedDrawableBackArrow != null) {
            wrappedDrawableBackArrow = DrawableCompat.wrap(unwrappedDrawableBackArrow);
        }
        Drawable unwrappedDrawableUnknownUser = AppCompatResources.getDrawable(context, R.drawable.unknown_user);
        Drawable wrappedDrawableUnknownUser = null;
        if (unwrappedDrawableUnknownUser != null) {
            wrappedDrawableUnknownUser = DrawableCompat.wrap(unwrappedDrawableUnknownUser);
        }
        Drawable unwrappedDrawableVerified = AppCompatResources.getDrawable(context, R.drawable.verified_user);
        Drawable wrappedDrawableVerified = null;
        if (unwrappedDrawableVerified != null) {
            wrappedDrawableVerified = DrawableCompat.wrap(unwrappedDrawableVerified);
        }
        Drawable unwrappedDrawableNoFace = AppCompatResources.getDrawable(context, R.drawable.no_face_detected);
        Drawable wrappedDrawableNoFace = null;
        if (unwrappedDrawableNoFace != null) {
            wrappedDrawableNoFace = DrawableCompat.wrap(unwrappedDrawableNoFace);
        }
        Drawable unwrappedDrawableWarning = AppCompatResources.getDrawable(context, R.drawable.warning);
        Drawable wrappedDrawableWarning = null;
        if (unwrappedDrawableWarning != null) {
            wrappedDrawableWarning = DrawableCompat.wrap(unwrappedDrawableWarning);
        }
        Drawable unwrappedDrawableMessage = AppCompatResources.getDrawable(context, R.drawable.message_new);
        Drawable wrappedDrawableMessage = null;
        if (unwrappedDrawableMessage != null) {
            wrappedDrawableMessage = DrawableCompat.wrap(unwrappedDrawableMessage);
        }
        DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableBackArrow, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableExit, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableHome, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableSettings, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableFocus, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableUnknownUser, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableVerified, ColorStateList.valueOf(color));
        DrawableCompat.setTintList(wrappedDrawableNoFace, ColorStateList.valueOf(color));
        if (wrappedDrawableWarning != null) {
            DrawableCompat.setTintList(wrappedDrawableWarning, ColorStateList.valueOf(color));
        }
        if (wrappedDrawableMessage != null) {
            DrawableCompat.setTintList(wrappedDrawableMessage, ColorStateList.valueOf(color));
        }

    }
}

这是一个示例调用:

 if(Color.parseColor(AppUtils.getColorSharedPreference(LoginActivity.this,AppConstants.customCOLOR))!=-1)
    {
        AppUtils.changeTheme(LoginActivity.this);
    }

你可以试试这个吗?

Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable); 
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);  

好的,我发现了真正的问题。 此方法需要在 super.onCreate();

之前调用