2

I am writing a splash screen in android 2.1. I want 20 png images to be shown in an order at the splash screen. I tried to use animation-list but couldn't manage to do it.

Here is my code. Now, there is an image called firstLoadingImage.png. Only that is shown for 5000ms. Then the myappactivity starts. However, I couldn't manage to update the image source during this waiting time. How do you think I can do this?

SplashScreenActivity

package myapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

public class SplashScreenActivity extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 5000; // time to display the splash screen in ms

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);

        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    startActivity(new Intent(SplashScreenActivity.this, MyAppActivity.class));
                    stop();
                }
            }
        };
        splashTread.start();
    }

}

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/linearLayout"
    android:background="@drawable/loading_screen_background" >

    <ImageView
        android:id="@+id/firstLoadingImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/loading100" />

</LinearLayout>
faruk.kuscan
  • 499
  • 1
  • 6
  • 19

3 Answers3

1

Frame animations can be achieved by setting it to an AnimationDrawable to the background of an ImageView.

Example:

final AnimationDrawable anim = new AnimationDrawable();
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame1, durationInMs);
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame2, durationInMs);
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_frame3, durationInMs);
...
anim.addFrame(Context.getResources().getDrawable(R.drawable.resource_id_of_framen, durationInMs);
anim.setOneShot(false);

ImageView myImageView = getImageViewToSet();
myImageView.setBackgroundDrawable(anim);
myImageView.post(new Runnable(){
   public void run(){
      anim.start();
   }
}

So make your splash screen a simple layout with an ImageView or create one and add it to the layout. Set the AnimationDrawable to it and run.

DeeV
  • 35,865
  • 9
  • 108
  • 95
0

My guess would be to use the function runOnUiThread() after calling sleep in your splash thread. Make a runnable class to change the ImageView background, and run in this function perhaps? Activity.runOnUiThread()

Drake Clarris
  • 1,047
  • 6
  • 10
0

You could find the tutorial for it here

Duc Tran
  • 6,016
  • 4
  • 34
  • 42