我是Android App Development的新手。我只是想要一些我想要的应用程序的帮助。 我使用AsyncTask来运行Timer,但是在计时器运行后,图像将被更改。
class syncTask扩展了AsyncTask {
@Override
protected Void doInBackground(Void... arg0)
{
Thread timer=new Thread(){
public void run(){
try{
sleep(3000);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
};
timer.start();
return null;
}
@Override
protected void onPostExecute(Void result)
{
q=images[random.nextInt(images.length)];
frame.setImageResource(q);
w=q;
}
}
我该怎么做才能更新imageview。请帮助!!
答案 0 :(得分:0)
不完全确定这是否有效,但我稍后会在eclipse中检查它。将此课程放在您的活动中。例如:
public class AndroidActivity extends Activity {
private ImageView frame;
private Image[] images = {blah, blah, blah};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
frame = (ImageView)findViewById(R.id.frame);
syncTask task = new syncTask();
task.execute();
}
private class syncTask extends AsyncTask <Void, Integer, Void> {
@Override
protected int doInBackground(Void...params) {
while(true) {
publishProgress(random.nextInt(images.length));
sleep(3000);
}
}
@Override
protected void onProgressUpdate(Integer...pictureIndex) {
frame.setImageResource(images[pictureIndex]);
}
@Override
protected void onPostExecute(Void...params) {
}
}
答案 1 :(得分:0)
更好的是,使用TimerTask:
public class AndroidActivity extends Activity {
private ImageView frame;
private Image[] images = {blah, blah, blah};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
frame = (ImageView)findViewById(R.id.frame);
syncTask task = new syncTask();
Timer timer= new Timer();
task.schedule(timer, 0, 3000);;
}
private class syncTask extends TimerTask {
public void run() {
frame.setImageResource(images[random.nextInt(images.length)]);
}
}
}