2012-04-19 47 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並運行。