frame animation is not working - android

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>

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

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();
}
}

You can't start animation in onCreate (and yes, you do this). You should read this carefully (especially last example with paragraph).

Related

Android Frame Animation only plays first frame

When I launch the emulator, the animation only plays the first frame of the animation, sits for the duration of the timer and then starts the activity.
This is my Drawable xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/title1" android:duration="200" />
<item android:drawable="#drawable/title2" android:duration="200" />
<item android:drawable="#drawable/title3" android:duration="200" />
<item android:drawable="#drawable/title4" android:duration="200" />
<item android:drawable="#drawable/title5" android:duration="200" />
<item android:drawable="#drawable/title6" android:duration="200" />
<item android:drawable="#drawable/title7" android:duration="200" />
<item android:drawable="#drawable/title8" android:duration="200" />
<item android:drawable="#drawable/title9" android:duration="200" />
<item android:drawable="#drawable/title10" android:duration="200" />
<item android:drawable="#drawable/title11" android:duration="200" />
</animation-list>
This is my layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/gyro"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
This is my java file
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
// Start the animation (looped playback by default).
ImageView splash = (ImageView) findViewById(R.id.gyro);
splash.setBackgroundResource(R.drawable.title);
AnimationDrawable splashAnimation = (AnimationDrawable) splash.getBackground();
splashAnimation.start();
Thread logoTimer = new Thread(){
public void run(){
try{
int logoTimer = 0;
while(logoTimer <10000){
sleep(100);
logoTimer = logoTimer + 100;
}//end of while loop
//mpsplash.stop();
startActivity(new Intent("tv.bScienceFiction.scrip.or.scrap.CLEARSCREEN"));
} catch (InterruptedException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}//end of finally
}//end of run
};//end of new Thread
logoTimer.start();
}
Start it from the UI thread after you exit onCreate
splash.postDelayed(new Runnable() {
public void run() {
splashAnimation.start();
}
}, 200);

Why animated drawable doesn't appear

I want to create animated drawable from 14 png images.
I added the 14 images to all drawable- folders, and created a animated-list like below, but nothing appear, what is the problem ?
circle.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/f1" android:duration="50" />
<item android:drawable="#drawable/f2" android:duration="50" />
<item android:drawable="#drawable/f3" android:duration="50" />
<item android:drawable="#drawable/f4" android:duration="50" />
<item android:drawable="#drawable/f5" android:duration="50" />
<item android:drawable="#drawable/f6" android:duration="50" />
<item android:drawable="#drawable/f7" android:duration="50" />
<item android:drawable="#drawable/f8" android:duration="50" />
<item android:drawable="#drawable/f9" android:duration="50" />
<item android:drawable="#drawable/f10" android:duration="50" />
<item android:drawable="#drawable/f11" android:duration="50" />
<item android:drawable="#drawable/f12" android:duration="50" />
<item android:drawable="#drawable/f13" android:duration="50" />
<item android:drawable="#drawable/f14" android:duration="50" />
</animation-list>
layout 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" >
<Button
android:id="#+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<ImageView
android:id="#+id/imgCircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
java code:
package pit.opensource.animation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CircleAnimationActivity extends Activity {
/** Called when the activity is first created. */
Button btnStart;
ImageView imgCircle;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart = (Button)findViewById(R.id.btnStart);
imgCircle = (ImageView) findViewById(R.id.imgCircle);
imgCircle.setBackgroundResource(R.drawable.circle);
AnimationDrawable ani = (AnimationDrawable) imgCircle.getBackground();
ani.start();
btnStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// ani.start();
}
});
}
}
The background should be animation, you should put circle.xml to res/anim, and change
imgCircle.setBackgroundResource(R.drawable.circle);
to
imgCircle.setBackgroundResource(R.anim.circle);
Try following code to start the animation
imgCircle.post(new Runnable() {
#Override
public void run() {
AnimationDrawable ani = (AnimationDrawable) imgCircle.getBackground();
ani.start();
}
});
or implement move the animation start to onWindowFocusChanged
public void onWindowFocusChanged(boolean flag) {
super.onWindowFocusChanged(flag);
AnimationDrawable anim = (AnimationDrawable) imgCircle.getBackground();
anim.start();
}
There could be third reasons.
First reason is OutOfMemoryError. You need to compress your images.
Solution
Second reason is android:oneshot parameter in your circle.xml. This animation runs for just 14 frames. By setting the android:oneshot attribute of the list to true, it will cycle just once then stop and hold on the last frame. If it is set false then the animation will loop.
Change
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
with
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
Third Reason is start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window.
Change your MainActivity.java with this code:
package pit.opensource.animation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CircleAnimationActivity extends Activity {
Button btnStart;
ImageView imgCircle;
AnimationDrawable ani;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgCircle = (ImageView) findViewById(R.id.imgCircle);
imgCircle.setBackgroundResource(R.drawable.circle);
ani = (AnimationDrawable) imgCircle.getBackground();
addListenerOnButton();
}
public void addListenerOnButton() {
btnStart = (Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
ani.start();
}
});
}
}

How to start an start activity after android animation is over

How to start an activity after the animation has ended.
I have added android:oneshot="true" in the xml but how to start a new activity
after this animation has stopped.I have attached the entire code below.
Please let me know how to start new activity.
package com.appsolut.example.animation;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class Animation extends Activity {
ImageView animation;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation =
(AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
public void onStart() {
{
super.onStart();
animation = (ImageView)findViewById(R.id.imageAnimation);
animation.setBackgroundResource(R.drawable.animation);
}
}
}
animation.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item android:drawable="#drawable/img00000" android:duration="500" />
<item android:drawable="#drawable/img00001" android:duration="500" />
<item android:drawable="#drawable/img00002" android:duration="500" />
<item android:drawable="#drawable/img00003" android:duration="500" />
<item android:drawable="#drawable/img00004" android:duration="500" />
<item android:drawable="#drawable/img00005" android:duration="500" />
<item android:drawable="#drawable/img00006" android:duration="500" />
<item android:drawable="#drawable/img00007" android:duration="500" />
<item android:drawable="#drawable/img00008" android:duration="500" />
<item android:drawable="#drawable/img00009" android:duration="500" />
<item android:drawable="#drawable/img00010" android:duration="500" />
</animation-list>
Use and AnimationListener on your animation to do whatever you want on its onAnimationEnd() method.
After your animation call create a new Intent (From the Intent class).
Intent myIntent = new Intent(context, newActivity.class);
startActivity(myIntent);
Context can be getContext() or getBaseContext()
This should work. Also dont't forget to declare your new activity in the AndroidManifest or the application will crash!
Hope this helped.
Use the method hasEnded() to check if ended and then if it has then call your intent to open up the new activity.
It should work
Best Regards

Frame Animation on button click in Android

My Problem is I have Some Images & I used frame animation to display this images on click event of button but if i click button first time the image is display in sequence & if i click this button another time that time the image is not displayed. following is my code.
Animation.java file:-
public class Animation extends Activity {
Button mBtnOK;
AnimationDrawable frameAnimation;
ImageView imgView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBtnOK = (Button) findViewById(R.id.mBtnOK);
mBtnOK.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
animate();
}
});
}
private void animate() {
imgView = (ImageView) findViewById(R.id.simple_anim);
imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.anim.simple_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) imgView
.getBackground();
frameAnimation.start();
frameAnimation.setOneShot(true);
}
}
Animation file:-
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
<item android:drawable="#drawable/monkey_1" android:duration="50" />
<item android:drawable="#drawable/monkey_2" android:duration="50" />
<item android:drawable="#drawable/monkey_3" android:duration="50" />
<item android:drawable="#drawable/monkey_4" android:duration="50" />
<item android:drawable="#drawable/monkey_5" android:duration="50" />
<item android:drawable="#drawable/monkey_6" android:duration="50" />
<item android:drawable="#drawable/monkey_7" android:duration="50" />
<item android:drawable="#drawable/monkey_8" android:duration="50" />
<item android:drawable="#drawable/monkey_9" android:duration="50" />
<item android:drawable="#drawable/monkey_10" android:duration="50" />
</animation-list>
The only way to restart a frame animation is to use the setVisible() which contains a flag to force the animation to reset to the first frame. If you modify the animating section of code like so:
AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground();
frameAnimation.setOneShot(true);
frameAnimation.setVisible(true, true);
frameAnimation.start();
The animation should always start from the first frame and run to completion each time you click the button. The animation can also be reset by toggling visibility on the drawable itself, instead of the ImageView that contains it.
HTH
#Dipak,
I have done with the animation using the same way you have done. Try to add this code, hope your error will be resolved. And also one more thing is use thread to run the animation. This will surely run it nicely.
if(frameAnimation.isRunning()) {
frameAnimation.stop();
frameAnimation.start();
}

Layout Animation is blank

Nothing comes to the screen. The individual pngs render fine. what is wrong?
<ImageButton
android:id="#+id/btn_loading"
android:src="#drawable/loading_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And next is the file "loading_animation.xml" :
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
android:repeatMode="repeat" >
<item android:drawable="#drawable/load0" android:duration="20" />
<item android:drawable="#drawable/load1" android:duration="20" />
<item android:drawable="#drawable/load2" android:duration="20" />
<item android:drawable="#drawable/load3" android:duration="20" />
<item android:drawable="#drawable/load4" android:duration="20" />
<item android:drawable="#drawable/load5" android:duration="20" />
<item android:drawable="#drawable/load6" android:duration="20" />
<item android:drawable="#drawable/load7" android:duration="20" />
<item android:drawable="#drawable/load8" android:duration="20" />
</animation-list>
You have to start the animation
Use the following code in your java code
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class Main extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myxml);
ImageView animation = (ImageView) findViewById(R.id.btn_loading);
animation.setBackgroundResource(R.drawable.loading_animation);
AnimationRoutine animationRoutine = new AnimationRoutine();
Timer t = new Timer(false);
t.schedule(animationRoutine, 100);
}
private class AnimationRoutine extends TimerTask {
AnimationRoutine() {
}
public void run() {
ImageView img = (ImageView) findViewById(R.id.btn_loading);
AnimationDrawable frameAnimation = (AnimationDrawable) img
.getBackground();
frameAnimation.start();
}
}
}

Categories

Resources