0

当我的活动开始时,我无法让我的按钮动画运行,我在这里有点新,任何建议都将不胜感激。这是我在活动开始时所拥有的......

    // this is flag for home key press
public static boolean isHome = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.press);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);



    // init data
    init();

    // start player display
    start(10000 * 60 * 60 * 24);

    spin++;

    // play background music
    playmp3(R.raw.press);
 }
//HERE IS THE WORKING VERSION THAT WAS CORRECTED BY THE ANSWER BELOW
 public void onWindowFocusChanged(boolean hasFocus) {
     super.onWindowFocusChanged(hasFocus);

     if(hasFocus) {
     final Button b_1 = (Button) findViewById(R.id.bttest);
         b_1.setBackgroundResource(R.drawable.animation);
         AnimationDrawable b1Amin = (AnimationDrawable) b_1.getBackground();
     b1Amin.start();
    }
 }

这是我的可绘制文件中的animation.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  android:oneshot="false">
<item android:drawable="@drawable/btn1" android:duration="100" />
<item android:drawable="@drawable/btn2" android:duration="100" />
<item android:drawable="@drawable/btn3" android:duration="100" />
<item android:drawable="@drawable/btn4" android:duration="100" />
<item android:drawable="@drawable/btn5" android:duration="100" />
<item android:drawable="@drawable/btn6" android:duration="100" />
<item android:drawable="@drawable/btn7" android:duration="100" />
<item android:drawable="@drawable/btn8" android:duration="100" />

</animation-list>
4

1 回答 1

0

API 文档AnimationDrawable#start()明确指出不应调用它,因为onCreate(Bundle)尚未AnimationDrawable将其附加到窗口。遵循文档中的建议并调用start()from onWindowFocusChanged(boolean)

于 2012-08-02T19:13:26.623 回答