0

我已經搜索了周圍的堆棧溢出,但仍然遇到了這個問題。根據按鈕繪製動畫

我想在我的應用程序上顯示一個Button,使用AnimationDrawable作爲我的背景。我遵循了Android開發者頁面(http://developer.android.com/guide/topics/graphics/drawable-animation.html)並能夠獲得動畫效果。但是,我的按鈕完全隱藏。用戶仍然可以點擊它,但背景動畫可以隱藏它,就好像沒有按鈕一樣。

我試過setAlpha,但將它應用到我的'AnimationDrawable'模糊了我的Drawable「電影膠捲」中的3張圖像。此外,屏幕從黑色開始,需要很長時間才能逐漸達到設定的不透明度。它首先看到它是一個內存問題,但截至目前,我的應用程序只是一個ActivityButton導致另一個Activity

有誰知道如何讓我的Button出現在我的AnimationDrawable之上?或者是否有人使用setAlpha減慢動畫速度?

這裏是我的參考代碼:

public class Main extends Activity { 
Button start; 
Intent intent= new Intent(); 
ImageView background; 
AnimationDrawable animation; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    background= (ImageView) findViewById(R.id.imagehost); 
    background.setBackgroundResource(R.drawable.anim); 
    animation= (AnimationDrawable) background.getBackground(); 


    start= (Button) findViewById(R.id.start); 

    start.setOnClickListener(new OnClickListener(){ 
     ... 


} //end of onCreate 

@Override 
public void onWindowFocusChanged(boolean hasFocus){ 
    super.onWindowFocusChanged(hasFocus); 
    animation.start(); 
    //animation.setAlpha(50); //doesn't help 

}//end of onWindowFocusChanged 

EDIT

佈局XML(main.xml中)的代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/start" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/start" 
     android:textSize="40sp" /> 

    <ImageView 
     android:id="@+id/imagehost" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="58dp" /> 

animationdrawable.xml:

<?xml version="1.0" encoding="utf-8"?> 

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:drawable="@drawable/bgnd1" 
    android:duration="600" /> 
<item android:drawable="@drawable/bgnd2" 
    android:duration="600" /> 
<item android:drawable="@drawable/bgnd3" 
    android:duration="600" /> 


</animation-list> 
+0

你能告訴我們你的佈局xml嗎? – caiocpricci2

+0

是的,只是加了它們! – Bethany

+0

爲什麼不把動畫可繪製應用到按鈕bg? – Leonidos

回答

1

更換<Button><ImageView>的位置。元素稍後在RelativeLayout中添加,每個元素的z-index由它添加到容器的順序決定。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" >  

<ImageView 
    android:id="@+id/imagehost" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="58dp" /> 
<Button 
    android:id="@+id/start" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/start" 
    android:textSize="40sp" /> 
+0

感謝您的答案!我不知道關於相關佈局。不幸的是我的app部隊關閉了。 logcat拋出我這個錯誤: 「無法啓動活動組件信息... java.lang.ClassCastException:android.widget.Button不能轉換爲android widget.ImageView」 任何想法? – Bethany

+0

仔細檢查xml上的id。你混合他們。 – caiocpricci2

+0

事實證明,我只需要清理項目!非常感謝! – Bethany