在启动画面中逐帧动画 - Android 2.1

时间:2012-04-19 15:25:38

标签: android animation android-2.1-eclair

我在android 2.1中写了一个启动画面。我希望在启动画面上按顺序显示20个png图像。我试图使用动画列表,但无法设法做到这一点。

这是我的代码。现在,有一个名为firstLoadingImage.png的图像。仅显示5000毫秒。然后myappactivity开始。但是,我无法在此等待时间内更新图像源。你觉得我怎么能这样做?

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>

3 个答案:

答案 0 :(得分:1)

可以通过将AnimationDrawable设置为ImageView的背景来实现帧动画。

示例:

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();
   }
}

因此,使用ImageView使您的初始屏幕成为简单布局,或者创建一个并将其添加到布局中。将AnimationDrawable设置为它并运行。

答案 1 :(得分:0)

我的猜测是在你的splash线程中调用sleep后使用runOnUiThread()函数。创建一个runnable类来改变ImageView背景,并在这个函数中运行吗? Activity.runOnUiThread()

答案 2 :(得分:0)

你可以找到它的教程 here