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>
4

3 回答 3

1

您有两个解决方案,您可以从planetStart方法中删除线程,或者如果您想使用现有代码而不是提供一些值,例如Thread.sleep(1000); 在线程睡眠中我已经检查了这个值,它对我有用还有一件事避免在第一个动画结束之前开始第二个动画

于 2012-06-22T11:19:42.763 回答
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();

  }
}
于 2012-06-22T11:05:42.353 回答
0

您不能在 onCreate 中启动动画(是的,您可以这样做)。您应该仔细阅读此内容(尤其是最后一个带有段落的示例)。

于 2012-06-22T11:17:11.597 回答