How to get an array of images?
Asked Answered
K

1

5

I want to make a Battery app which should notify the battery percentage to user in form of image. Now for battery percentage 0-100, I need 101 images in all. What I want to do is that is it possible to insert all these images in an array and use them accordingly. For instance, if my battery percentage is 47%, the image in array at position 47 should be the resource. That is, suppose I have ImageView named Battery, and I have an array named batteryimage(containing all the 101 images) then I should be able to do this,

Battery.setBackgroundResource(R.drawable.batteryimage[p])

where p will be the current percentage of battery, which I will get by code.

Hope you all understood what I want.. Thanx in advance..

Karyolysis answered 27/6, 2013 at 11:57 Comment(2)
100 images? really? of course it's up to you, but it would be much better to think about reusing one image to display progress.Festive
i was just thinking that, overlay transparent images and just use 0 - 100 % bounds of one of the images, youll be better off memory wise alot more pros than consAnatomical
C
14

define a array of image id like this

int[] p = {R.drawable.image1, R.drawable.image2....}

now for different condition use member of this array like

Battery.setBackgroundResource(p[0])  // or p[1]

or you can use ENUM to make it more readable..

But.. I think you should think in another way. instead of using 100 different image create a custom progressbar to show different state of your battery.

To know the basics of Progress bar see doc here and to create custom progressbar you can check this so question

Chemush answered 27/6, 2013 at 11:59 Comment(1)
Yes and it's worth to note that you can't really do it in other way. Only by manually writing all images' ids.Festive

© 2022 - 2024 — McMap. All rights reserved.