Android的油漆PorterDuff.Mode.CLEAR [英] Android Paint PorterDuff.Mode.CLEAR

查看:181
本文介绍了Android的油漆PorterDuff.Mode.CLEAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的应用程序,它是绘制在画布上类似于从Android SDK中手指画演示。当我使用 PorterDuff.Mode.CLEAR 我的问题是。当绘画和画布,如果我试图抹掉的东西,它的正常工作。但是,如果我尝试保存我的形象为 PNG 文件橡皮擦的笔都为黑色,而我不知道为什么会这样。下面是一个例子我在做什么:

I'm working on application which is drawing on Canvas similar to Finger Paint demo from Android SDK. My problem is when I'm using PorterDuff.Mode.CLEAR. When drawing and Canvas and if I try to erase something, it's working fine. But if I try to save my image as PNG file the strokes of eraser are coloured black, and I'm not sure why is this happening. Here is an example what I'm doing :

@Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);
    }

橡皮擦:

case ERASE_MENU_ID:
            mPaint.setXfermode(new PorterDuffXfermode(
                                                    PorterDuff.Mode.CLEAR));
            return true;

和我如何保存图像:

            Calendar currentDate = Calendar.getInstance();
            SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMMddHmmss");
            String dateNow = formatter.format(currentDate.getTime());
            File dir = new File(mImagePath);
            if(!dir.exists())
                dir.mkdirs();

            File file = new File(mImagePath + "/" + dateNow +".png");

            FileOutputStream fos;
            try {
                fos = new FileOutputStream(file);
                mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                fos.close();
                Toast.makeText(getApplicationContext(), "File saved at \n"+mImagePath + "/" + dateNow +".png", Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {
                Log.e("Panel", "FileNotFoundException", e);
            } 
            catch (IOException e) {
                Log.e("Panel", "IOEception", e);
            }
            return true;

这里是图像的一个例子:

And here is an example of images :

这里是我的画布看起来像保存前:

here is what my canvas looks like before saving :

和这里的图像保存它放在SD卡后:

and here is the image after saving it on sd card :

推荐答案

与fingerpaint code的问题是,你看到的是不一样的就是COM pressed成PNG。看的OnDraw()。首先,你画在屏幕白。然后添加的位图。因为你用波特达夫清除位图的删除部分包含实际上透明的黑色像素(值00000000)。但是,因为你有白色背景这些的的像素显示为白色。

The problem with the fingerpaint code is that what you see is not the same that is compressed into the png. Look at onDraw(). First you draw the screen white. Then you add the Bitmap. Because you used Porter Duff Clear the erased part of the bitmap contains actually transparent black pixels (value 0x00000000). But because you have the white background these black pixels show as white.

要解决这个问题要么改变你节省code做同样的事情,平局code

To fix this either change your save code to do the same thing as the draw code

 try {
                    fos = new FileOutputStream(file);
                    Bitmap saveBitmap = Bitmap.createBitmap(mBitmap);
                    Canvas c = new Canvas(saveBitmap);
                    c.drawColor(0xFFFFFFFF);
                    c.drawBitmap(mBitmap,0,0,null);
                    saveBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    saveBitmap.recycle();
...

或者不使用PortDuff.Clear:

or don't use PortDuff.Clear:

    case ERASE_MENU_ID:
        mPaint.setColor(Color.WHITE);

这篇关于Android的油漆PorterDuff.Mode.CLEAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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