以编程方式设置drawable中的形状颜色

时间:2022-11-21 12:43:47

I'm adding an item (with an icon) to my menu like this:

我正在我的菜单中添加一个项目(带有图标),如下所示:

subMenu.add(user.getName()).setIcon(R.drawable.user_bg);

The user_bg drawable layout looks like this:

user_bg drawable布局如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/user_color">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval">
            <solid
                android:color="#000000"
                />
            <size
                android:width="100dp"
                android:height="100dp"
                />
        </shape>
    </item>

    <item
        android:drawable="@drawable/user"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        />

</layer-list>

How can I programmatically change the color of the drawable (#000000) after adding the new item?

如何在添加新项目后以编程方式更改drawable(#000000)的颜色?

3 个解决方案

#1


0  

Create the drawable programmatically, set its color, then set it on the menu. Something like:

以编程方式创建可绘制,设置其颜色,然后在菜单上设置它。就像是:

Drawable icon = getResources().getDrawable(R.drawable.icon);
if (icon instanceOf ShapeDrawable) {
  icon.getPaint().setColor(getResources().getColor(R.color.some_color));
  subMenu.setIcon(icon);
}

#2


0  

int red = 102, green = 34, blue = 100;
drawable.setColorFilter(red,Mode.ADD);
drawable.setColorFilter(green,Mode.ADD);
drawable.setColorFilter(blue,Mode.ADD);

NOTE: This will remove the Tint if there any, in your drawable. Check this question as well, to get more info abot setColorFilter() method

注意:这将删除您的drawable中的Tint(如果有的话)。也请检查此问题,以获取更多信息abot setColorFilter()方法

#3


0  

I don't know if this is the best way to do it, but it works.

我不知道这是否是最好的方法,但它确实有效。

  1. Obtain a reference to the drawable

    获得对drawable的引用

    If you want to setup your drawable's color and then add the subItem

    如果要设置drawable的颜色,然后添加subItem

    LayerDrawable drawable = (LayerDrawable)getResources().getDrawable(R.drawable.user_bg);

    LayerDrawable drawable =(LayerDrawable)getResources()。getDrawable(R.drawable.user_bg);

    Or

    If you want to change the color after you have already added the item

    如果要在添加项目后更改颜色

    LayerDrawable drawable = (LayerDrawable)subMenu.getItem().getIcon()

    LayerDrawable drawable =(LayerDrawable)subMenu.getItem()。getIcon()

  2. Call Drawable.Mutate()

    drawable .Mutate();

    For explanation, refer to item #2 of this answer

    有关说明,请参阅本答案的第2项

  3. Create a color drawable

    创建一个颜色drawable

    ColorDrawable newColor = new ColorDrawable(Color.parseColor("#000000"));

    ColorDrawable newColor = new ColorDrawable(Color.parseColor(“#000000”));

  4. Set/reset the color

    设置/重置颜色

    drawable.setDrawableByLayerId(R.id.user_color, newColor);

  5. Redraw drawable

    drawable.invalidateSelf();

  6. Redraw drawable drawable.invalidateSelf();

#1


0  

Create the drawable programmatically, set its color, then set it on the menu. Something like:

以编程方式创建可绘制,设置其颜色,然后在菜单上设置它。就像是:

Drawable icon = getResources().getDrawable(R.drawable.icon);
if (icon instanceOf ShapeDrawable) {
  icon.getPaint().setColor(getResources().getColor(R.color.some_color));
  subMenu.setIcon(icon);
}

#2


0  

int red = 102, green = 34, blue = 100;
drawable.setColorFilter(red,Mode.ADD);
drawable.setColorFilter(green,Mode.ADD);
drawable.setColorFilter(blue,Mode.ADD);

NOTE: This will remove the Tint if there any, in your drawable. Check this question as well, to get more info abot setColorFilter() method

注意:这将删除您的drawable中的Tint(如果有的话)。也请检查此问题,以获取更多信息abot setColorFilter()方法

#3


0  

I don't know if this is the best way to do it, but it works.

我不知道这是否是最好的方法,但它确实有效。

  1. Obtain a reference to the drawable

    获得对drawable的引用

    If you want to setup your drawable's color and then add the subItem

    如果要设置drawable的颜色,然后添加subItem

    LayerDrawable drawable = (LayerDrawable)getResources().getDrawable(R.drawable.user_bg);

    LayerDrawable drawable =(LayerDrawable)getResources()。getDrawable(R.drawable.user_bg);

    Or

    If you want to change the color after you have already added the item

    如果要在添加项目后更改颜色

    LayerDrawable drawable = (LayerDrawable)subMenu.getItem().getIcon()

    LayerDrawable drawable =(LayerDrawable)subMenu.getItem()。getIcon()

  2. Call Drawable.Mutate()

    drawable .Mutate();

    For explanation, refer to item #2 of this answer

    有关说明,请参阅本答案的第2项

  3. Create a color drawable

    创建一个颜色drawable

    ColorDrawable newColor = new ColorDrawable(Color.parseColor("#000000"));

    ColorDrawable newColor = new ColorDrawable(Color.parseColor(“#000000”));

  4. Set/reset the color

    设置/重置颜色

    drawable.setDrawableByLayerId(R.id.user_color, newColor);

  5. Redraw drawable

    drawable.invalidateSelf();

  6. Redraw drawable drawable.invalidateSelf();