随机改变Android的背景图片 [英] Randomly change background image in Android

查看:636
本文介绍了随机改变Android的背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的测验应用的活动,我想改变它的背景每隔5秒。我如何可以随机图像在我的绘制文件夹,使他们我的活动的背景图像。注意:这是只有一个单一的活性。

I have an activity in my quiz application and I want to change its background every 5 seconds. How can I randomize the images in my drawable folder and make them the background images of my activity. Note: It is only a single activity.

任何帮助将是很大的AP preciated。谢谢你。

Any help would be greatly appreciated. Thank you.

推荐答案

您可以创建可绘制的数组是这样的:

You can create an array of your drawables like this:

<array name="myImages">
       <item>@drawable/img1</item>
       <item>@drawable/img2</item>
       <item>@drawable/img3</item>
</array>

您可以让他们像这样的:

You can get them like this:

Resources res = getResources();
TypedArray myImages = res.obtainTypedArray(R.array.myImages);

现在创建随机数:

Random r = new Random(myImages.length())
int i = r.nextInt();

Drawable drawable = icons.getDrawable(i);

现在将它们设置为背景每隔5秒(如在ρяσѕρєяs例子)。

Now set them as your background every 5 seconds(like in ρяσѕρєяs example).

这篇关于随机改变Android的背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆