我正在尝试为我的应用构建启动画面。当我尝试更改我的 imagesview 属性并设置动画时,我收到错误消息。
我已经测试过 runonuithread 和 Hadler,但在这两种情况下没有错误,屏幕上也没有显示任何内容,并且在延迟(正是我在代码中指定的内容)后程序 Activity 切换到下一个 Activity ,这是我的代码:
@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);
}
如何解决这个奇怪的错误?
最佳答案
我试过你的代码,没有问题。当您更改任何 View 时,您必须在 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/