Issue
How to get all Drawable
resources and put them into ArrayList
?
I wanna make something like playing cards, put them into ArrayList
and shuffle them. After that I wanna give players cards. What is the best way to do it?
Solution
To iterate through your drawables, you can use the Field
class.
Try something like the following:
Field[] drawablesFields = com.your.project.R.drawable.class.getFields();
ArrayList<Drawable> drawables = new ArrayList<>();
for (Field field : drawablesFields) {
try {
Log.i("LOG_TAG", "com.your.project.R.drawable." + field.getName());
drawables.add(getResources().getDrawable(field.getInt(null)));
} catch (Exception e) {
e.printStackTrace();
}
}
Or you could also use the assets
folder. Put your images inside a sub-directory called images
, and the use the AssetManager
class to get those files. This is an approach that is not project-specific.
Try something like:
AssetManager am = context.getAssets();
String[] files = am.list("images");
InputStream istr = null;
ArrayList<Drawable> drawables = new ArrayList<>();
for (String file : files) {
Drawable d = Drawable.createFromStream(am.open(file), null);
drawables.add(d);
}
Answered By - Mauker
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.