我正在处理我希望同时运行两个帧动画的应用程序,但它无法正常工作.... 我的代码如下....
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 :(得分:1)
你有两个解决方案要么你可以从 planetStart 方法中移除线程,要么你想要使用现有代码而不是像 Thread.sleep(1000); 在线程睡眠中我已经使用此值进行了检查,这对我来说还有一件事可以避免在第一个动画结束之前开始的第二个动画
答案 1 :(得分: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();
}
}
答案 2 :(得分:0)
你无法在onCreate中启动动画(是的,你这样做)。您应该仔细阅读this(特别是带段落的最后一个例子)。