我正在尝试为我的应用程序构建一个启动屏幕。当我试图更改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);
}
我怎样才能纠正这个奇怪的错误?
最佳答案
我试过你的代码,没有问题。当您更改任何视图时,必须在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
}
});
关于android - 只有创建 View 层次结构的原始线程才能触摸其 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13061174/