Varying ImageView source according to value
I would like to show something similar to a progressbar by using different images, for exampl开发者_如何学Goe an empty lawn if progress = 0 and a nearly complete house if progress = 95.
I have 30 files named image1.jpg .... image30.jpg
Would it be possible to set the Drawable of an ImageView by using a string for the file name? That way I could just get an int between 1 and 30 from the progress and add that to a string that I use to set the Drawable.
I have read this: Android: Imageview showing an image according to edittext option but I don't want to write code for every case as I would like to use this function at different places of a project.
I would be very grateful for any help, either on how I can use strings and filenames for the Drawable or if there is another way of solving this.
Best regards, Emil
public void setImageSrc(int i){
final ImageView iv = (ImageView) findViewById(R.id.naviView);
iv.setImageResource(getResources().getIdentifier("image"+i, "drawable", "com.example"));
}
You can retrieve the id if you know the name of the resources and then set the image via the id you determined earlier
精彩评论