Android Question Assume out of memory loading bitmap. Is tiling the solution?

CRoberts

Member
Licensed User
Longtime User
I have a map that is too big to load into a bitmap. It is 3361 * 4496 pixels. Not all of it loads.

I have a Rect that holds the map and as you scroll around the screen, by changing the origin the map scrolls in the imageview.

I’m sure there is a better way of doing this. But I have cut the map into 42 overlapping map sections, most of them 900*1200 pixels and I load the first one in the top left corner.

When I move scroll over the screen the map follows my finger and when it reaches a certain threshold (for when I’m going east, this is at -1277) the existing map is ditched and the next one to the east is loaded. The position of the Rect is then changed to show the same part of the map that was showing on the discarded map.

It all kind of works, with a short delay during scrolling as the new map is loaded. The transistions don’t match up exactly, but that’s a bit more investigation.

My question is, is there a better way of doing this?
B4X:
Sub Class_Globals
    Private Root As B4XView 'ignore
    Private xui As XUI 'ignore
    Private ImageView1 As ImageView
    Private Panel1 As Panel
    Dim start_x, start_y  As Float
    Dim ii_image_top_y, ii_image_left_x As Int
    Private Label1 As Label

    Type maprecord(map_offset As Int, section_name  As String, northmap As Int, eastmap As Int, southmap As Int, westmap As Int, pixelwidth As Int, pixelheight As Int)
    Private mapTable(42) As maprecord

    Private ii_current_map_offset As Int
End Sub


'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
    
Sub f_add_Bike_map_section(map_offset As Int, section_name  As String, northmap As Int, eastmap As Int, southmap As Int, westmap As Int, pixelwidth As Int, pixelheight As Int)
    mapTable(map_offset).section_name=section_name
    mapTable(map_offset).northmap=northmap
    mapTable(map_offset).eastmap=eastmap
    mapTable(map_offset).southmap=southmap
    mapTable(map_offset).westmap=westmap
    mapTable(map_offset).pixelwidth=pixelwidth
    mapTable(map_offset).pixelheight=pixelheight
    
End Sub
Sub f_populate_map_sections
    f_add_Bike_map_section(0,"bike_section_0x0.jpg",-1,6,1,-1,1348,2616)
    f_add_Bike_map_section(1,"bike_section_0x1.jpg",0,7,2,-1,1348,2616)
    f_add_Bike_map_section(2,"bike_section_0x2.jpg",1,8,3,-1,1348,2616)
    f_add_Bike_map_section(3,"bike_section_0x3.jpg",2,9,4,-1,1348,2616)
    f_add_Bike_map_section(4,"bike_section_0x4.jpg",3,10,5,-1,1348,2616)
    f_add_Bike_map_section(5,"bike_section_0x5.jpg",4,11,-1,-1,1348,2616)
    f_add_Bike_map_section(6,"bike_section_1x0.jpg",-1,12,7,0,1348,2616)
    f_add_Bike_map_section(7,"bike_section_1x1.jpg",6,13,8,1,1348,2616)
    f_add_Bike_map_section(8,"bike_section_1x2.jpg",7,14,9,2,1348,2616)
    f_add_Bike_map_section(9,"bike_section_1x3.jpg",8,15,10,3,1348,2616)
    f_add_Bike_map_section(10,"bike_section_1x4.jpg",9,16,11,4,1348,2616)
    f_add_Bike_map_section(11,"bike_section_1x5.jpg",10,17,-1,5,1348,2616)
    f_add_Bike_map_section(12,"bike_section_2x0.jpg",-1,18,13,6,1348,2616)
    f_add_Bike_map_section(13,"bike_section_2x1.jpg",12,19,14,7,1348,2616)
    f_add_Bike_map_section(14,"bike_section_2x2.jpg",13,20,15,8,1348,2616)
    f_add_Bike_map_section(15,"bike_section_2x3.jpg",14,21,16,9,1348,2616)
    f_add_Bike_map_section(16,"bike_section_2x4.jpg",15,22,17,10,1348,2616)
    f_add_Bike_map_section(17,"bike_section_2x5.jpg",16,23,-1,11,1348,2616)
    f_add_Bike_map_section(18,"bike_section_3x0.jpg",-1,24,19,12,1348,2616)
    f_add_Bike_map_section(19,"bike_section_3x1.jpg",18,25,20,13,1348,2616)
    f_add_Bike_map_section(20,"bike_section_3x2.jpg",19,26,21,14,1348,2616)
    f_add_Bike_map_section(21,"bike_section_3x3.jpg",20,27,22,15,1348,2616)
    f_add_Bike_map_section(22,"bike_section_3x4.jpg",21,28,23,16,1348,2616)
    f_add_Bike_map_section(23,"bike_section_3x5.jpg",22,29,-1,17,1348,2616)
    f_add_Bike_map_section(24,"bike_section_4x0.jpg",-1,30,25,18,1348,2616)
    f_add_Bike_map_section(25,"bike_section_4x1.jpg",24,31,26,19,1348,2616)
    f_add_Bike_map_section(26,"bike_section_4x2.jpg",25,32,27,20,1348,2616)
    f_add_Bike_map_section(27,"bike_section_4x3.jpg",26,33,28,21,1348,2616)
    f_add_Bike_map_section(28,"bike_section_4x4.jpg",27,34,29,22,1348,2616)
    f_add_Bike_map_section(29,"bike_section_4x5.jpg",28,35,-1,23,1348,2616)
    f_add_Bike_map_section(30,"bike_section_5x0.jpg",-1,36,31,24,1348,2616)
    f_add_Bike_map_section(31,"bike_section_5x1.jpg",30,37,32,25,1348,2616)
    f_add_Bike_map_section(32,"bike_section_5x2.jpg",31,38,33,26,1348,2616)
    f_add_Bike_map_section(33,"bike_section_5x3.jpg",32,39,34,27,1348,2616)
    f_add_Bike_map_section(34,"bike_section_5x4.jpg",33,40,35,28,1348,2616)
    f_add_Bike_map_section(35,"bike_section_5x5.jpg",34,41,-1,29,1348,2616)
    f_add_Bike_map_section(36,"bike_section_6x0.jpg",-1,-1,37,30,1348,2616)
    f_add_Bike_map_section(37,"bike_section_6x1.jpg",36,-1,38,31,1348,2616)
    f_add_Bike_map_section(38,"bike_section_6x2.jpg",37,-1,39,32,1348,2616)
    f_add_Bike_map_section(39,"bike_section_6x3.jpg",38,-1,40,33,1348,2616)
    f_add_Bike_map_section(40,"bike_section_6x4.jpg",39,-1,41,34,1348,2616)
    f_add_Bike_map_section(41,"bike_section_6x5.jpg",40,-1,-1,35,1348,2616)


End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Sub f_draw_an_image_page
    Dim Canvas1 As Canvas
    Canvas1.Initialize(ImageView1)
    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize(File.DirAssets, mapTable(ii_current_map_offset).section_name)
    Dim DestRect As Rect
    Try
        DestRect.Initialize(0, 0, 900dip ,  1200dip )
    Catch
        Log("Whoops!")
    End Try
    Canvas1.DrawBitmap(Bitmap1, Null, DestRect) 'draws the bitmap to the destination rectangle.

End Sub
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("b4xBikeMap")
    f_populate_map_sections
    f_draw_an_image_page
    B4XPages.SetTitle(Me, "Bike Map")
    ii_current_map_offset=0
End Sub


'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.

Sub move_Image(ax As Int, ay As Int)
    ' To go North ay is negative
    ' To go South ay is positive
    ' To go West ax is Positive
    ' To go East ax is negative


    If ax>0 And mapTable(ii_current_map_offset).westmap<>-1 Then
        ii_current_map_offset=mapTable(ii_current_map_offset).westmap
        ax=0
        f_draw_an_image_page
        ii_image_left_x=ii_image_left_x - 1178
    else if ay>0 And mapTable(ii_current_map_offset).northmap<>-1 Then
        ii_current_map_offset=mapTable(ii_current_map_offset).Northmap
        ay=0
        f_draw_an_image_page
        ii_image_top_y=ii_image_top_y - 1027
    else if ax<-1277 And mapTable(ii_current_map_offset).eastmap<>-1 Then
        ii_current_map_offset=mapTable(ii_current_map_offset).eastmap
        ax=0
        f_draw_an_image_page
        ii_image_left_x=ii_image_left_x + 1178
    else if ay<-1127 And mapTable(ii_current_map_offset).southmap<>-1 Then
        ii_current_map_offset=mapTable(ii_current_map_offset).southmap
        ay=0
        f_draw_an_image_page
        ii_image_top_y=ii_image_top_y + 1227
    End If

    If ay>0 Then
        ay=0
    End If
    If ax>0 Then
        ax=0
    End If
    
    ImageView1.Top= ay
    ImageView1.left= ax
    Label1.Text=ax & " " & ay & " " & ii_image_left_x & " " & ii_image_top_y
End Sub

    
'



Private Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    Select Action
        Case 0
            ii_image_top_y=ImageView1.Top
            ii_image_left_x=ImageView1.left
            start_x=Round(X)
            start_y=Round(Y)
        Case 1
            move_Image(X - start_x +ii_image_left_x,Y - start_y + ii_image_top_y)
        Case 2
            move_Image(X - start_x +ii_image_left_x,Y - start_y + ii_image_top_y)
    End Select

End Sub
 
Top