Erase Draw on activity

AndroidUserCCS

Member
Licensed User
Longtime User
Hi I am using the following code for draw inside an image and save this with the change.

I get this code from some example in the board.

How do I to erase the lastest line drawed, when I use colors.transparent I see a black line.



B4X:
Sub Process_Globals
    Dim fromx,fromy,tox,toy As Int
   
End Sub

Sub Globals
    Dim Obj1 As Reflector
    
    Dim Panel1 As Panel
    Dim picCanvas As Canvas
   Dim bm As Bitmap
    Dim Button2 As Button
   Dim Button3 As Button
   Dim color2 As Int    
    Dim DestRect As Rect
End Sub

Sub Activity_Create(FirstTime As Boolean)
    

      Dim bd As BitmapDrawable
       bd.Initialize(LoadBitmap(File.DirAssets, "h1.jpg"))
       bd.Gravity = Gravity.FILL
      
      Activity.Background = bd   
      
   picCanvas.Initialize(Activity)   
      
   color2 = Colors.Red
    
    Obj1.Target = Activity
    Obj1.SetOnTouchListener("Panel1_OnTouch")
   Button2.Initialize("Button2")
   Button2.Text = "Clear"
   
   Button3.Initialize("Button3")
   Button3.Text = "Save"
   
   Activity.AddView(Button2,0,0,100%x,60dip)
   Activity.AddView(Button3,0,60dip,100%x,60dip)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Panel1_OnTouch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
  Select action
   Case Activity.ACTION_DOWN
    
        fromx = X
        fromy = Y
        picCanvas.DrawCircle(fromx, fromy, 2dip, color2, True, 3dip)
      
   Case Activity.ACTION_UP
      
      tox = X
      toy = Y
      picCanvas.DrawCircle(tox, toy, 2dip, color2, True, 3dip)
      
   Case Activity.ACTION_MOVE
   
      tox = X
      toy = Y
      picCanvas.DrawLine(fromx,fromy,tox,toy,color2, 3dip)

      fromx = tox
      fromy = toy
      
   End Select 
   Activity.Invalidate
      
   Return True
End Sub

Sub Button2_Click

color2 = Colors.Transparent 

End Sub

Sub Button3_Click
  
  Dim Out As OutputStream
   bm.Initialize3(picCanvas.Bitmap)
   Out = File.OpenOutput(File.DirRootExternal, "Test2189.png", False)
   bm.WriteToStream(Out, 100, "PNG")
   Out.Close
End Sub
 

klaus

Expert
Licensed User
Longtime User
If you draw with a transparent color on an Activity you get a black color.
If you draw on a Panel or an ImageView with transparent color you'll see what's behind the View you draw on and not the previous colors of the pixels you modified.
If you want to erase a line drawn on an image you must draw the original image again. If there already were other lines these are lost.
You could memorise the properties of all lines and draw them again, this may take some time depending on the number of lines.
Or you could memorise the current bitmap before drawing the new line, to erase the last line you load the previous bitmap, this may be memory consuming.

Best regards.
 
Last edited:
Upvote 0
Top