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

时间:2015-10-22 17:56:32

标签: android bitmap drawable colorfilter

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

3 个答案:

答案 0 :(得分:0)

使用画布将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

答案 1 :(得分:0)

我遇到了同样的问题,终于找到答案了!

我们需要做以下事情来获得应用了彩色滤光片的位图。

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

答案 2 :(得分:0)

我遇到了同样的问题,终于找到了解决方案。 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);