Hi All,
I've been working on a way to get images on a scrollview
instead of standard imageviews from the web using the
FlickrView demo.
But it's getting messy and I can't see the wood for the trees
anymore.
This is what I have:
'main module FlickrView demo:
And the scrollview module:
If someone's willing to give me a hint or direction, I would appreciate it.
Thanks for now.
I've been working on a way to get images on a scrollview
instead of standard imageviews from the web using the
FlickrView demo.
But it's getting messy and I can't see the wood for the trees
anymore.
This is what I have:
'main module FlickrView demo:
B4X:
Sub Process_Globals
Dim MainUrl As String
MainUrl = "http://www.com/test/"
End Sub
Sub Globals
Dim ImageIndex As Int
Dim btnConnect As Button
Dim ImageView1 As ImageView
Dim ImageView2 As ImageView
Dim ImageView3 As ImageView
Dim ImageView4 As ImageView
Dim ImageView5 As ImageView
Dim ImageView6 As ImageView
Dim ImageView7 As ImageView
Dim ImageView8 As ImageView
Dim ImageView9 As ImageView
Dim ivs() As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("1")
ivs = Array As ImageView(ImageView1, ImageView2, ImageView3, ImageView4, _
ImageView5, ImageView6, ImageView7, ImageView8, ImageView9)
ResetImagesBackground
HttpUtils.CallbackActivity = "Main"
HttpUtils.CallbackJobDoneSub = "JobDone"
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnConnect_Click
HttpUtils.CallbackUrlDoneSub = "" 'don't want to handle this event
HttpUtils.Download("Main page", MainUrl)
ProgressDialogShow("Fetching data...")
End Sub
Sub Activity_Resume
'This is important as it is possible that the activity was in the background when the job has finished.
If HttpUtils.Complete Then JobDone(HttpUtils.Job)
End Sub
'Usually we will want to have a single JobDone sub for all jobs.
'This allows us to check HttpUtils.Complete flag in Activity_Resume (as shown above).
Sub JobDone(Job As String)
Select job
Case "Main page"
HandleMainPage
Case "Images"
ImagesJobDone
End Select
HttpUtils.Complete = False
End Sub
'Parse the main page and find the 9 image links
Sub HandleMainPage
If HttpUtils.IsSuccess(MainUrl) = False Then
ToastMessageShow("Error downloading main page.", True)
Return
End If
ResetImagesBackground
start = DateTime.Now
Dim TextReader1 As TextReader
TextReader1.Initialize(HttpUtils.GetInputStream(MainUrl))
Dim pattern, class As String
class = "<td class=" & QUOTE & "thumbcell" & QUOTE & ">"
pattern = "img src=\q([^q]+)\q".Replace("q", QUOTE)
Dim links As List
links.Initialize
Dim line As String
line = TextReader1.ReadLine
Do While line <> Null
If line.IndexOf(class) > -1 Then
Dim link As String
Dim m As Matcher
m = Regex.Matcher(pattern, line)
If m.Find Then
links.Add("http://www.com/test/" & m.Group(1).Replace("_small","")) 'add the image link
End If
End If
line = TextReader1.ReadLine
Loop
TextReader1.Close
Log("done parsing main page: " & (DateTime.Now - start))
HttpUtils.CallbackUrlDoneSub = "ImageUrlDone"
HttpUtils.DownloadList("Images", links)
ImageIndex = 0
btnConnect.Enabled = False
ProgressDialogHide
End Sub
Sub ImageUrlDone (Url As String)
ivs(ImageIndex).Bitmap = HttpUtils.GetBitmap(Url)
ivs(ImageIndex).Gravity = Gravity.FILL
ImageIndex = ImageIndex + 1
'The SuccessfulUrls map holds all the successful links. We are removing this link from the list
'to avoid dealing with this link again in ImagesJobDone
HttpUtils.SuccessfulUrls.Remove(Url)
End Sub
Sub ImagesJobDone
'In most cases this For loop will not be used, assuming that ImageUrlDone was called for each Url.
'However if the activity was paused during the download it is possible that we have missed some calls.
'So here we make sure that all downloaded images are visible.
For i = 0 To HttpUtils.SuccessfulUrls.Size - 1
ivs(ImageIndex + i).Bitmap = HttpUtils.GetBitmap(HttpUtils.SuccessfulUrls.GetKeyAt(i))
ivs(ImageIndex + i).Gravity = Gravity.FILL
Next
btnConnect.Enabled = True
End Sub
Sub ResetImagesBackground
For i = 0 To Activity.NumberOfViews - 1
If Activity.GetView(i) Is ImageView Then Activity.GetView(i).Color = Colors.Black
Next
End Sub
'Show the second activity with the chosen image.
Sub img_Click
Dim iv As ImageView
iv = Sender
Dim bd As BitmapDrawable
bd = iv.Background
Activity2.bmp = bd.Bitmap
StartActivity(Activity2)
End Sub
And the scrollview module:
B4X:
Sub Process_Globals
Dim Bitmaps As List
End Sub
Sub Globals
Dim LbResults As Label
Dim ScrollView1 As ScrollView
Dim iv3 As ImageView
Dim Panel2 As Panel
End Sub
Sub Activity_Create(FirstTime As Boolean)
ProgressDialogShow("Loading images")
LoadImages
ProgressDialogHide
Activity.LoadLayout("results")
ScrollView1.Panel.Height = 300dip * Bitmaps.Size
For i = 0 To Bitmaps.Size - 1
Dim iv As ImageView
iv.Initialize("iv")
Dim bd As BitmapDrawable
bd.Initialize(Bitmaps.Get(i))
iv.Background = bd
ScrollView1.Panel.AddView(iv, 5dip, 5dip + i * 300dip, ScrollView1.Width - 10dip, 290dip)
Next
End Sub
Sub LoadImages
Bitmaps.Initialize
Dim f As String
If File.Exists(Main.ImageFolder, "") = False Then
ToastMessageShow("Images not found:" & CRLF & Main.ImageFolder, True)
Return
End If
For i = 0 To Main.Files.Size - 1
f = Main.Files.Get(i)
If f.ToLowerCase.EndsWith(".jpg") Then
Dim b As Bitmap
b.InitializeSample(Main.ImageFolder, f, 300dip, 300dip)
Bitmaps.Add(b)
If Bitmaps.Size > 100 Then Exit
End If
Next
ToastMessageShow(Bitmaps.Size & " images found", False)
End Sub
Sub iv_Click
Dim iv As ImageView
iv = Sender
Dim bd As BitmapDrawable
bd = iv.Background
showpics.Bitmap1 = bd.Bitmap
StartActivity(showpics)
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
If someone's willing to give me a hint or direction, I would appreciate it.
Thanks for now.