Scroll an ImageView

Mike Henry

Member
Licensed User
Longtime User
I have an ImageView that might be bigger than the user's screen.
How do I configure the ImageView so the user can swipe it back and forth with his finger?
This ImageView is on a TabHost, so it's not on the main activity, which might complicate it a little.
 

Mark Read

Well-Known Member
Licensed User
Longtime User
Hello Mike,

Scrollview2d is great. Just to help, here is my code.

B4X:
Sub Process_Globals
   Private mBmp As Bitmap
End Sub

Sub Globals
   Dim sv2d As ScrollView2D
   Dim width, height As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.AddMenuItem("Fill Screen", "FitToScreen")
   Activity.AddMenuItem("Go back", "GoBack")
End Sub

Sub Activity_Resume
   Activity.Invalidate
End Sub

Sub FitToScreen_Click
   If sv2d.IsInitialized=False Then Return      'Could be that a Webview is active!
   
   'Rescale bitmap image to fit screen
   Dim AspectRate As Double
   width=mBmp.width
   height=mBmp.height
   
   AspectRate=width/height
   If width>90%x Then
      'too wide
      width=90%x
      height=width/AspectRate
   Else
      'too high
      height=90%y
      width=height*AspectRate
   End If
   
   sv2d.Panel.height=height
   sv2d.Panel.width=width
   sv2d.Top=(sv2d.height-height)/2
   sv2d.Left=(sv2d.width-width)/2
   sv2d.Invalidate
End Sub

Sub ShowImage(bmp As Bitmap)
   mBmp = bmp
   sv2d.Initialize(mBmp.width, mBmp.height, "") 
   sv2d.Panel.SetBackgroundImage(mBmp)
   Activity.AddView(sv2d, 0, 0, 100%x, 100%y)
End Sub

Sub GoBack_Click
   If Webview1.IsInitialized Then Webview1.RemoveView
    If sv2d.IsInitialized Then sv2d.RemoveView
   File.Delete(File.DirRootExternal, Main.ImageFileName)
   Activity.Finish
End Sub

'Trap the back button
Sub Activity_KeyPress (KeyCode As Int) As Boolean                            
    If KeyCode = KeyCodes.KEYCODE_BACK Then
      Activity.OpenMenu
   Else
      Return False
    End If 
End Sub

I call the ShowImage sub from another activity, so you could move this.

Hope the code explains itself.
Mark
 
Upvote 0

Mike Henry

Member
Licensed User
Longtime User
Thanks for the info. The ScrollView was my second choice, but I think I'll try manually reading a Touch event first. I've done that before and it's pretty easy. I was hoping Android had scrolling automatically built into the Activity somehow.
 
Upvote 0
Top