简体   繁体   English

转换成位图后,Drawable会丢失滤色器

[英]Drawable loses color filter after converting into bitmap

I am trying to add a color filer in a drawable and then convert it in Bitmap. 我试图在drawable中添加一个颜色文件管理器,然后在Bitmap中转换它。 The problem is when convert the drawable into bitmap it loses it's color filter.I used drawable in imageview and its have the color filter but using bitmap in imageview doesn't have any color effect. 问题是当将drawable转换为位图时,它会丢失它的滤色器。我在imageview中使用了drawable并且它具有滤色器,但在imageview中使用位图没有任何颜色效果。 Why this happen ? 为什么会这样? Thanks in advance. 提前致谢。

Use a canvas to blit the Drawable back onto a bitmap: 使用画布将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 http://developer.android.com/guide/topics/graphics/2d-graphics.html

I had the same problem and I figured it out finally! 我有同样的问题,我终于弄明白了!

We need to do following thing to get the bitmap with color filter applied on it. 我们需要做以下事情来获得应用了滤色器的位图。

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

I've faced the same issue and finally found a solution. 我遇到了同样的问题,终于找到了解决方案。 Drawable can have multiple states thus you might be drawing wrong state. Drawable可以有多个状态,因此您可能会绘制错误的状态。

You should switch it to proper mode before drawing: 您应该在绘制之前将其切换到正确的模式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM