如何在android中改变可绘制形状的颜色

时间:2022-11-21 12:11:06

I am developing small android application in which I set drawable resource as background for linear layout. Now what I want to do change background color of linear layout dynamically, but within drawable resource. My code looks like :

我正在开发小型Android应用程序,其中我将drawable资源设置为线性布局的背景。现在,我想要做的是动态地改变线性布局的背景颜色,但是在可绘制资源中。我的代码看起来像:

//  bcd.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item>        
    <shape>
        <gradient
            android:endColor="#22000000"
            android:startColor="#22000000"
            android:angle="270" />
        <stroke
            android:width="3dp"
            android:color="@color/white" />
        <corners
            android:radius="3dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
</item>

<LinearLayout 
android:id="@+id/lin_llt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

and I set background for linear layout in my activity like this...

我在我的活动中设置了线性布局的背景,如下所示......

parentLayout = (LinearLayout) view.findViewById(R.id.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);

Now what I want to do i want to change color of my drawable resource that mean change color of my linear layout with rounded corner and padding define in drawable..

现在我想做什么我想改变我的可绘制资源的颜色,这意味着改变我的线性布局的颜色,圆角和填充定义在drawable ..

I tried this in following way

我按照以下方式尝试了这个

ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground();
bgShape.getPaint().setColor(Color.BLACK);

but its not working for me. any other solution .

但它不适合我。任何其他解决方案

So how to do it... Need help... thank you...

那怎么办......需要帮助......谢谢......

5 个解决方案

#1


26  

Change the layout color dynamically

动态更改布局颜色

LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));

Dynamically set the background color gradient

动态设置背景颜色渐变

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

#2


10  

You could try something like this :

你可以尝试这样的事情:

Drawable sampleDrawable = context.getResources().getDrawable(R.drawable.balloons); 
sampleDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

and for more you could refer to :

而且你可以参考:

How to change colors of a Drawable in Android?

如何在Android中更改Drawable的颜色?

Change drawable color programmatically

以编程方式更改可绘制颜色

Android: Change Shape Color in runtime

Android:在运行时更改形状颜色

http://pastebin.com/Hd2aU4XC

You could also try this :

你也可以试试这个:

private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_colors);

    ImageView iv = (ImageView) findViewById(R.id.img);
    Drawable d = getResources().getDrawable(RES);
    iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
    int to = Color.RED;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y))) 
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
    //There may be a better way to match, but I wanted to do a comparison ignoring
    //transparency, so I couldn't just do a direct integer compare.
    return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD && Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD && Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}

as given in How to change colors of a Drawable in Android?

如何在Android中更改Drawable的颜色?

#3


2  

use this..

<solid android:color="#e1e1e1" />

<stroke
    android:width="2dp"
    android:color="#808080" />

<corners android:radius="10dp" />

<padding
    android:bottom="5dp"
    android:left="5dp"
    android:right="5dp"
    android:top="5dp" />

#4


1  

One approach would be to create a second drawable XML with the 2nd color and then change the background of the layout with the 2nd drawable.

一种方法是使用第二种颜色创建第二个可绘制的XML,然后使用第二个drawable更改布局的背景。

#5


0  

The following works great for setting the color of the drawable programmatically without changing its shape:

以下工作非常适合以编程方式设置可绘制的颜色而不更改其形状:

parentLayout.getBackground().setColorFilter(
    Color.BLACK,
    PorterDuff.Mode.SRC_ATOP
);

#1


26  

Change the layout color dynamically

动态更改布局颜色

LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));

Dynamically set the background color gradient

动态设置背景颜色渐变

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

#2


10  

You could try something like this :

你可以尝试这样的事情:

Drawable sampleDrawable = context.getResources().getDrawable(R.drawable.balloons); 
sampleDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

and for more you could refer to :

而且你可以参考:

How to change colors of a Drawable in Android?

如何在Android中更改Drawable的颜色?

Change drawable color programmatically

以编程方式更改可绘制颜色

Android: Change Shape Color in runtime

Android:在运行时更改形状颜色

http://pastebin.com/Hd2aU4XC

You could also try this :

你也可以试试这个:

private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_colors);

    ImageView iv = (ImageView) findViewById(R.id.img);
    Drawable d = getResources().getDrawable(RES);
    iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
    int to = Color.RED;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y))) 
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
    //There may be a better way to match, but I wanted to do a comparison ignoring
    //transparency, so I couldn't just do a direct integer compare.
    return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD && Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD && Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}

as given in How to change colors of a Drawable in Android?

如何在Android中更改Drawable的颜色?

#3


2  

use this..

<solid android:color="#e1e1e1" />

<stroke
    android:width="2dp"
    android:color="#808080" />

<corners android:radius="10dp" />

<padding
    android:bottom="5dp"
    android:left="5dp"
    android:right="5dp"
    android:top="5dp" />

#4


1  

One approach would be to create a second drawable XML with the 2nd color and then change the background of the layout with the 2nd drawable.

一种方法是使用第二种颜色创建第二个可绘制的XML,然后使用第二个drawable更改布局的背景。

#5


0  

The following works great for setting the color of the drawable programmatically without changing its shape:

以下工作非常适合以编程方式设置可绘制的颜色而不更改其形状:

parentLayout.getBackground().setColorFilter(
    Color.BLACK,
    PorterDuff.Mode.SRC_ATOP
);