我有一个 imageView 并希望它像这样工作:
ImageViewer 可见
5 秒暂停
图像视图不可见
5 秒暂停
ImageViewer 可见
等等 ...
我怎么做?我试过 sleep 但它会在 5 秒内冻结整个程序。我只想影响我的 imageView。
我不是Android程序员,但是,作为一般建议,我会说你应该在另一个线程上执行睡眠,最好说等待,并在等待期结束时在主线程上执行一个方法切换图像视图的可见性。
进入更具体的细节,我想说您必须使用 Handler 对象,因为您无法在单独的线程中更新大多数 UI 对象。当您向 Handler 发送消息时,它将被保存到队列中并由 UI 线程尽快执行:
public class MyActivity extends Activity {
// Handler needed for callbacks to the UI thread
final Handler mHandler = new Handler();
// Create runnable for posting
final Runnable mUpdateUIState = new Runnable() {
public void run() {
updateUIState();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
[ . . . ]
}
protected void startToggle() {
// Fire off a thread to do the waiting
Thread t = new Thread() {
public void run() {
Thread.Sleep(5000);
mHandler.post(mUpdateUIState);
}
};
t.start();
}
private void updateUiState() {
// Back in the UI thread -- toggle imageview's visibility
imageview.setVisibility(1 - imageview.getVisibility());
}
}
或者,一个较短版本的片段,
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
imageview.setVisibility(1 - imageview.getVisibility());
}
}, 5000);
使用该postDelayed
方法,该方法将延迟合并到消息发布逻辑中。
在AlphaAnimation上ImageView
使用 10 秒的持续时间,从 alpha 100 到 0 再回到 100。然后使用INFINITE的重复计数。ImageView
您可以使用插值器在出现或消失时产生令人愉悦的效果。