This is often the case in development: there are a number of similar images in the Drawable folder (the similarity here is not only similar to the image name, but also similar), and now you have to select one of them according to the user's operation. For example, in a speech-like development, press and hold the "talk" button can be entered into the user said, then, need to have a picture in real time to record the user speaking volume level (1 to 7), we can put 7 pictures in the Drawable folder, the name is "Voice1.png"-" Voice7.png ", use other code to get the current user speaking volume level, and then through level 1-7 to find the picture voice1.png-voice7.png. The code is as follows (the function in the code is only used to get the ID of the image, and the ID is assigned to ImageView afterwards):
1 Public int int Level ) {2 return context.getresources (). Getidentifier ("Voice" + Level, "drawable", Context.getpackagename ()); 3 // find a picture named voice in the Drawable folder of the package that contains the current context 4 }
Android to find pictures by name