我正在尝试制作一个能够连续显示图像(每秒4或5次)的应用程序,因此我需要更新保存在imageview对象中的图像,如果我通过按下按钮触发功能,它就会工作,每次按下按钮,下一个图像就会显示:
int i = 0;
public void buttonPressed(View view) {
ImageView image = (ImageView) findViewById(R.id.imageView5);
String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
File imgFile = new File(path);
bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
image.setImageBitmap(bMap);
i++;
}
但是,如果我尝试在同一个父函数中多次调用相同的函数,或者尝试在循环中运行它们,则在loopfunc()完成后,将根本不加载第一个图像,而最终只显示最后一个图像:
public void loopFunc() {
ImageView image = (ImageView) findViewById(R.id.imageView5);
for (i = 1; i < 3; i++) {
String path = String.format("/storage/emulated/0/Download/%d.bmp", i);
File imgFile = new File(path);
bMap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
image.setImageBitmap(bMap);
//wait some time or do other things...
}
}
简单地说,我需要能够执行 image.setImageBitmap(bMap);
从代码而不是从按钮有人知道怎么做?谢谢
4条答案
按热度按时间ryhaxcpt1#
当您在循环中更改图像路径时,到达最后一个路径只需几毫秒。您需要使用sleep()函数来停留一段时间。
rvpgvaaj2#
根本原因
因为循环在很短的时间内运行得太快,所以您看到的第一个图像根本不显示,而只有最后一个图像在之后才最终显示
loopFunc()
完整的。解决方案
若要每秒连续显示一系列图像,则应使用处理程序。
ig9co6j13#
问题在于你用来计算其他东西的时间。里面有你写的延迟函数代码吗。
qoefvg9y4#
试试这个。只需创建一个函数,并使用某个线程或处理程序调用oncreate方法。
每200米后,你的循环就开始工作。