Canvas drawing draws a black line.

I searched for this problem and suggested various solutions.
However, no one worked for me.

I have a canvas for painting in the application.
The canvas background is set to png Image in Activity, which uses a custom view (drawView);

Bundle extras = intent.getExtras(); if (extras != null) { if (extras.containsKey("background")) { //set the background to the resource in the extras int imageResource = intent.getIntExtra("background",-1); Drawable image = getResources().getDrawable(imageResource); drawView.setBackground(image); } } 

In the DrawingView class (drawview is an instance) I save the paths drawn in the PathPaints collection, which has 3 properties (the path used by the paint and if it was an eraser);

 private ArrayList<PathPaint> paths = new ArrayList<PathPaint>(); 

Then I try to skip these paths in OnDraw and redraw them every time with the paints they were painted on (several colors);

 protected void onDraw(Canvas canvas) { //if the drawing is new - dont draw any paths if (isNew != true) { //go through every previous path and draw them for (PathPaint p : paths) { if (!p.isErase) { canvas.drawPath(p.myPath, p.myPaint); } else { //Paint eraserPaint = setDefaultPaint(); //eraserPaint.setAlpha(0xFF); //eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); //eraserPaint.setColor(Color.TRANSPARENT); //canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawPath(p.myPath, p.myPaint); } canvas.drawBitmap(canvasBitmap, 0, 0, null); } } 

I tried many of the options on the Internet, but to no avail.

I tried setting the paint on the sidewalk to set all the different properties of the commented properties.

I tried to draw a bitmap and then load that bitmap into canvas (canvas.drawBitmap (canvasBitmap, 0, 0, null))

I turned off hardware acceleration in the constructor of this class

 setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

but either the line is not drawn, or when the collection redraws the path, the eraser draws a black line;

enter image description here

Interestingly, if I perform an erase using a bitmap without an aspect of the loop, the eraser works as expected.

 //If we are making a new drawing we don't want to go through all the paths if (isNew != true && erase ==false) { //go through every previous path and draw them for (PathPaint p : paths) { if (!p.isErase) { canvas.drawPath(p.myPath, p.myPaint); } //this section now takes place in the elseIF else { Paint eraserPaint = setDefaultPaint(); eraserPaint.setAlpha(0xFF); eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); eraserPaint.setColor(Color.TRANSPARENT); canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawPath(p.myPath, p.myPaint); } } } else if (isNew != true && erase ==true) { //This works correctly for Erasing but I dont have the ability to Undo/Redo with this approach! canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint); canvas.drawPath(drawPath, drawPaint); } 

This, however, is a problem, since I want to be able to undo / redo (thus, collection points)

Can anybody help me?

+7
android android-canvas eraser
source share
2 answers

It looks like you are using only one view (layer), where you first place the background image and then draw paths that replace the background. If so, when you delete, you delete this and only view / layer, which includes paths and background. If you use two layers (two views inside Framelayout), one at the back where you load the background and one at the front where you put all the paths, then erasing on the top layer deletes only the paths and the background comes.

There are various ways to make layers. As an example, this FrameLayout replaces the view that currently contains the background and the drawn paths (in code as an XXXView).

 <FrameLayout android:layout_width= ...copy from the existing XXXView ... android:layout_height= ...copy from the existing XXXView ... > <ImageView android:id = "@+id/background" android:layout_width="fill_parent" android:layout_height="fill_parent" ... ... the background is loaded here /> <XXXView (this the existing view where the paths are drawn onto) android:layout_width="fill_parent" android:layout_height="fill_parent" ... ... no background here /> </FrameLayout> 
+2
source share

Check the bitmap on the canvas, is it Config.ARGB8888?

Also check out this Android canvas answer : draw a transparent circle on the image

I think your drawView is an ImageView, is it wrong?

0
source share

All Articles