Android 更改 ImageView / Bitmap 的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10449917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:47:50  来源:igfitidea点击:

Android change color of ImageView / Bitmap

androidbitmapandroid-imageview

提问by Android-Droid

I need to find a way to change the color of bitmap in Android. I need to replace/change colors of oval image smoothly in my application depending on intvalue. I need something like if myValue=5than change my image's color to REDand if myValue=322change color to BLUE. The only way which I find I can do this was using xml file which looks like this :

我需要找到一种方法来更改 Android 中位图的颜色。我需要根据int值在我的应用程序中平滑地替换/更改椭圆图像的颜色。我需要一些类似的东西,如果myValue=5不是将我的图像颜色更改为RED,如果myValue=322将颜色更改为BLUE. 我发现我可以做到这一点的唯一方法是使用如下所示的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
 <solid android:color="#cccccc"/> 
    <corners
     android:bottomRightRadius="10dp"
     android:bottomLeftRadius="10dp"
     android:topLeftRadius="10dp"
     android:topRightRadius="10dp"/>
</shape>

and after that when myValueis changing to set my ImageViewimage resource. But in this way I have to create 35 different xml files...which I don't think is a good idea.

之后什么时候myValue更改以设置我的ImageView图像资源。但是这样我必须创建 35 个不同的 xml 文件……我认为这不是一个好主意。

So anyone who can suggest better solution to do this?

那么任何人都可以提出更好的解决方案来做到这一点?

回答by Android-Droid

This is how I solved this issue :

这就是我解决这个问题的方法:

  1. Declare an ImageViewwith src="@drawable/button"
  2. Create a Drawableand set ColorFilterto it and after that use it as src to your declared ImageViewlike this :
  1. 声明一个ImageViewsrc="@drawable/button"
  2. 创建一个Drawable并设置ColorFilter为它,然后将其用作 src 到您的声明中,ImageView如下所示:

>

>

Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
myIcon.setColorFilter(filter);
color.setImageDrawable(myIcon);

回答by Marylia Gutierrez

This solution doesn't work very well for me. In some images the final color was wrong. I use this solution instead:

这个解决方案对我来说不是很好。在某些图像中,最终颜色是错误的。我改用这个解决方案:

Drawable myIcon = getResources().getDrawable(R.drawable.your_image); 
myIcon.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP); 
((ImageView)findViewById(R.id.view_to_change)).setImageDrawable(myIcon);

回答by Himadri Pant

getResources().getDrawable( R.drawable.button );

is now deprecated. Can also do it this way:

现在已弃用。也可以这样做:

((ImageView) findViewById(R.id.my_icon))
  .setColorFilter(new LightingColorFilter(Color.BLUE, Color.BLUE));

回答by hoangtu23

Should you this.

你该不该这样。

Drawable myIcon = getResources().getDrawable( R.drawable.button ); 
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);

回答by asenovm