只有创建视图层次结构的原始线程才能触及其视图

时间:2012-10-25 03:35:10

标签: android

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

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

@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);

    }

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

1 个答案:

答案 0 :(得分: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
    }
});