如何在android中动态更改图像颜色?

时间:2022-11-21 15:05:13

I am doing such type of project ,In my project change Image color dynamically.

我正在做这种类型的项目,在我的项目中动态更改图像颜色。

I have a one black shape color image ,when user click on this image change image color dynamically green.

我有一个黑色的彩色图像,当用户点击这个图像时动态变绿图像颜色。

如何在android中动态更改图像颜色?

Googling and other document follow but I am not solve my problem .

谷歌搜索和其他文件,但我没有解决我的问题。

Please help me , is there any method or document to follow solve my problem ,

请帮帮我,是否有任何方法或文件可以解决我的问题,

6 个解决方案

#1


45  

Here's how I do this: It's pulling the color from a resource xml file.

这是我如何做到这一点:它从资源xml文件中提取颜色。

<resources>
<color name="new_color">#FFAAAAAA</color>
</resources>

In your activity .java file:

在您的活动.java文件中:

import android.graphics.PorterDuff.Mode;

Resources res = context.getResources();
final ImageView image = (ImageView) findViewById(R.id.imageId);
final int newColor = res.getColor(R.color.new_color);
image.setColorFilter(newColor, Mode.SRC_ATOP);

To clear it call:

要清除它,请致电:

image.setColorFilter(null);

#2


7  

imageView.setImageResource(R.drawable.ic_person_black_48dp);

imageView.setColorFilter(imageView.getContext().getResources().getColor(R.color.desired_color), PorterDuff.Mode.SRC_ATOP);

#3


5  

Set android:tint attribute of image/image button to the color you need.

将image / image按钮的android:tint属性设置为您需要的颜色。

android:tint="@android:color/black"

Optionally you can set android:tintMode attribute.

您可以选择设置android:tintMode属性。

#4


4  

Create resource in drawable folder like `

在`drawable文件夹中创建资源

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/rect"
            android:tint="@color/red"/>
    </item>
</layer-list>

`

android:tint="@color/red" do it.

android:tint =“@ color / red”做到了。

#5


0  

Put this in your OnDraw, just before you draw your square.

在绘制正方形之前,将它放在OnDraw中。

if (userclicked){
paint.setColor(Color.GREEN);
} else {
paint.setColor(Color.BLACK);
}

Of course that is if you are drawing it with canvas.drawRect(x0,y0,x1,y1,paint) which you would if you were drawing a simple shape like that.

当然,如果你用canvas.drawRect(x0,y0,x1,y1,paint)绘制它,如果你正在绘制一个这样的简单形状,你会想到它。

#6


0  

In XML use src not background tag in ImageView. In java code-

在XML中使用src而不是ImageView中的背景标记。在java代码中 -

import android.graphics.PorterDuff.Mode;
final Context context=this;

        home1=(ImageView) findViewById(R.id.home1);
 Resources res = context1.getResources();
        final int newColor = res.getColor(android.R.color.black);
        home1.setColorFilter(newColor, Mode.SRC_ATOP);

#1


45  

Here's how I do this: It's pulling the color from a resource xml file.

这是我如何做到这一点:它从资源xml文件中提取颜色。

<resources>
<color name="new_color">#FFAAAAAA</color>
</resources>

In your activity .java file:

在您的活动.java文件中:

import android.graphics.PorterDuff.Mode;

Resources res = context.getResources();
final ImageView image = (ImageView) findViewById(R.id.imageId);
final int newColor = res.getColor(R.color.new_color);
image.setColorFilter(newColor, Mode.SRC_ATOP);

To clear it call:

要清除它,请致电:

image.setColorFilter(null);

#2


7  

imageView.setImageResource(R.drawable.ic_person_black_48dp);

imageView.setColorFilter(imageView.getContext().getResources().getColor(R.color.desired_color), PorterDuff.Mode.SRC_ATOP);

#3


5  

Set android:tint attribute of image/image button to the color you need.

将image / image按钮的android:tint属性设置为您需要的颜色。

android:tint="@android:color/black"

Optionally you can set android:tintMode attribute.

您可以选择设置android:tintMode属性。

#4


4  

Create resource in drawable folder like `

在`drawable文件夹中创建资源

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:src="@drawable/rect"
            android:tint="@color/red"/>
    </item>
</layer-list>

`

android:tint="@color/red" do it.

android:tint =“@ color / red”做到了。

#5


0  

Put this in your OnDraw, just before you draw your square.

在绘制正方形之前,将它放在OnDraw中。

if (userclicked){
paint.setColor(Color.GREEN);
} else {
paint.setColor(Color.BLACK);
}

Of course that is if you are drawing it with canvas.drawRect(x0,y0,x1,y1,paint) which you would if you were drawing a simple shape like that.

当然,如果你用canvas.drawRect(x0,y0,x1,y1,paint)绘制它,如果你正在绘制一个这样的简单形状,你会想到它。

#6


0  

In XML use src not background tag in ImageView. In java code-

在XML中使用src而不是ImageView中的背景标记。在java代码中 -

import android.graphics.PorterDuff.Mode;
final Context context=this;

        home1=(ImageView) findViewById(R.id.home1);
 Resources res = context1.getResources();
        final int newColor = res.getColor(android.R.color.black);
        home1.setColorFilter(newColor, Mode.SRC_ATOP);