Android更改ImageView/Bitmap的颜色 [英] Android change color of ImageView / Bitmap
问题描述
我需要找到一种方法来更改Android中位图的颜色.我需要根据int
值在我的应用程序中平稳地替换/更改椭圆形图像的颜色.我需要类似myValue=5
的内容,而不是将图像的颜色更改为RED
,如果myValue=322
的颜色更改为BLUE
.我发现可以做到这一点的唯一方法是使用看起来像这样的xml文件:
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 :
<?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>
,然后更改myValue
以设置我的ImageView
图像资源.但是以这种方式,我必须创建35个不同的xml文件...我认为这不是一个好主意.
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.
那么有人谁可以提出更好的解决方案来做到这一点?
So anyone who can suggest better solution to do this?
推荐答案
这是我解决此问题的方法:
This is how I solved this issue :
- 用
src="@drawable/button"
声明ImageView
- 创建一个
Drawable
并将其设置为ColorFilter
,然后将其用作声明的ImageView
的src,如下所示:
- 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 :
>
Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLUE, Color.BLUE );
myIcon.setColorFilter(filter);
color.setImageDrawable(myIcon);
这篇关于Android更改ImageView/Bitmap的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!