Android AnimationDrawable 在返回主屏幕时停止

发布于 2024-12-10 21:09:16 字数 2086 浏览 4 评论 0原文

我正在 Android 中开发一款游戏,其中启动器屏幕上有 2 个 AnimationDrawables。

这些动画一起运行得很好,但是当我离开屏幕然后返回到屏幕时,其中一个动画停止,另一个动画继续。

我的代码如下:

  static AnimationDrawable animation;
  static AnimationDrawable animation2;
 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

animation = new AnimationDrawable();
  animation.addFrame(getResources().getDrawable(R.drawable.j0), 200);
  animation.addFrame(getResources().getDrawable(R.drawable.j1), 200);
  animation.addFrame(getResources().getDrawable(R.drawable.j2), 200);
  animation.addFrame(getResources().getDrawable(R.drawable.j3), 200);
  animation.setOneShot(false);

  animation2 = new AnimationDrawable();
  animation2.addFrame(getResources().getDrawable(R.drawable.logo), 250);
  animation2.addFrame(getResources().getDrawable(R.drawable.logo1), 250);
  animation2.setOneShot(false);

ImageView imageAnim =  (ImageView) findViewById(R.id.imageView1);
  imageAnim.setBackgroundDrawable(animation);
  imageAnim.post(new MenuAnimate());

 ImageView imageAnima =  (ImageView) findViewById(R.id.imageView2);
      imageAnima.setBackgroundDrawable(animation2);
      imageAnima.post(new MenuAnimate2());


class MenuAnimate implements Runnable {

public void run() {
    Menu.animation.start();
}
}

class MenuAnimate2 implements Runnable {

public void run() {
    Menu.animation2.start();
}
}

Xml 'menu':

   <LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="wrap_content" android:weightSum="1" android:layout_weight="0.93">
     <ImageView android:id="@+id/imageView1" android:layout_height="match_parent" android:layout_width="wrap_content"></ImageView>
     <ImageView android:id="@+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="bottom"></ImageView>
</LinearLayout>

如果有人有任何想法,那就太棒了。

I am developing a game in Android in which the launcher screen has 2 AnimationDrawables on it.

The animations run fine together, however when I navigate away from the screen and then hit back to return to it, one of the animations stops, and the other continues.

My code is below:

  static AnimationDrawable animation;
  static AnimationDrawable animation2;
 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

animation = new AnimationDrawable();
  animation.addFrame(getResources().getDrawable(R.drawable.j0), 200);
  animation.addFrame(getResources().getDrawable(R.drawable.j1), 200);
  animation.addFrame(getResources().getDrawable(R.drawable.j2), 200);
  animation.addFrame(getResources().getDrawable(R.drawable.j3), 200);
  animation.setOneShot(false);

  animation2 = new AnimationDrawable();
  animation2.addFrame(getResources().getDrawable(R.drawable.logo), 250);
  animation2.addFrame(getResources().getDrawable(R.drawable.logo1), 250);
  animation2.setOneShot(false);

ImageView imageAnim =  (ImageView) findViewById(R.id.imageView1);
  imageAnim.setBackgroundDrawable(animation);
  imageAnim.post(new MenuAnimate());

 ImageView imageAnima =  (ImageView) findViewById(R.id.imageView2);
      imageAnima.setBackgroundDrawable(animation2);
      imageAnima.post(new MenuAnimate2());


class MenuAnimate implements Runnable {

public void run() {
    Menu.animation.start();
}
}

class MenuAnimate2 implements Runnable {

public void run() {
    Menu.animation2.start();
}
}

Xml 'menu':

   <LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="wrap_content" android:weightSum="1" android:layout_weight="0.93">
     <ImageView android:id="@+id/imageView1" android:layout_height="match_parent" android:layout_width="wrap_content"></ImageView>
     <ImageView android:id="@+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="bottom"></ImageView>
</LinearLayout>

If anyone has any idea it would be amazing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文