Вопрос

I've searched around stack overflow and yet am still having trouble with this issue.

i want to display a Button on my app, using an AnimationDrawable as my background. I followed the Android Dev page (http://developer.android.com/guide/topics/graphics/drawable-animation.html) and was able to get the animation working. However, my button is entirely hidden. The user can still click it, but the background animation visually hides it, as if there is no button.

I have tried setAlpha, but applying it to my 'AnimationDrawable' blurs the 3 images in my Drawable "roll of film". Also, the screen starts from black and takes a long time to gradually reach the set opacity. It looks at first that it is a memory issue, but as of now, my app is just one Activity with a Button that leads to another Activity.

Does anyone know how to make my Button appear on top of my AnimationDrawable? Or does anyone have experience with setAlpha slowing down your animation?

Here is my code for reference:

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

layout xml (main.xml) code:

 <?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>
Это было полезно?

Решение

Swap the position of <Button> and <ImageView>. Elements added later in a RelativeLayout, each element's z-index is determined by the order which it was added to the container.

<?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" />
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top