使用thread.sleep()为Android UI组件设置动画(此处为ImageView)

时间:2013-03-13 21:50:24

标签: android android-layout imageview

我有一个Android应用程序,我希望使用以下代码在启动画面上逐帧移动imageView:

ImageView iv = (ImageView) findViewById(R.id.myimage);
try {
    Thread.sleep(2000); //giving a 2s wait
} catch (InterruptedException e1) {
    e1.printStackTrace();
}
for(int i=0; i<100; i++){
    iv.setPadding(0, 0, 0, i*2); //increasing the bottom padding each 50ms
    try {
        Thread.sleep(50);
    } catch (Exception e) {

    }
}

但是这段代码会将屏幕空白为(2000 + 100*50) milliseconds,然后显示具有图像最终位置的应用程序(即不显示动画)。 我怎么能得到动画?另外,是否有任何用于Android的UI库可以帮助我以更简单的方式执行此操作?

1 个答案:

答案 0 :(得分:1)

而不是使用线程, 使用处理程序:

new Handler().postDelayed(new Runnable(){...RUNNING CODE GOES HERE....}, 2000)

详细了解为何使用处理程序而非线程: http://shenhengbin.wordpress.com/2012/08/22/androidupdate-ui-through-handler/

相关问题