Android Question Why not Bitmap displayed in ImageView?

goomoo

Member
Licensed User
Longtime User
I have added the images files to FileManager tab, and my code is:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private HSV_TOP_BAR As HorizontalScrollView
    Private button1 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("mainLayout")
    button1.Initialize("button1")
    button1.Color=Colors.ARGB(255,0,255,0) 'worked well
   
    'button1.Bitmap=LoadBitmapSample(File.DirAssets,"move_left.png",128,128)  '<-- not work!
    'button1.Bitmap=LoadBitmap(File.DirAssets,"move_left.png")    '<-- not work!
    Dim bmp As Bitmap
    bmp=LoadBitmap(File.DirAssets,"move_left.png")
    Log(bmp)  'Load success.
    button1.SetBackgroundImage(bmp)  '<-- not work
   
    HSV_TOP_BAR.Panel.AddView(button1,2,2,100%x/5,100%y)   

End Sub

Thanks a lot.
 

goomoo

Member
Licensed User
Longtime User
I have resolved this problem, code is:

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private button_width As Float

    Private HSV_TOP_BAR As HorizontalScrollView
    Private button1 As ImageView  ' Not created with the Designer   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("mainLayout")
    button1.Initialize("button1")
    button1.Color=Colors.ARGB(255,0,255,0) 'worked well
   
    button_width=HSV_TOP_BAR.Height
   
    Log(button_width)
           
    HSV_TOP_BAR.Panel.AddView(button1,0,0,button_width,button_width)   
   
    Dim bmp As Bitmap=LoadBitmap(File.DirAssets,"move_left.png")       
    Log(bmp)       
    Dim r1,r2 As Rect
    r1.Initialize(0,0,bmp.Width,bmp.Height)
    r2.Initialize(0,0,button_width,button_width)
   
    Dim c As Canvas
    c.Initialize(button1)
    c.DrawBitmap(bmp,r1,r2)  '<-- HaHa , OK !
       
   
    'Causes: When ImageView is created with code dynamically, assigns to its Bitmap property not work.
    'button1.Bitmap=bmp'LoadBitmapSample(File.DirAssets,"move_left.png",128,128)  '<-- not work!
    'button1.Bitmap=LoadBitmap(File.DirAssets,"move_left.png")    '<-- not work!
   
    'bmp=LoadBitmap(File.DirAssets,"move_left.png")
    'Log(bmp)  'Load success.
    'button1.SetBackgroundImage(bmp)  '<-- not work
   
    'ImageView1.Bitmap=bmp
    'HSV_TOP_BAR.Panel.AddView(ImageView1,130,0,100%x/5,100%y)

End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
The problem in your code in post #1 is 100%y!
You set the height of the button equal to the screen height.
You should use:
HSV_TOP_BAR.Panel.AddView(button1, 2dip, 2dip, 100%x/5, HSV_TOP_BAR.Height - 4dip)
You should use dip values and not pure pixels.
 
Upvote 0

goomoo

Member
Licensed User
Longtime User
The problem in your code in post #1 is 100%y!
You set the height of the button equal to the screen height.
You should use:
HSV_TOP_BAR.Panel.AddView(button1, 2dip, 2dip, 100%x/5, HSV_TOP_BAR.Height - 4dip)
You should use dip values and not pure pixels.

Thanks for your suggestions. But I think this is not the key problem, Because the ImageView displayed there with its colored background, its size is correct, in my code in post #1.
 
Upvote 0

goomoo

Member
Licensed User
Longtime User
The problem in your code in post #1 is 100%y!
You set the height of the button equal to the screen height.
You should use:
HSV_TOP_BAR.Panel.AddView(button1, 2dip, 2dip, 100%x/5, HSV_TOP_BAR.Height - 4dip)
You should use dip values and not pure pixels.

Its height is not screen height, it is its parent (HorizontalScrollView)'s height.
 
Upvote 0
Top