Android Question refresh imageview

Sia

Member
Licensed User
Longtime User
Hello every one,
how can refresh imageview after draw line with Canvas

in visual.net i did it with PictureBox1.Refresh() but in b4a i can't find any solution's for this

i try to draw line tandem like a column (pic name : col.jpg )

b4a draw it in buffer and when it complete show me

how can fix it ?
 

Attachments

  • col.jpg
    col.jpg
    13.1 KB · Views: 212
Last edited:

RandomCoder

Well-Known Member
Licensed User
Longtime User
Use the invalidate property to force the imageview to redraw itself. You can also use invalidate on the activity to redraw the whole activity if you want. I believe the idea is that you should update all your drawings and images essentially in the background and then call invalidate to refresh either the whole view or a section of it. Which ever is preferable for your needs.
 
Upvote 0

Sia

Member
Licensed User
Longtime User
Hello,
it doesn't work for me
i add ImageGraph.Invalidate , Activity.Invalidate after every where i use drawline but doesn't work
maybe it's my fault
please check scan.zip

thank you
 

Attachments

  • scan.zip
    32.5 KB · Views: 158
Last edited:
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Hello,
it doesn't work for me
i add ImageGraph.Invalidate , Activity.Invalidate after every where i use drawline but doesn't work
maybe it's my fault
please check scan.zip

thank you
Sorry for the delay, I've had to update to version 4 and download all the libraries.
I get errors when loading the bitmap and so I've commented that bit out. The two DrawLine commands on lines 106 and 107 are working, I've changed them to draw diagonal lines so that so can see them better. Not sure exactly what your app does as I've not spent the time to study it, but you will see that if you press button 1 first the two diagonal lines are displayed. Then if you press button 2 a black band will be draw vertically through the image at varying positions.
Is it possible that the only problem is that your drawing a black band on a black background?

A smaller sample of the problem would be easier to assist with.
 

Attachments

  • Scan.zip
    32.6 KB · Views: 191
Upvote 0

Sia

Member
Licensed User
Longtime User
very thanks for your answer,
i doesn't get any error in loading bitmap
and button number 1 (first) it's ok no problem but when use button 2 ( Second ) you must see draw line with color and draw line over another line like column

but 2 green column in the page only for margin and detect point start and endline.
everything in this project ok and doesn't have problem
but i don't know why when i use invalidate nothing go to change it's very impotent to draw and show line over line

i wrote this code in vb.net and everything is ok and draw line over line without any problem
but in b4a this code have problem
in vb.net i use this code exactly and all difference between b4a and vb.net only this code PictureBox1.Refresh()

if you want i can attach vb.net code
please give me solution for resolve this issue.

very thanks.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User

Hi @Sia please see the Youtube clip. The error I received from loading the bitmap was due to the capitalisation of the first character of the image file, android is case sensitive to file names. It looks to me like the DrawLine is working but maybe not quite as you expect? There are a lot of calculations going on in your code, have you tried stepping through the code to ensure that the values are correct? They don't mean a lot to me as I do not know what is expected.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
@Sia please see the attached example which better demonstrates the use of invalidate...
B4X:
#Region  Project Attributes
    #ApplicationLabel: DrawLine_Ex
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim tmr As Timer
    Dim blnColourInvert, blnRefresh As Boolean
    Dim intStart, intLineWidth As Int
End Sub

Sub Globals
    Dim btnStart As Button
    Dim btnRefresh As Button
    Dim img As ImageView
    Dim c As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("DrawLine_Ex")
    tmr.Initialize("tmr", 250)
    c.Initialize(img)
    If FirstTime Then blnRefresh = True
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btnStart_Click()
    c.DrawColor(Colors.Black)
    intStart = 0dip
    intLineWidth = 5%x
    tmr.Enabled = True
End Sub

Sub btnRefresh_Click()
    ' When set will invalidate the img every tick
    blnRefresh = Not(blnRefresh)
End Sub

Sub tmr_Tick()
    ' Set line colour, alternates between blue and green each tick
    Dim intColour As Int
    blnColourInvert = Not(blnColourInvert)
    If blnColourInvert Then
        intColour = Colors.Blue
    Else
        intColour = Colors.Green
    End If
    ' Draw a vertical line at position intStart
    c.DrawLine(intStart,0Dip,intStart,100%y,intColour,intLineWidth)
    ' Increment start position ready for next Tick
    intStart = intStart + intLineWidth
    ' Check whether to invalidate every tick or when all lines have been drawn
    If intStart > img.Width Then
        tmr.Enabled = False
        img.Invalidate
    Else If blnRefresh Then
        img.Invalidate
    End If
End Sub
 

Attachments

  • DrawLine_Ex.zip
    7.9 KB · Views: 166
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
@Sia Thanks for the video, it clearly showed me what you required. Check the attached zip file, I've tidied the code up slightly, fixed the indenting and removed excessive white-space. If you uncomment the other sections of code shown below...
B4X:
'        ImageGraph.Invalidate
'        Activity.Invalidate
'        DoEvents
You will then start to see every line drawn on the screen.

The reason that you weren't seeing the results of using invalidate to force the view to redraw itself is because at those points in the program where you called invalidate the code was stuck in a loop and so not able to perform GUI updates. Using DoEvents is a way of attempting to tell the processor to update the GUI if and when possible.

A word of warning though! It's not ideal to use DoEvents in this way as it can cause a bottleneck on the processor and make your app feel unresponsive. A better approach would be to use a timer with a very short interval to draw each segment and allow the GUI to update itself between tick events. But I realise that would be a rework of your code.
 

Attachments

  • Scan.zip
    32.4 KB · Views: 220
  • Like
Reactions: Sia
Upvote 0
Top