I have a ViewPager
which loads lots of fragments (+20). Each one of these fragments is just an instance of one single fragment, and the only thing that changes is the content of each one. The UI for this fragment has, among other widgets, 4 different ImageViews
which all load the same resource drawable but are of course located in different parts of the UI.
Now, if we take into account that I might have for example 20 fragments with this scheme, we're talking about 80 (20*4) ImageViews which would all load the same drawable.
So, that made me wonder, does Android cache in any way resources that are loaded from xml layouts? If not, is there a better option then simply loading this drawable in code, caching it and attaching it programmatically to their corresponding ImageView every time a new instance of this fragment gets created?
Thanks.
Drawable drawable = ResourcesCompat. getDrawable (res, R. drawable. myimage, null);
Right Click on Drawable folder -> New -> Drawable Resource File , will create a XML file inside the Drawable folder.
Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.
Sometimes you want an outline around your shape and to do that you can use the stroke tag. You can specify the width and color of the outline using android:width and android:color.
Android does have some kind of recycling management with Drawable
component. I have noticed this very clear when using same resources for Drawables
and using ColorFilter
with them, and I have ended seeing wrong color with previously created drawable which hasn't been visible yet in UI.
I would say your approach is OK.
In this post Romain explains that Drawables
share some information, for example the Bitmap
they are drawing: http://www.curious-creature.org/category/android/page/2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With