Create Drawable at Runtime and add into Resources

2019-09-07 17:44发布

问题:

I create Drawable for my Android application at runtime. These Drawables are generated from font icon FontAwesome. I'm searching a way to add these drawables into Resources of my Android project. I would like to generate drawables at start of the application and use them in the rest of the application by accessing through resources.

Is it possible to add a drawable object into project resources in Java code?

回答1:

You can't add a drawable to your project resources at runtime. These are compiled into your APK when you create the app. What you can do is create the drawable at runtime and hold a reference to it for as long as you need it to be reused throughout the application lifecycle (of course, make sure you don't accidentally leak any memory if you choose this route).