简体   繁体   English

ArrayList的随机背景<Drawable>

[英]Random background from ArrayList<Drawable>

in my app i have this part of code for set a background image to a button! 在我的应用程序中,我具有这部分代码,用于将背景图像设置为按钮!

private int [] [] double;
private List<Drawable> images;
----------------------------------------------------------
images = new ArrayList<Drawable>();

images.add(getResources().getDrawable(R.drawable.img1));
images.add(getResources().getDrawable(R.drawable.img2));
images.add(getResources().getDrawable(R.drawable.img3));
images.add(getResources().getDrawable(R.drawable.img4));
images.add(getResources().getDrawable(R.drawable.img5));
images.add(getResources().getDrawable(R.drawable.img6));
images.add(getResources().getDrawable(R.drawable.img7));
images.add(getResources().getDrawable(R.drawable.img8));
-----------------------------------------------------------
button.setBackgroundDrawable(images.get(double[x][y]));

Now i' im trying to set random image to this button from this ArrayList<Drawable>. 现在,我正在尝试从此ArrayList<Drawable>.将此按钮设置为随机图像ArrayList<Drawable>. Is there someone who has some helpful tip for me? 有没有对我有用的提示的人?

Yeah you should make an arraylist of Integers and put the drawable id in there and access it with setBackgroundResource. 是的,您应该创建一个整数数组列表,并将可绘制ID放在其中,并使用setBackgroundResource访问它。

Example: 例:

private List<Integer> images;

images = new ArrayList<Integer>();

images.add(Integer.valueOf(R.drawable.img1));
button.setBackgroundResource(images.get(double[x][y]).intValue());

This way it will consume less memory since now you are only storing Integers rather than Image Drawables, and it will be more efficient. 这样,它将消耗更少的内存,因为现在您只存储Integers而不是Image Drawables,并且效率更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM