【问题标题】:How to change color of painted bitmaps on canvas?如何更改画布上绘制位图的颜色?
【发布时间】:2019-01-28 21:28:18
【问题描述】:

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

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

一个例子是这样的

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

On Saving:(保存图片后的结果)

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

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

【问题讨论】:

  • 我不知道我是否问错了问题,但我只想将颜色从绿色和黑色变为白色和黑色,如何做到这一点。任何帮助

标签: java android canvas


【解决方案1】:

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

ARGB = Alpha, Red, Green, Blue.

【讨论】:

  • 它不起作用,而不是将绿色更改为白色,而是将 color.argb(255,0,255,0); 上的黑色更改为浅绿色;
  • 笔画是绿色的,我想要白色
  • 绿色将是 255, 0, 255, 0 ... 零红,全绿,零蓝
  • 在第二张图片中,油漆是使用绿色完成的,我想更改为白色,并且在给颜色 255,0,255,0 时它不会更改绘制区域,但画布黑色背景更改为绿色。
  • 我正在使用画布颜色作为背景,因此更改颜色时,背景颜色会改变,而不是我之前所做的笔触颜色。
猜你喜欢
  • 1970-01-01
  • 2018-12-10
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多