Reputation: 742
I am working on apps in which i want to run two frame animation simultaneously but it is not working.... my code is as follow....
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();
}
can any one help me why it is not working ?
Edit1 my earth file is as follow
<?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>
and bg file is as follow
<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>
Upvotes: 1
Views: 1348
Reputation: 7605
you have two solution either u can remove thread from the planetStart method or if u want to go with an exisiting code than give some value like Thread.sleep(1000); in thread sleep i have checked with this value and it works for me one more thing avoid second animation starting before the ending of first animation
Upvotes: 1
Reputation: 2350
You can't start animation in onCreate (and yes, you do this). You should read this carefully (especially last example with paragraph).
Upvotes: 0
Reputation: 28093
Try Below code
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();
}
}
Upvotes: 0