3

我正在尝试为我的应用程序构建启动画面。当我尝试更改我的 imagesview 属性并设置动画时,我收到一个错误。

我已经测试了 runonuithread 和 Hadler,但是在这 2 种情况下,没有错误,屏幕上也没有显示任何内容,并且在延迟(正是我在代码中指定的内容)之后,程序活动切换到下一个活动,这是我的代码:

@Override
public void run(){
    try {
            TranslateAnimation moveLefttoRight = new TranslateAnimation(500, 0, 0, 0);
            moveLefttoRight.setDuration(5000);
            moveLefttoRight.setFillAfter(true);
            im.startAnimation(moveLefttoRight);//im is an image view
            SystemClock.sleep(6000);
            TranslateAnimation moveRighttoLeft = new TranslateAnimation(0, 100, 0, 0);
            moveRighttoLeft.setDuration(1000);
            moveRighttoLeft.setFillAfter(true);
            im.startAnimation(moveRighttoLeft);
            SystemClock.sleep(6000);
            ScaleAnimation scale = new ScaleAnimation(0.3f, 1f, 0.3f, 1f, 
                    ScaleAnimation.RELATIVE_TO_SELF, 0f, 
                    ScaleAnimation.RELATIVE_TO_SELF, 0f);
            scale.setDuration(1000);
            scale.setFillAfter(true);
            // Wait given period of time or exit on touch

            sig.startAnimation(scale);//sig is an image view

            SystemClock.sleep(2000);

    }

我该如何解决这个奇怪的错误?

4

1 回答 1

0

我试过你的代码,没有问题。当您更改任何视图时,您必须在 UI/主线程上进行。你可以试试我编辑的代码:

TranslateAnimation moveLefttoRight = new TranslateAnimation(500, 0, 0,
        0);
moveLefttoRight.setDuration(5000);
moveLefttoRight.setFillAfter(true);
bt1.startAnimation(moveLefttoRight);// im is an image view

moveLefttoRight.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        doAnim2(); // do the next animation
    }
});
于 2012-10-25T04:35:14.297 回答