在android中更改可绘图项的颜色

bfnvny8b  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(368)

我已经做了一个函数来改变android中可绘制图形的颜色。问题是它只在60-70%的时间内有效。我在应用程序的每个活动的oncreate方法中调用它(颜色保存在共享首选项中)正如您在这张图像中看到的,背景可绘制的颜色没有改变,它工作得很好。但有时,在一个奇怪的活动中,只有一些可绘制图形的颜色发生变化,而其他图形则保留其默认颜色。这是函数的代码:

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);
    }
stszievb

stszievb1#

你可以试试这个吗?

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

vnjpjtjt2#

好吧,我发现了真正的问题。这是初学者的错误。我忘了在我选择颜色的地方调用我的函数。

相关问题