1

当开发选项不保留活动打开时,我有一个运行良好的应用程序。

什么时候关闭,

每次应用程序进入后台并恢复时,都会再次调用 onCreate 函数。在那里,我正在重新创建最后一个应用程序状态。

现在的问题是,发生在用户操作上的简单动画不会启动,并且动画处理程序从未调用过。

似乎动画被忽略了。只有当活动被杀死并再次创建时才会发生这种情况。

最奇怪的部分是我有 4 个动画,2 个用于一个图像视图(打开、关闭)和 2 个用于另一个图像视图(打开、关闭)。

这仅发生在开场动画上。

你能帮帮我吗?

动画 xmls(用于打开/关闭动画之一)

红色显示动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator">
  <alpha
      android:fromAlpha="0.1"
      android:toAlpha="1.0"
      android:duration="800"
      /> 
  <scale android:fromXScale="0.0"
      android:toXScale="1.0"
      android:fromYScale="0.0"
      android:toYScale="1.0"
      android:pivotX="50%"
      android:pivotY="50%"
      android:duration="800">

  </scale>
</set>

红色隐藏动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator">
  <alpha
      android:fromAlpha="1.0"
      android:toAlpha="0.0"
      android:duration="300"
      />
  <scale android:fromXScale="1.0"
      android:toXScale="0.0"
      android:fromYScale="1.0"
      android:toYScale="0.0"
      android:pivotX="50%"
      android:pivotY="50%"
      android:duration="300">

  </scale>
</set>

我正在初始化东西的代码(在onCreate)

red = (ImageView) findViewById(R.id.red);

redHide = AnimationUtils.loadAnimation(this, R.anim.red_hide);
redHide.setFillAfter(true);
redHide.setAnimationListener(this);

redShow = AnimationUtils.loadAnimation(this, R.anim.red_show);          
redShow.setFillAfter(true);
redShow.setAnimationListener(this);

我正在调用的处理程序和方法

private void showRed() {
    red.startAnimation(redShow); //this is the one that is not happening
}

private void hideRed() {
     red.startAnimation(redHide);
}

@Override
public void onAnimationEnd(Animation a) {   
}

@Override
public void onAnimationRepeat(Animation a) {
}

@Override
public void onAnimationStart(Animation a) {
}

G

4

1 回答 1

4

Things you could try are:

  • init the animations in onResume() instead.
  • Call clearAnimation() before you start each animation.
  • Try to animate without fillAfter(), maybe the animation has been locked to it's latest state somehow.

Just some ideas..

A blinking red warning lamp went off when you wrote that you are recreating the last app state since the code you are showing to us looks just fine.

于 2012-11-21T15:48:22.233 回答