Android更改ImageView/Bitmap的颜色 [英] Android change color of ImageView / Bitmap

查看:257
本文介绍了Android更改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 :

  1. src="@drawable/button"声明ImageView
  2. 创建一个Drawable并将其设置为ColorFilter,然后将其用作声明的ImageView的src,如下所示:
  1. Declare an ImageView with src="@drawable/button"
  2. Create a Drawable and set ColorFilter to it and after that use it as src to your declared ImageView 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆