Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Android cache drawable resources loaded in xml layout files?

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.

like image 318
Henrique Avatar asked Dec 24 '13 23:12

Henrique


People also ask

Which function is used to load a drawable image resource?

Drawable drawable = ResourcesCompat. getDrawable (res, R. drawable. myimage, null);

How do I create an XML file in a drawable folder?

Right Click on Drawable folder -> New -> Drawable Resource File , will create a XML file inside the Drawable folder.

What are the resources how different types of resources managed in android explain?

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.

What is stroke in android XML?

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.


1 Answers

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/

like image 95
Niko Avatar answered Sep 22 '22 23:09

Niko