Android Paint PorterDuff.Mode.CLEAR drawing black color on my view

一个人想着一个人 提交于 2019-12-08 01:54:49

问题


I want to implement eraser for my painting app . I am able to erase with the following code

 paint.setColor(0x00000000);
 paint.setAlpha(0x00);

But after erasing when you start painting again it does not paint properly so any idea to erase paint please suggest.


回答1:


Try the below code.

      paint.setAlpha(0xFF);//transperent color
      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));//clear the draw

Also have a look at the sample FingerPaint.java in the api demos under the folder graphics.

 setAlpha(int a)

Helper to setColor(), that only assigns the color's alpha value, leaving its r,g,b values unchanged.

http://developer.android.com/reference/android/graphics/Paint.html. Have a look a the documentation.

Edit:

Also check this

https://code.google.com/p/android/issues/detail?id=54105#c1




回答2:


I had same issue.

Need to set view's setLayerType.

yourView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

You can set it in constructor or by view's object.

Its done.




回答3:


This is should resolve this issue

private void touch_move(float x, float y){
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);

        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath, mPaint);
        mPath.reset();
        mPath.moveTo(mX, mY);

        mX = x;
        mY = y;
    }
}

in touch_up() change it to:

private void touch_up() {

     mPath.reset();
}


来源:https://stackoverflow.com/questions/15546342/android-paint-porterduff-mode-clear-drawing-black-color-on-my-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!