如何更改画布上绘制的位图的颜色?

时间:2019-01-28 13:07:13

标签: java android canvas

我是android新手,我有一个可以在画布上绘画的应用程序。在画布上绘画后,我将位图保存为png格式的图像,并且可以正常工作。

绘画是使用绿色完成的,但是在保存之前我想更改绿色绘制的位图的颜色,如何将绿色更改为白色。

一个例子就是这样

在绘画时:(图像设置为背景,即MyView.setbackgroud(sample.png))

during painting

保存时:(保存图像后的结果)

saved image

我想要的结果是这样的,它将以png格式保存。

resultant image

       FileOutputStream ostream = null;
    try
    {
        ostream = new FileOutputStream(file);

        System.out.println(ostream);
        View targetView = mv;

        // myDrawView.setDrawingCacheEnabled(true);
        //   Bitmap save = Bitmap.createBitmap(myDrawView.getDrawingCache());
        //   myDrawView.setDrawingCacheEnabled(false);
        // copy this bitmap otherwise distroying the cache will destroy
        // the bitmap for the referencing drawable and you'll not
        // get the captured view
        //   Bitmap save = b1.copy(Bitmap.Config.ARGB_8888, false);
        //BitmapDrawable d = new BitmapDrawable(b);
        //canvasView.setBackgroundDrawable(d);
        //   myDrawView.destroyDrawingCache();
        // Bitmap save = myDrawView.getBitmapFromMemCache("0");
        // myDrawView.setDrawingCacheEnabled(true);
        //Bitmap save = myDrawView.getDrawingCache(false);




        Bitmap well = mv.mBitmap;
        Bitmap save = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        //Paint paint2 = new Paint();
       // paint2.setColor(Color.WHITE);

        Canvas now = new Canvas(save);
        now.drawRect(new Rect(0,0,320,480), paint);
        now.drawBitmap(well, new Rect(0,0,well.getWidth(),well.getHeight()), new Rect(0,0,320,480),null);
       // now.drawBitmap(well,well.getWidth(),well.getHeight(),paint2);
      //  now.setColorFilter(ContextCompat.getColor(this, R.color.colorBlack));
      //  now.drawBitmap(well,0,paint2,well.getWidth(),well.getHeight());

        if(save == null) {
            System.out.println("NULL bitmap save\n");
        }
        save.compress(Bitmap.CompressFormat.PNG, 100, ostream);
        //bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream);
        //ostream.flush();
        //ostream.close();
    }catch (NullPointerException e)
    {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Null error", Toast.LENGTH_SHORT).show();
    }

    catch (FileNotFoundException e)
    {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "File error", Toast.LENGTH_SHORT).show();
    }

    catch (IOException e)
    {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "IO error", Toast.LENGTH_SHORT).show();
    }

1 个答案:

答案 0 :(得分:0)

使用Color.black代替Color.argb(255, 0, 0, 0);

ARGB = Alpha, Red, Green, Blue.