Repeat the animated background

Asked

Viewed 360 times

1

I made an app with an animated background, here’s the code:

Main layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >
...

The animation list:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/matrix_39" android:duration="100" />
<item android:drawable="@drawable/matrix_38" android:duration="100" />
<item android:drawable="@drawable/matrix_37" android:duration="100" />
<item android:drawable="@drawable/matrix_36" android:duration="100" />
...

The java implementation:

public class Main extends Activity {
    LinearLayout bck;
    AnimationDrawable ad;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bck = (LinearLayout) findViewById(R.id.background);
        bck.setBackgroundResource(R.drawable.progress_animation);
        ad = (AnimationDrawable) bck.getBackground();
        ad.start();
}

Well, it works great as a full screen animated backdrop, but I’d like to repeat the animated background image. There’s a way?

1 answer

1

If I understand your question correctly what you have to do is add ad.setOneShot(false) before making ad.start().

public class Main extends Activity {
    LinearLayout bck;
    AnimationDrawable ad;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bck = (LinearLayout) findViewById(R.id.background);
        bck.setBackgroundResource(R.drawable.progress_animation);
        ad = (AnimationDrawable) bck.getBackground();
        ad.setOneShot(false);
        ad.start();
}  

Another thing I read in documentation is that start() should not be done in the onCreate method of Activity but rather in onWindowFocusChanged

Browser other questions tagged

You are not signed in. Login or sign up in order to post.