【问题标题】:Drawable loses color filter after converting into bitmapDrawable 转换为位图后会丢失颜色过滤器
【发布时间】:2016-01-22 02:45:09
【问题描述】:

我正在尝试在可绘制对象中添加颜色过滤器,然后将其转换为位图。问题是当将drawable转换为位图时它会丢失它的颜色过滤器。我在imageview中使用drawable并且它有颜色过滤器但是在imageview中使用位图没有任何颜色效果。为什么会这样?提前致谢。

【问题讨论】:

  • 你真正想要做什么?你是否试图将 Drawable 转换回位图。向我们展示您的代码。
  • 我想给drawable添加一个颜色过滤器并将其转换为位图。
  • 你得到解决方案了吗?

标签: android bitmap drawable colorfilter


【解决方案1】:

使用画布将 Drawable 重新粘贴到位图上:

    Canvas canvas;
    Drawable drawable = <yourDrawable created from wherever>;
    Bitmap bmp = <your Bitmap which is the same width/height as the drawable>

    // blit the drawable onto the bitmap using a Canvas
    canvas = new Canvas(bmp);
    drawable.draw(canvas);

http://developer.android.com/guide/topics/graphics/2d-graphics.html

【讨论】:

  • 使用了这个代码,但结果不是预期的。我需要在drawable中应用颜色过滤器的位图。
【解决方案2】:

我遇到了同样的问题,我终于解决了!

我们需要做以下事情来获取应用了颜色过滤器的位图。

image_view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(image_view.getDrawingCache());

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,终于找到了解决方案。 Drawable 可以有多个状态,因此您可能会绘制错误的状态。

    你应该在绘图前将它切换到正确的模式:

    Drawable drawable = icon.loadDrawable(getContext());
    
            if (drawable == null) {
                return;
            }
            drawable.setState(new int[] {android.R.attr.state_enabled});
    
            Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      相关资源
      最近更新 更多