Android 以特定间隔更改背景
我必须每30秒更改一次背景图像,但我没有得到正确的结果 我试过线程和TimerTask。但是没有工作。 我有端口1、端口2…等图像 在TimerTask中,背景已消失Android 以特定间隔更改背景,android,android-animation,timertask,Android,Android Animation,Timertask,我必须每30秒更改一次背景图像,但我没有得到正确的结果 我试过线程和TimerTask。但是没有工作。 我有端口1、端口2…等图像 在TimerTask中,背景已消失 class Task1 extends TimerTask { public void run() { System.out.println("Checking a"); Random r = new Random(); int i = r.nextInt(3) + 1;
class Task1 extends TimerTask {
public void run() {
System.out.println("Checking a");
Random r = new Random();
int i = r.nextInt(3) + 1;
rl.setBackgroundResource(getResources().getIdentifier(
"port" + i, "drawable","com.samcom.breakingdowncd"));
}
}
Timer timerBackground = new Timer();
timerBackground.scheduleAtFixedRate(new Task1(), 0, 30000);
它不起作用,任何帮助都将不胜感激。
谢谢你使用这个,它很管用
private static final long GET_DATA_INTERVAL = 1000;
int images[] = {R.drawable.cloud1,R.drawable.cloud2};
int index = 0;
ImageView img;
Handler hand = new Handler();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
img = (ImageView) findViewById(R.id.image);
hand.postDelayed(run, GET_DATA_INTERVAL);
}
Runnable run = new Runnable() {
@Override
public void run() {
img.setBackgroundDrawable(getResources().getDrawable(images[index++]));
if (index == images.length)
index = 0;
hand.postDelayed(run, GET_DATA_INTERVAL);
}
};
XML代码:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/line">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/image"
/>
</LinearLayout>
试试这个:
Runnable run = new Runnable() {
@Override
public void run() {
if (index < images.length){
img.setBackgroundDrawable(getResources().getDrawable(images[index++]));}
else{
index = 0;
}
hand.postDelayed(run, GET_DATA_INTERVAL);
}
};
Runnable run=new Runnable(){
@凌驾
公开募捐{
如果(索引
这是正确的