2012-04-19 73 views
2

我正在android 2.1中编写启动画面。我想要在启动屏幕上按顺序显示20个PNG图像。我试图使用动画列表,但无法做到这一点。在启动画面中逐帧动画 - Android 2.1

这是我的代码。现在,有一个名为firstLoadingImage.png的图像。只显示5000ms。然后,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> 

回答

0

我的猜测是在你的飞溅线程调用sleep后使用的功能runOnUiThread()。制作一个可运行的类来更改ImageView背景,并在这个函数中运行? Activity.runOnUiThread()

1

帧动画可以通过将其设置到一个AnimationDrawableImageView的背景来实现。

例子:

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并运行。