使用 Thread 使 ImageView 数组中的元素可见
Make elements in ImageView array visible using Thread
我有一个 ImageView
的数组,其中包含许多我想按顺序显示的不可见元素(即,使第一个可见,等待半秒钟,使下一个可见,然后依此类推)使用 Thread
或 runOnUiThread
.
我试过以下方法:
Thread th = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
th.start();
和
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
但是我没有得到想要的效果。发生的情况是所有图像同时可见,而不是一个接一个地显示。
我该如何解决这个问题?
int counter=1; //Global variable
private void visibleImageview()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(counter==1)
{
im1.setVisibility(View.VISIBLE);
}
else if(counter==2)
{
im2.setVisibility(View.VISIBLE);
}
else if(counter==3)
{
im3.setVisibility(View.VISIBLE);
}
++counter;
if(couter<=3)
visibleImageview();
}
}, 3000);
}
解决了。这就是我设法做到的:
Handler handler = new Handler();
im1.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im2.setVisibility(View.VISIBLE);
}
});
}
},500);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im3.setVisibility(View.VISIBLE);
}
});
}
},1000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},1500);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},2000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bottom.setVisibility(View.VISIBLE);
}
});
}
},3000);
我有一个 ImageView
的数组,其中包含许多我想按顺序显示的不可见元素(即,使第一个可见,等待半秒钟,使下一个可见,然后依此类推)使用 Thread
或 runOnUiThread
.
我试过以下方法:
Thread th = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
th.start();
和
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
但是我没有得到想要的效果。发生的情况是所有图像同时可见,而不是一个接一个地显示。
我该如何解决这个问题?
int counter=1; //Global variable
private void visibleImageview()
{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(counter==1)
{
im1.setVisibility(View.VISIBLE);
}
else if(counter==2)
{
im2.setVisibility(View.VISIBLE);
}
else if(counter==3)
{
im3.setVisibility(View.VISIBLE);
}
++counter;
if(couter<=3)
visibleImageview();
}
}, 3000);
}
解决了。这就是我设法做到的:
Handler handler = new Handler();
im1.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im2.setVisibility(View.VISIBLE);
}
});
}
},500);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im3.setVisibility(View.VISIBLE);
}
});
}
},1000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},1500);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},2000);
handler.postDelayed(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bottom.setVisibility(View.VISIBLE);
}
});
}
},3000);