[英]Button animation in xml for android
我在活動開始時無法運行按鈕動畫,我在這里有點陌生,任何建議都將不勝感激。 這是我開始活動時所擁有的.....
// 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>
有關AnimationDrawable#start()
的API文檔 ,明確聲明不應從onCreate(Bundle)
調用它,因為AnimationDrawable
尚未附加到窗口。 請遵循文檔中的建議,然后從onWindowFocusChanged(boolean)
調用start()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.