我有imageview,我必须显示不同的图像 有时间间隔,但我改变ImageResource 显示最后一张图片
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test);
try {
Thread.sleep(2000) ;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);
}
请建议正确的方法
先谢谢
答案 0 :(得分:0)
也许这个主题正是你要找的:
答案 1 :(得分:0)
每当您想要更新用户界面而不执行任何操作或事件时,都应该使用处理程序。
这是示例代码
Main.java
package com.balaji.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
private ImageView txtStatus;
int i=0;
int imgid[]={R.drawable.icon,R.drawable.back,R.drawable.slider,R.drawable.forward};
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler {
@Override
public void handleMessage(Message msg) {
Main.this.updateUI();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
private void updateUI(){
//int currentInt = Integer.parseInt((String) txtStatus.getText()) + 10;
if(i<imgid.length){
mRedrawHandler.sleep(1000);
txtStatus.setBackgroundResource(imgid[i]);
i++;
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
this.txtStatus = (ImageView)this.findViewById(R.id.txtStatus);
updateUI();
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</ImageView>
</RelativeLayout>