2012-06-22 108 views
1

我在,我想兩個動畫同時運行的應用程序的工作,但它不工作.... 我的代碼如下....幀動畫不能正常工作

ImageView Background, planet, info; 
AnimationDrawable infoview, backgroundview; 
@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Background = (ImageView) findViewById(R.id.imageView1); 
    planet = (ImageView) findViewById(R.id.planet); 
    //Background.setImageResource(R.drawable.friend_night_sky_31000); 
    Log.w("debug", "planetanimation started"); 
    planetStart(R.drawable.earth, R.drawable.background); 
    Log.w("debug", "planetanimation stoped"); 
    info = (ImageView) findViewById(R.id.info); 
    info.setImageResource(R.drawable.earthinfo); 
    Log.w("DEBUG", "which is null:image " + infoview + "or" + backgroundview); 
} 

public void planetStart(final int pid, final int bid){ 
    Thread timer = new Thread(){ 
    @Override 
    public void run(){ 
     try{ 
     //Thread.sleep(time); 
     } catch (Exception e){ 

     } finally{ 
     Infoview.this.runOnUiThread(new Runnable(){ 
      public void run(){ 
      planet.setBackgroundResource(pid); 
      infoview = (AnimationDrawable) planet.getBackground(); 
      infoview.start(); 
      Background.setBackgroundResource(bid); 
      backgroundview = (AnimationDrawable) Background.getBackground(); 
      backgroundview.start(); 
      Log.w("DEBUG", "which is null:image " + infoview + "or" + backgroundview); 
      } 
     }); 
     } 
    } 
    }; 
    timer.start(); 
} 

能任何一個幫助我爲什麼它不工作?

EDIT1我的地球文件是遵循

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <item android:drawable="@drawable/earthframe1" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe2" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe3" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe4" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe5" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe6" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe7" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe8" android:duration="150" /> 
    <item android:drawable="@drawable/earthframe9" android:duration="150" /> 
    </animation-list> 

和BG文件如下

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/bg_1" android:duration="150" /> 
    <item android:drawable="@drawable/bgimage2" android:duration="150" /> 
    <item android:drawable="@drawable/bgimage03" android:duration="150" /> 
    <item android:drawable="@drawable/bgimage4" android:duration="150" /> 
    <item android:drawable="@drawable/bgimage5" android:duration="150" /> 


</animation-list> 
+0

請在這裏發佈logcat。 –

+0

它沒有顯示任何東西..... – Ashishsingh

+0

顯示您的AnimationDrawable! –

回答

1

你有兩個解決方案要麼ü可以從planetStart方法刪除線程或如果u希望有exisiting代碼去不是給像了Thread.sleep(1000)的一些值;在線程睡眠我已經檢查過這個值,它適用於我一件事避免第二個動畫在第一個動畫結束之前開始

+0

我希望兩個動畫應該同時工作 – Ashishsingh

+0

我得到了我的ansewer好友感謝.... – Ashishsingh

0

試試下面的代碼

package org.sample; 

import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.ImageView; 

public class SampleActivity extends Activity 
{ 

    ImageView Background, planet, info; 


    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Background = (ImageView) findViewById(R.id.imageView1); 
    Background.setBackgroundResource(R.drawable.background); 

    planet = (ImageView) findViewById(R.id.planet); 
    planet.setBackgroundResource(R.drawable.earth); 

    info = (ImageView) findViewById(R.id.info); 
    info.setImageResource(R.drawable.earthinfo); 

    AnimationDrawable BackgroundAnimation = (AnimationDrawable) Background.getBackground(); 

    BackgroundAnimation.start(); 

    AnimationDrawable PlanetAnimation = (AnimationDrawable) planet.getBackground(); 

    PlanetAnimation.start(); 

    } 
} 
0

無法啓動動畫onCreate(是的,你這樣做)。您應仔細閱讀this(尤其是帶段落的最後一個示例)。