-2

我有一个 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 库可以帮助我以更简单的方式做到这一点?

4

1 回答 1

1

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

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

在此处阅读有关为什么使用处理程序而不是线程的更多信息:http: //shenhengbin.wordpress.com/2012/08/22/androidupdate-ui-through-handler/

于 2013-03-13T21:53:40.447 回答