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
Android change color of ImageView / Bitmap
提问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 int
value. I need something like if myValue=5
than change my image's color to RED
and if myValue=322
change 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 myValue
is changing to set my ImageView
image 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 :
这就是我解决这个问题的方法:
- Declare an
ImageView
withsrc="@drawable/button"
- Create a
Drawable
and setColorFilter
to it and after that use it as src to your declaredImageView
like this :
- 声明一个
ImageView
与src="@drawable/button"
- 创建一个
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
You can use a TransitionDrawable to achieve this - http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html
您可以使用 TransitionDrawable 来实现这一点 - http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html