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