Android set dynamic image on Imagebutton [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this questionI want to set image dynamically on the ImageButton in android. However, I have more than 开发者_Go百科200 images. What would be the good solution for that?
The best idea is that I can use the name of image to call different images. i.e. imagebutton.setImage("/res/abc.png"); however, it seems to me that it is not trivial to do so.. please help me to solve these problems.
You could use Typed Array resource. There is an example at the end of the link how to use it for drawables (images).
Edited:
Resources can be accessed as raw data: use AssetManager.open(..) Then you can use BitmapFactory.decodeStream(..) to create a Bitmap from the data stream.
You can take the images in your resource folder. After that follow this simple code:
try {
Class<drawable> res = R.drawable.class;
if(str!=null){
Field field = res.getField(str);
int drawableId = field.getInt(null);
bengalidaypng.setImageResource(drawableId);
}
}
catch (Exception e) {
System.out.println("Image not found in drawable folder");
}
A more detailed sample can be found here.
You could store the images into a database, then pull the images into the ListView when you are binding each row.
精彩评论