【问题标题】:How to program for eraserPaint as on canvas in android?如何在 android 的画布上为橡皮擦编程?
【发布时间】:2011-12-30 06:07:47
【问题描述】:

我已经实现了 Canvas 并在画布上绘画。 我可以用不同的颜色在画布上作画。

现在,如果有纯白色背景,那就没问题了。我正在为 Eraseand 使用白色,它的工作正常。 但是,如果背景上有任何图像并且我选择橡皮擦(如白色),那么它就是在图像上涂上白色。

我想要的是,当我选择橡皮擦时,如果背景中有任何图像,那么橡皮擦应该擦除油漆并显示图像。 . 那么anybudy可以帮助我如何实现它吗? 谢谢。

编辑: 我正在使用下面的代码来擦除正在做的油漆。但还是无法抹去。

case R.id.eraserBtn:
            currentPaint = new Paint();
            currentPaint.setAlpha(0);         
            currentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));         
            currentPaint.setAntiAlias(true);

            //currentPaint.setColor(0x00000000);
            currentPaint.setDither(true);
            currentPaint.setStyle(Paint.Style.STROKE);
            currentPaint.setStrokeJoin(Paint.Join.ROUND);
            currentPaint.setStrokeCap(Paint.Cap.ROUND);
            currentPaint.setStrokeWidth(3);
            break;

新编辑:

 case R.id.eraserBtn:
            currentPaint = new Paint();
            currentPaint.setAlpha(0);         
            currentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));         
            currentPaint.setAntiAlias(true);

            //currentPaint.setColor(0x00000000);
            currentPaint.setDither(true);
            currentPaint.setStyle(Paint.Style.STROKE);
            currentPaint.setStrokeJoin(Paint.Join.ROUND);
            currentPaint.setStrokeCap(Paint.Cap.ROUND);
            currentPaint.setStrokeWidth(15);
            break;

另一个编辑:

      while (_run){

                try{
                    canvas = mSurfaceHolder.lockCanvas(null);
                    if(mBitmap == null){
                        mBitmap =  Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
                    }
                    final Canvas c = new Canvas (mBitmap);
                    //canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                    c.drawColor(0, PorterDuff.Mode.CLEAR);
                    //canvas.drawColor(mColor);// Edited by Shreyash
                    c.drawColor(mColor);

//                    Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                      canvas.drawBitmap (kangoo, 0,  200,null);

//                  works for logo                  
//                  Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
//                  c.drawBitmap (kangoo, 0,  200,null);

                    if(!(DrawingActivity.imagePath==null)){
                        c.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
                    }
                    commandManager.executeAll(c);
                    canvas.drawBitmap (mBitmap, 0,  0,null);

                } finally {
                    mSurfaceHolder.unlockCanvasAndPost(canvas);
                }
            }

【问题讨论】:

    标签: android android-layout canvas android-emulator android-widget


    【解决方案1】:

    您应该为绘制对象设置 setXfermode 并查看不同的 PorterDuffModes。

    这应该证明对您有用: Erase bitmap parts using PorterDuff mode

    【讨论】:

    • 请查看更新后的问题。我仍然无法擦除油漆。
    • 使用 DST_IN,而不是 DST_OUT。还要确保,在创建位图时,您使用的是 Bitmap.Config.ARGB_8888。只有这样 setAlpha() 才会起作用。
    • 我已经按照你说的设置了值。但仍然没有得到解决方案。
    • 在您创建位图的位置发布代码。 Bitmap.Config.ARGB_8888 是绝对必要的。
    • 好的,请查看我正在创建 mBitmap 并在其上绘制的代码的另一个编辑。
    【解决方案2】:

    我并不完全了解您的代码。但setColor 对我有用。但是您可以尝试以下代码之一,虽然不是一个确切的解决方案,但供您参考......

    1. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html
    2. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchPaint.html
    3. 最好的一个(我建议):http://som-itsolutions.blogspot.com/2010/12/freeware-android-paint.html

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 1970-01-01
      • 2011-12-24
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2019-01-30
      相关资源
      最近更新 更多