我怎样才能让一个ImageView的可见,5秒暂停,看不见的,5秒暂停等
问题描述:
我有一个ImageView的,并希望它是这样工作的:我怎样才能让一个ImageView的可见,5秒暂停,看不见的,5秒暂停等
图像浏览可见
5秒的暂停
图像视图无形
5秒暂停
图像浏览器可见
依此类推...
我该怎么做?我尝试了睡眠,但它在5秒内冻结了整个程序。我只是想影响我的imageView。
答
我不是一个Android程序员,但作为一般性建议,我会说你应该在等待期结束时执行睡眠,更好地说等待,在另一个线程上执行,并在主要线程,这是一种切换imageview可见性的方法。
进入更具体的细节,我会说你必须使用Handler对象,因为你不能在单独的线程中更新大多数UI对象。当您发送一条消息给处理程序将得到保存到队列中,并获得由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
方法,该方法在消息发布逻辑中包含延迟。
答
在ImageView
上使用AlphaAnimation,持续时间为10秒,从alpha 100到0再次回到100。 然后使用INFINITE的反应计数。 当ImageView
出现或消失时,可以使用插补器产生令人愉快的效果。