Drawable转换为位图后会丢失滤色器 [英] Drawable loses color filter after converting into bitmap

查看:115
本文介绍了Drawable转换为位图后会丢失滤色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to add a color filer in a drawable and then convert it in 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. Why this happen ? Thanks in advance.

推荐答案

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

I've faced the same issue and finally found a solution. Drawable can have multiple states thus you might be drawing wrong state.

在绘制之前,您应该将其切换到适当的模式:

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

这篇关于Drawable转换为位图后会丢失滤色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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