【问题标题】:Android change color of ImageView / BitmapAndroid改变ImageView / Bitmap的颜色
【发布时间】:2012-05-14 01:29:45
【问题描述】:

我需要找到一种方法来更改 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>

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

那么任何人都可以提出更好的解决方案吗?

【问题讨论】:

    标签: android bitmap android-imageview


    【解决方案1】:

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

    1. src="@drawable/button" 声明ImageView
    2. 创建一个Drawable 并将ColorFilter 设置为它,然后将其用作您声明的ImageView 的源代码,如下所示:

    >

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

    【讨论】:

    • 如果可能的话,您能否在 xml 中提及如何执行此操作?
    • Nvm 我找到了,必须为imageview 使用android:tint 属性
    【解决方案2】:

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

    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);
    

    【讨论】:

      【解决方案3】:
      getResources().getDrawable( R.drawable.button );
      

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

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

      【讨论】:

        【解决方案4】:

        你应该这样吗?

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

        【讨论】:

          【解决方案5】:

          【讨论】:

          【解决方案6】:

          试试这个:

          private final ImageView uiIV_statusIcon;
          uiIV_statusIcon = anyView.findViewById(R.id.iv_status);
          uiIV_statusIcon.setImageResource(R.drawable.ic_twotone_error_24);
          

          在活动中:

          ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(getColor(R.color.md_red_400)));
          

          在一个片段中

          ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(getContext().getColor(R.color.md_red_400)));
          

          在带有预分配变量 _context 的 Recyclerviewadapter 中:

          ImageViewCompat.setImageTintList(uiIV_statusIcon, ColorStateList.valueOf(_context.getColor(R.color.md_red_400)));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多