Android Question How to extract a country flag from a composite image.

tigrot

Well-Known Member
Licensed User
Longtime User
Hi everybody,
how can I show iconized nation's flags from a composite image, like pictures' matrix.
I have an ordered list of national prefixes, related to the images.
Have I to extract images with an external pgm and treat them saparatelly or can I handle them directly by cropping single flags from the composite one? I don't use this function many times in my app, so if the user has to wait it's not a problem.
I attach the jpg image for your reference and an ordered list of nations with additional data. Also a demo which allows to chose a flag from a list of nations(in a spinner) and phone prefixes.
Thank you for help
 

Attachments

  • flags.jpg
    flags.jpg
    410.5 KB · Views: 298
  • countries.txt
    4.1 KB · Views: 274
  • flagrender.zip
    387.3 KB · Views: 253
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Private flags As Bitmap
   Private Const flagsPerRow = 10, flagsPerColumn = 24 As Int
End Sub

Sub Service_Create
   flags = LoadBitmap(File.DirAssets, "flags.jpg")
End Sub

Public Sub GetFlag(Row As Int, Col As Int) As Bitmap
   Dim b As Bitmap
   b.InitializeMutable(flags.Width / flagsPerRow, flags.Height / flagsPerColumn)
   Dim c As Canvas
   c.Initialize2(b)
   Dim r As Rect
   r.Initialize(b.Width * Row, b.Height * Col, b.Width * (Row + 1), b.Height * (Col + 1))
   Dim dest As Rect
   dest.Initialize(0, 0, b.Width, b.Height)
   c.DrawBitmap(flags, r, dest)
   Return b
End Sub

B4X:
Activity.SetBackgroundImage(CallSub3(Starter, "GetFlag", 2, 3))

You can put it in a code module if you prefer.
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
Really smart. You are the master of OOP ! Coming from procedural languages I'm much limited in object programming.
Thank you Erel. You always have immediate answer. I add content's list to my first post plus a working demo. So if somebody needs flags, they are ready!
 
Last edited:
Upvote 0
Top