Greetings to all and happy new year.
I have an issue developing an application. I have an activity with a scroll view which contains some pictures (100 pcs).
After compiling and testing, I lived the OutOfMemoryError exception, while changing the orientation. After some research in the online community I decided to apply the one bitmap solution. I dimmed and initialized bmpBitmap in the Sub Globals section and in the sub that fills the scroll with images, I did draw on that bitmap and set it as background to a label.
Using the same bitmap in all labels, without re-initialize it, had the side-effect of updating the previous declared labels with the new background. If I initialize the bitmap in the loop, I will be trapped in the OutOfMemory error again.
Can someone help?
Below is a sample code of filling the labels with backgrounds and I have attach two shots of the application. First-one is the wrong result and the second if the preferred.
I have an issue developing an application. I have an activity with a scroll view which contains some pictures (100 pcs).
After compiling and testing, I lived the OutOfMemoryError exception, while changing the orientation. After some research in the online community I decided to apply the one bitmap solution. I dimmed and initialized bmpBitmap in the Sub Globals section and in the sub that fills the scroll with images, I did draw on that bitmap and set it as background to a label.
Using the same bitmap in all labels, without re-initialize it, had the side-effect of updating the previous declared labels with the new background. If I initialize the bitmap in the loop, I will be trapped in the OutOfMemory error again.
Can someone help?
Below is a sample code of filling the labels with backgrounds and I have attach two shots of the application. First-one is the wrong result and the second if the preferred.
B4X:
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim cnvCanvas As Canvas
Dim rctRect As Rect
Dim bmpBitmap As Bitmap
End Sub
' Fill the movie scroll with Icons
Sub FillMovies_Icons(ScreenWidth As Int)
Dim Cursor1 As Cursor
Dim Query As String
Dim M, N, Gap, SemiGap As Int
Dim ImageLeft, ImageTop, ImageWidth, ImageHeight, RankValueWidth As Int
' Building SQL
bla bla bla
' Query database
bla bla bla
' Initialize Drawables
bmpBitmap.InitializeMutable(ImageWidth, ImageHeight)
cnvCanvas.Initialize2(bmpBitmap)
' Loop in records
For N=0 To Cursor1.RowCount - 1
Cursor1.Position=N
Dim lblMovieIcon As Panel
Dim lblTitle As Label
' Label Poster
lblMovieIcon.Initialize("MovieItem")
svMovies.Panel.AddView(lblMovieIcon, ImageLeft, ImageTop, ImageWidth, ImageHeight)
' Title
lblTitle.Initialize("")
lblMovieIcon.AddView(lblTitle, SemiGap, SemiGap, ImageWidth - Gap, 10)
lblTitle.TextColor=Colors.White
lblTitle.Gravity=Gravity.TOP + Gravity.CENTER_HORIZONTAL
lblMenu.TextSize=MM.Settings_FontSize
MM.WrapLabelHeight(lblTitle, False)
MM.ShowEllipsis_Label(lblTitle, Field_name)
' Shadow
For M=0 To SemiGap
cnvCanvas.DrawLine(SemiGap - M, SemiGap - M, ImageWidth - SemiGap + M, SemiGap - M, Colors.ARGB(150 - (150 * M / SemiGap), 0, 0, 0), 1)
cnvCanvas.DrawLine(SemiGap - M, SemiGap - M, SemiGap - M, ImageHeight - SemiGap + M, Colors.ARGB(150 - (150 * M / SemiGap), 0, 0, 0), 1)
cnvCanvas.DrawLine(SemiGap - M, ImageHeight - SemiGap + M, ImageWidth - SemiGap + M + 1, ImageHeight - SemiGap + M, Colors.ARGB(150 - (150 * M / SemiGap), 0, 0, 0), 1)
cnvCanvas.DrawLine(ImageWidth - SemiGap + M, ImageHeight - SemiGap + M, ImageWidth - SemiGap + M, SemiGap - M, Colors.ARGB(150 - (150 * M / SemiGap), 0, 0, 0), 1)
Next
' Poster bitmap
rctRect.Initialize(SemiGap, SemiGap, ImageWidth - SemiGap, ImageHeight - SemiGap)
If File.Exists(MM.OutputFolder & "/posters", Field_id & ".jpg") Then
cnvCanvas.DrawBitmap(LoadBitmapSample(MM.OutputFolder & "/posters", Field_id & ".jpg", ImageWidth - SemiGap, ImageHeight - SemiGap), Null, rctRect)
Else
cnvCanvas.DrawBitmap(MM.bmpNoImage, Null, rctRect)
End If
' Title gradient
For M=0 To 10
cnvCanvas.DrawLine(SemiGap - 1, SemiGap + lblTitle.Height * M/7, ImageWidth - SemiGap, SemiGap + lblTitle.Height * M/7, Colors.ARGB(250 - M * 24, 0, 0, 0), lblTitle.Height/7)
Next
' Draw bmpBitmap
lblMovieIcon.SetBackgroundImage(cnvCanvas.Bitmap)
lblMovieIcon.Invalidate
Next
' Close DB
MM.sqlDB.Close
End Sub