节约位图绘制在画布上 [英] Saving Bitmap drawn on Canvas

查看:128
本文介绍了节约位图绘制在画布上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序扩展了ImageView的和重写它的onDraw()方法。我使用的是彩色滤光片操纵位图添加像倒置,灰度ETCC一定的影响。绘制位图后,我试图挽救它,但我只能够保存原始位没有增加的影响。下面是的onDraw(在code)及保存方法:结果

In my application I extended the ImageView and overriden its onDraw() method. I am using a color filter to manipulate the bitmap for adding some effects like invert, grayscale etcc. After drawing the bitmap I am trying to save it but I am only able to save the original bitmap with no added effects. Here is the code for onDraw() and save method:

protected void onDraw(Canvas canvas)
{
    Paint paint = mPaint;
    //cmf is the color matrix filter
    paint.setColorFilter(cmf);

    if(mBitmap != null)
    {
        canvas.drawBitmap(mBitmap, offsetW, offsetH, paint);
    }
}

code保存位图:结果

code for saving the bitmap:

 try
        {
            FileOutputStream fout = new FileOutputStream(path);

            mBitmap.compress(CompressFormat.JPEG, 100, fout);
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }

我是不是做错了什么?任何帮助将AP pretiated。

Am I doing something wrong? Any help will be appretiated.

推荐答案

您的画显示的画布上,原始位不被改变。您应该创建一个新的位图和油漆就可以了。当彩色矩阵过滤器的更换做到这一点:

You are painting on the canvas that is displayed, original bitmap is not changed. You should create a new bitmap and paint on it. When color matrix filter changes do this:

Bitmap tmp = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig())
Canvas canvas = new Canvas(tmp)
cnvas.drawBitmap(tmp, 0, 0, paint);

然后,您可以使用此TMP位图绘制并保存它。

Then, you can use this tmp bitmap to draw it and save it.

而不是使用自定义的的ImageView 使用正常的,并设置其形象到这个新位图:

Instead of using customized ImageView use a normal one and set its image to this new bitmap:

imageView.setImageBitmap(tmp)

这篇关于节约位图绘制在画布上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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