问题描述
我使用 ImageDownloader
类来从服务器映像和获得使用一个ArrayList这些图像的链接。下载图片后,我设置的图像作为布局背景
。所有的工作,但我希望有一个具体的的时间间隔
后改变这些图像,并设置为背景不同的图像。我已经经历了很多帖子了这里,但没有得到我想要的。正如我在ArrayList中的所有图片的链接,所以我怎么能设置一个计时器来改变图像,从的ArrayList
。它到来总是让我在<$ c中的第一图片$ C>指数为零连我都设置一个计时器,但同样的图像再次显示?请帮我,如果有人有任何code为例,看看我的code有什么改变呢?
最后ImagesSerialized项目;
最后ImageView的bgImage =(ImageView的)findViewById(R.id.image);
ArrayList的&LT; ImagesSerialized&GT;清单;
控制=(控制器)getApplicationContext();
名单=(ArrayList中&LT; ImagesSerialized&GT;)control.Table_Images.GetData();
的for(int i = 0; I&LT;则为list.size();我++)
{
项= list.get(ⅰ);
}
下载=新ImageDownloader();
downloader.download(item.imageurl(),bgImage);
我不知道 ImageLoader的
分量,但在一个视图调度计时器是很容易在Android中。 (没有任何额外的对象)
最后ImageView的bgImage =(ImageView的)findViewById(R.id.image);
...
新的Runnable(){
INT updateInterval = 1000; // =1秒
@覆盖
公共无效的run(){
//任何code肚里,这里将被执行每一个updateInterval
//这里改变你的背景
bgImage.postDelayed(这一点,updateInterval);
}
}。跑();
你愿意,可以改变这个模板,假设我想停止这个定时器,用于这个目的,我必须添加一个stop方法我同步的可运行(此停车方法行为,并不会导致不一致的计时器内codeS):
的Runnable myRunnable =新的Runnable(){
INT updateInterval = 1000; // =1秒
布尔停止= FALSE;
公共无效停止(){
this.stop = TRUE;
}
@覆盖
公共无效的run(){
//任何code肚里,这里将被执行每一个updateInterval
//这里改变你的背景
如果(!停止){
bgImage.postDelayed(这一点,updateInterval);
}
}
}。跑();
现在我可以通过 myRunnable.stop()停止;
编辑:你应该重复你的网址阵列,并通过其中的一个到你的下载器。它可以通过这个片段code来实现:
INT ARRAYSIZE =则为list.size();
新的Runnable(){
INT CURRENTINDEX = 0;
INT updateInterval = 1000; // =1秒
@覆盖
公共无效的run(){
CURRENTINDEX + = 1;
如果(CURRENTINDEX == ARRAYSIZE){
CURRENTINDEX = 0;
}
项目= list.get(CURRENTINDEX);
downloader.download(item.imageurl(),bgImage);
bgImage.postDelayed(这一点,updateInterval);
}
}。跑();
I am using ImageDownloader
class to get images from server and getting these images links using an ArrayList. After downloading the Image I am setting the Image as background
of the layout. All is working but I want to change these Images after a specific time interval
and set as background different images. I have gone through many posts here but didn't get what I want. As I have all Images links in ArrayList, so how can I set a timer to change the images, coming from that ArrayList
.It always show me the first Image at index zero
even I have set a timer but the same Image is showing again? Please help me if someone has any code example and see my code what to change there?
final ImagesSerialized item;
final ImageView bgImage=(ImageView) findViewById(R.id.image);
ArrayList<ImagesSerialized> list;
control = (Controller) getApplicationContext();
list = (ArrayList<ImagesSerialized>) control.Table_Images.GetData();
for(int i=0; i<list.size(); i++)
{
item = list.get(i);
}
downloader = new ImageDownloader();
downloader.download(item.imageurl(), bgImage);
I do not know about ImageLoader
component but scheduling a timer on a view is quite easy in Android.(Without any additional Object)
final ImageView bgImage=(ImageView) findViewById(R.id.image);
...
new Runnable() {
int updateInterval = 1000; //=one second
@Override
public void run() {
// Any code which goes here will be executed every 'updateInterval'
// change your background here
bgImage.postDelayed(this, updateInterval);
}
}.run();
You can change this template as you wish, suppose I want to stop this timer, for this purpose I have to add a stop method to my runnable(This stop method acts synchronized and do not cause inconsistency in timer inner codes):
Runnable myRunnable = new Runnable() {
int updateInterval = 1000; //=one second
boolean stop = false;
public void stop() {
this.stop = true;
}
@Override
public void run() {
// Any code which goes here will be executed every 'updateInterval'
// change your background here
if(!stop) {
bgImage.postDelayed(this, updateInterval);
}
}
}.run();
Now I can stop it by myRunnable.stop();
EDIT :You should iterate your array of URLs and pass one of them to your downloader. It can be accomplished by this snippet code:
int arraySize = list.size();
new Runnable() {
int currentIndex = 0;
int updateInterval = 1000; //=one second
@Override
public void run() {
currentIndex += 1;
if(currentIndex == arraySize){
currentIndex = 0;
}
item = list.get(currentIndex);
downloader.download(item.imageurl(), bgImage);
bgImage.postDelayed(this, updateInterval);
}
}.run();
这篇关于Android的:改变图像的时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!