【问题标题】:Drawable => grayscale可绘制 => 灰度
【发布时间】:2010-12-20 02:00:32
【问题描述】:

将颜色 Drawable 转换为灰度(表示禁用状态)的正确方法是什么?

编辑:
黑白 => 灰度

【问题讨论】:

    标签: android drawable


    【解决方案1】:

    显然,您可以使用ColorMatrix 类进行任何类型的色彩空间转换。它有一个setSaturation() 方法,可以轻松地为您创建颜色到灰度的转换(零饱和度)。

    因此,您可以使用该滤镜绘制图像的新副本。我还没有尝试过,但它应该可以工作:

    Bitmap grayscaleBitmap = Bitmap.createBitmap(
        colorBitmap.getWidth(), colorBitmap.getHeight(),
        Bitmap.Config.RGB_565);
    
    Canvas c = new Canvas(grayscaleBitmap);
    Paint p = new Paint();
    ColorMatrix cm = new ColorMatrix();
    
    cm.setSaturation(0);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm);
    p.setColorFilter(filter); 
    c.drawBitmap(colorBitmap, 0, 0, p);
    

    【讨论】:

      【解决方案2】:

      您是否特别想以编程方式执行此操作,而不仅仅是使用禁用版本的图像?您可以引用 XML 可绘制对象,例如:

      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_window_focused="false" android:state_enabled="true"
              android:drawable="@drawable/btn_default_normal" />
          <item android:state_window_focused="false" android:state_enabled="false"
              android:drawable="@drawable/btn_default_normal_disable" />
          <item android:state_pressed="true" 
              android:drawable="@drawable/btn_default_pressed" />
          <item android:state_focused="true" android:state_enabled="true"
              android:drawable="@drawable/btn_default_selected" />
          <item android:state_enabled="true"
              android:drawable="@drawable/btn_default_normal" />
          <item android:state_focused="true"
              android:drawable="@drawable/btn_default_normal_disable_focused" />
          <item
               android:drawable="@drawable/btn_default_normal_disable" />
      </selector>
      

      【讨论】:

      • 是的,我想在每个实例的基础上进行。不过感谢您的 sn-p。
      【解决方案3】:

      对@intgr 答案的一些评论。
      1.Bitmap.Config.ARGB_8888保留alpha通道。
      2. 一点额外的代码:

      //remember, you are converting a .png image, as opposed to a Drawable defined in .xml  
      Bitmap colorBitmap = ((BitmapDrawable)drawable).getBitmap();    
      // the code by intgr  
      Drawable grayscaleDrawable = new BitmapDrawable(grayscaleBitmap);
      

      【讨论】:

      • 小错字:Bitmap.Config.RGB_8888 应该是 Bitmap.Config.ARGB_8888。那么工作正常,谢谢。
      【解决方案4】:

      我知道这个问题是不久前提出的,但我遇到了一个更简单的解决方案,如果你有一个 Drawable 并且你只想以灰度显示相同的 drawable。不需要画布或画家......

      protected Drawable convertToGrayscale(Drawable drawable)
      {
          ColorMatrix matrix = new ColorMatrix();
          matrix.setSaturation(0);
      
          ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
      
          drawable.setColorFilter(filter);
      
          return drawable;
      }
      

      希望这也有帮助!

      【讨论】:

      • 好提示!有趣的是,如果您通过传递 ContextCompat.getDrawable(context, R.drawable.myId); 运行此方法;下次您需要获得相同的可绘制对象时,已经设置了颜色过滤器。就我而言,我需要移除颜色过滤器以使颜色恢复到我的可绘制对象上:)
      • 在你的 drawable 上应用颜色过滤器之前 mutate() 。可绘制属性在实例之间共享,因此通过对其进行变异,您正在创建该可绘制对象的另一个“副本”。在变异后使用颜色过滤器将是安全的,并且不会影响您的其他可绘制实例。
      猜你喜欢
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2015-04-22
      • 1970-01-01
      • 2023-03-03
      • 2012-05-26
      相关资源
      最近更新 更多