[英]Android change color of ImageView / Bitmap
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.我需要找到一种方法来更改 Android 中 bitmap 的颜色。我需要根据int
值在我的应用程序中顺利地替换/更改椭圆图像的 colors。 I need something like if myValue=5
than change my image's color to RED
and if myValue=322
change color to BLUE
.我需要类似 if myValue=5
的东西,而不是将图像的颜色更改为RED
,如果myValue=322
将颜色更改为BLUE
。 The only way which I find I can do this was using xml file which looks like this:我发现我能做到这一点的唯一方法是使用 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.之后,当myValue
更改为设置我的ImageView
图像资源时。 But in this way I have to create 35 different xml files...which I don't think is a good idea.但是通过这种方式我必须创建 35 个不同的 xml 文件......我认为这不是一个好主意。
So anyone who can suggest better solution to do this?那么有人可以建议更好的解决方案吗?
This is how I solved this issue : 这就是我解决这个问题的方法:
ImageView
with src="@drawable/button"
用src="@drawable/button"
声明一个ImageView
Drawable
and set ColorFilter
to it and after that use it as src to your declared ImageView
like this : 创建一个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);
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);
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));
Should you this. 你应该这样吗?
Drawable myIcon = getResources().getDrawable( R.drawable.button );
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);
您可以使用TransitionDrawable来实现此目的 - http://developer.android.com/reference/android/graphics/drawable/TransitionDrawable.html
Try this:尝试这个:
private final ImageView uiIV_statusIcon;
uiIV_statusIcon = anyView.findViewById(R.id.iv_status);
uiIV_statusIcon.setImageResource(R.drawable.ic_twotone_error_24);
in Activity:在活动中:
ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(getColor(R.color.md_red_400)));
in a Fragment在片段中
ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(getContext().getColor(R.color.md_red_400)));
in a Recyclerviewadapter with preassigned variable _context:在具有预分配变量 _context 的 Recyclerviewadapter 中:
ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(_context.getColor(R.color.md_red_400)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.