Android Question Bitmap drawable cache

Emme Developer

Well-Known Member
Licensed User
Longtime User
Hi all!
I'm figured out in a strange error. I have 4 bitmap drawable, 2 couples of 2 image (with different colors).
When i try to change the color at Runtime (switching between 2 images) i don't see nothing... this is the code, maybe anyone can help me.. thanks!

B4X:
Sub InitializeImage
Dim xml As XmlLayoutBuilder
    Dim p As JavaObject
    p.InitializeStatic("android.graphics.PorterDuff.Mode")

        Dim bddowne As BitmapDrawable = xml.GetDrawable("wifidown")
        Dim bddownd As BitmapDrawable = xml.GetDrawable("wifidown")
        Dim bdupe As BitmapDrawable = xml.GetDrawable("wifiup")
        Dim bdupd As BitmapDrawable = xml.GetDrawable("wifiup")
    
        Dim jo As JavaObject = bddowne : jo.RunMethod("setColorFilter",Array(Utils.Azzurro,p.GetField("SRC_IN")))
        Dim jo As JavaObject = bddownd : jo.RunMethod("setColorFilter",Array(Utils.greydisabled,p.GetField("SRC_IN")))
        Dim jo As JavaObject = bdupe : jo.RunMethod("setColorFilter",Array(Utils.Azzurro,p.GetField("SRC_IN")))
        Dim jo As JavaObject = bdupd : jo.RunMethod("setColorFilter",Array(Utils.greydisabled,p.GetField("SRC_IN")))
    
        Dim mdown As Map = CreateMap(True:bddowne,False:bddownd)
        Dim mup As Map = CreateMap(True:bdupe,False:bdupd)
        mapBdWifi = CreateMap("utils5":mdown,"utils6":mup)
End sub

Sub click(....)
'Some stuff..
'Notif is a class. Every time i call this sub, i change notif.enabled..
If mapBdWifi.IsInitialized = False Then InitializeImage("wifi")
        cs.Color(Utils.IIF(notif.enabled,Utils.Black,Utils.GreyDisabled)).Append(" •  ").Color(Utils.Azzurro).Pop.Pop
        cs.Append(notif.custom.Get("time"))
        cs.Color(Utils.IIF(notif.enabled,Utils.Black,Utils.GreyDisabled)).Append("  •  ").Color(Utils.Azzurro).Pop.Pop
        cs.Append(Utils.GetWifiRete(notif.idrete).name)
    
        Dim m As Map = mapBdWifi.Get(Utils.loc.GetLocalizeKey(notif.custom.Get("event")))
        img.Background = m.Get(notif.enabled)
        Log(img.Background)
        Log(notif.enabled)
End sub


This is the log. True and false are notif.enabled field, the drawables are the 2 images.. i saw that these change, but i don't see nothing..

B4X:
android.graphics.drawable.BitmapDrawable@c727695
false
*** Service (sendnotification) Create ***
** Service (sendnotification) Start **
android.graphics.drawable.BitmapDrawable@54ccc54
true
** Service (sendnotification) Start **
android.graphics.drawable.BitmapDrawable@c727695
false
** Service (sendnotification) Start **
android.graphics.drawable.BitmapDrawable@54ccc54
true
** Service (sendnotification) Start **
android.graphics.drawable.BitmapDrawable@c727695
false
** Service (sendnotification) Start **
android.graphics.drawable.BitmapDrawable@54ccc54
true
** Service (sendnotification) Start **
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
I tried this code to test the color, and i saw that colors of same images are same. Last defined color is the color of 2 images. Maybe is xml.getdrawable pointing at same instance? If yes, when i declare a new bitmap, it will point at same object got from xml.getdrawable... this means that i can't use xml.getdrawable to load the same drawable in different bitmap without affect the first instance.. hope i'm wrong..

B4X:
Dim pn As Panel
        pn.Initialize("")
        mBase.AddView(pn,0,0,100%x,100%y)
        For i = 0 To 3
            Dim im As ImageView
            im.Initialize("")
            pn.AddView(im,50dip*i,50dip,50dip,50dip)
            Select i
                Case 0
                    im.Background = mdown.Get(True)
                Case 1
                    im.Background = mdown.Get(False)
                Case 2
                    im.Background = mup.Get(True)
                Case 3
                    im.Background = mup.Get(False)
            End Select
        Next
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
I don't recommend you to use XmlLayoutBuilder at all. This is not the way B4A is designed to work.

It can return the same instance, or a different instance that points to the same internal state object. You can call the mutate method with JavaObject.

https://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()
Thanks for your answer. I use layout builder because designer of my company send me files splitted in resources folder

Anyway, I don't understand how to get a different instance to avoid my issue. Should I use bitmapdrawable = jo.runmethod(...)?
Thanks


Edit: Sorry, i now understand..
this is my code (i discover also that i don't need setColorFilter, i can use setTint :D)

B4X:
    Dim bddowne As BitmapDrawable = xml.GetDrawable("wifidown")
        Dim jo As JavaObject = bddowne
        jo.RunMethodJo("mutate",Null).RunMethod("setTint",Array As Object(Utils.Azzurro))
        Dim bddownd As BitmapDrawable = xml.GetDrawable("wifidown")
        Dim jo As JavaObject = bddowne
        jo.RunMethodJo("mutate",Null).RunMethod("setTint",Array As Object(Utils.greydisabled))
        Dim bdupe As BitmapDrawable = xml.GetDrawable("wifiup")
        Dim jo As JavaObject = bddowne
        jo.RunMethodJo("mutate",Null).RunMethod("setTint",Array As Object(Utils.greydisabled))
        Dim bdupd As BitmapDrawable = xml.GetDrawable("wifiup")
        Dim jo As JavaObject = bddowne
        jo.RunMethodJo("mutate",Null).RunMethod("setTint",Array As Object(Utils.greydisabled))
 
Last edited:
Upvote 0
Top