-1

我必须重复使用 Thread 和 AnimationDrawable 的图像序列,但它不能连续工作。在通过按钮单击事件启动下一个活动之前,我不想停止此动画。

这是我的java代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);{

final ImageView splashImage=(ImageView)findViewById(R.id.heartFunction);
     splashImage.setBackgroundResource(R.drawable.slide_right);
  splashAnimation = (AnimationDrawable) splashImage.getBackground();
}




public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if ( isFocused ) {
        //isFocused = false;

        splashAnimation.start();
        var=false;
        new Thread(new Runnable() {
        public void run() {
            try {
                Thread.sleep(SPLASH_DISPLAY_LENGTH);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

slide_right.xml:-

<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">

<item android:drawable="@drawable/heartcolored0" android:duration="200" />
<item android:drawable="@drawable/heartcolored2" android:duration="200" />
<item android:drawable="@drawable/heartcolored4" android:duration="200" />
<item android:drawable="@drawable/heartcolored5" android:duration="200" />
<item android:drawable="@drawable/heartcolored6" android:duration="200" />
<item android:drawable="@drawable/heartcolored7" android:duration="200" />
<item android:drawable="@drawable/heartcolored8" android:duration="200" />
<item android:drawable="@drawable/heartcolored9" android:duration="200" />
<item android:drawable="@drawable/heartcolored10" android:duration="200" />
<item android:drawable="@drawable/heartcolored11" android:duration="200" />
<item android:drawable="@drawable/heartcolored12" android:duration="200" />
<item android:drawable="@drawable/heartcolored13" android:duration="200" />

</animation-list>
4

1 回答 1

2

如果您希望动画连续运行,则需要设置android:oneshot="false"

你之前说只跑一次。

如果您希望动画一直运行,直到您单击屏幕转到下一个活动。onWindowFocusChanged 函数时启动动画

@Override
public void onWindowFocusChanged(boolean hasFocus){
    splashanimation.start();
}

然后使用 onTouchEvent 来捕捉触摸,开始一个新的活动并完成旧的活动。

@Override
public boolean onTouchEvent(MotionEvent event){
     if (event.getAction() == MotionEvent.ACTION_DOWN) {
          Intent i = new Intent(Anim.this, Main.class);
          startActivity(i);
          finish();
     }
return true;
}

希望这会有所帮助,您的问题很难阅读/理解。

于 2010-07-26T16:33:12.377 回答