Java 在特定时间内停止for循环并在Glide中调整图像
我正在生成截击请求,从服务器获取图像并显示在Glide中。我使用for循环解析响应并在Glide中显示图像。我想显示图像一,5/10秒后,我想用索引更改它。我面临的问题是,当for循环开始显示最后一个索引上的图像时。 mu代码为:Java 在特定时间内停止for循环并在Glide中调整图像,java,android,multithreading,android-glide,Java,Android,Multithreading,Android Glide,我正在生成截击请求,从服务器获取图像并显示在Glide中。我使用for循环解析响应并在Glide中显示图像。我想显示图像一,5/10秒后,我想用索引更改它。我面临的问题是,当for循环开始显示最后一个索引上的图像时。 mu代码为: StringRequest fetchingAddsForMainScreen= new StringRequest(URL, new Response.Listener<String>() { @Override pu
StringRequest fetchingAddsForMainScreen= new StringRequest(URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
int counter=0;
try {
JSONObject object= new JSONObject(response);
JSONArray jsonArray=object.getJSONArray("slots");
for (int i=0;i<jsonArray.length();i++)
{
JSONObject jsonObject=jsonArray.getJSONObject(i);
String packageFullName=jsonObject.getString("adurl");
System.out.println("Package Full Name is:"+packageFullName);
String packageName = packageFullName.substring(packageFullName.indexOf("?")+3, packageFullName.length() );
System.out.println("Package Name is:"+packageName);
Glide.with(getApplicationContext()).load(jsonObject.getString("imgurl")).into(iVAddOne);
counter++;
if (counter==1)
{
Toast.makeText(MainActivity.this, "one Time counter", Toast.LENGTH_SHORT).show();
//break;
}
else if (counter==2)
{
Toast.makeText(MainActivity.this, "Two Time counter", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "many Time counter", Toast.LENGTH_SHORT).show();
}
StringRequest fetchingAddsForMainScreen=newstringrequest(URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
int计数器=0;
试一试{
JSONObject对象=新的JSONObject(响应);
JSONArray JSONArray=object.getJSONArray(“插槽”);
for(inti=0;i简单的hack是在并行线程中运行for循环,并在每次迭代结束时调用sleep(5000)
更复杂的解决方案是使用更优雅、更适合android,这样您就可以设置一个重复任务,每5秒钟更改一次图像。这可能会有所帮助
int CODE_PAUSE_TIME = 3000; //IN MILLISEC
for(int i = 0; i<10; i++){
//Write your code here before there is a pause
try{
Thread.sleep(CODE_PAUSE_TIME);
}catch(Exception e){
//write your code here after there is a pause.
}
}
int code\u PAUSE\u TIME=3000;//以毫秒为单位
对于(int i=0;i