如何使图像视图可见,5秒暂停,不可见,5秒暂停等等

时间:2010-09-19 09:11:15

标签: android imageview visible invisible

我有一个imageView,希望它像这样工作:

ImageViewer可见

暂停5秒

图片视图不可见

暂停5秒

ImageViewer可见

依旧......

我该怎么做?我试过睡觉,但它在5秒内冻结了整个程序。我只是想影响我的imageView。

2 个答案:

答案 0 :(得分:2)

我不是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方法,该方法在消息发布逻辑中包含延迟。

答案 1 :(得分:2)

ImageView上使用AlphaAnimation,持续时间为10秒,从alpha 100到0再返回100。 然后使用重复计数INFINITE。 <{1}}出现或消失时,您可以使用插值器产生令人愉快的效果。

相关问题