java lang outofmemory error while using Animation in android - android

I have two buttons, one is used to start animation and other is used to stop animation. On one device, app is working fine, but on another device it crashes saying java.lang.outofmemory exception in my LogCat. Here is my xml for animation.
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/one"
android:duration="200"/>
<item
android:drawable="#drawable/two"
android:duration="200"/>
<item
android:drawable="#drawable/three"
android:duration="200"/>
<item
android:drawable="#drawable/four"
android:duration="200"/>
</animation-list>
And here is my activity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (ImageView) findViewById(R.id.imageAnimation);
on_button = (Button)findViewById(R.id.on);
off_button = (Button) findViewById(R.id.off);
view.setBackgroundResource(R.drawable.animation_list);
frameAnimation = (AnimationDrawable) view.getBackground();
on_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
frameAnimation.start();
}
});
off_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
frameAnimation.stop();
}
});
}
M actually trying to make a splash screen, and its my initial attempt.

Related

I have a loader animation but , sometimes it doesn't start the animation

I have this ImageView that I use as a loader:
<ImageView
android:id="#+id/loader_container"
android:layout_width="#dimen/loader_size"
android:layout_height="#dimen/loader_size"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/loader_bottom_offset"
android:visibility="invisible" />
I have a static method in my Utils class that does this:
public static void setLoaderVisible(ImageView loader){
loader.setVisibility(View.VISIBLE);
loader.setBackgroundResource(R.drawable.loading_animation);
loader.bringToFront();
AnimationDrawable anim = (AnimationDrawable) loader.getBackground();
anim.setOneShot(false);
anim.start();
}
I noticed that in some parts the loader does show the animation, going through the drawables, but sometimes it only shows the first picture. Why doesn't it want to animate all the time?
This is my animation:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/bus1" android:duration="200" />
<item android:drawable="#drawable/bus2" android:duration="200" />
<item android:drawable="#drawable/bus3" android:duration="200" />
<item android:drawable="#drawable/bus4" android:duration="200" />
</animation-list>
EDIT: I did try using a post, like this:
public static void setLoaderVisible(ImageView loader)
{
loader.setImageResource(R.drawable.loading_animation);
loader.setVisibility(View.VISIBLE);
loader.bringToFront();
final AnimationDrawable anim = (AnimationDrawable) loader.getDrawable();
loader.post(new Runnable() {
#Override
public void run() {
anim.start();
}
});
}
And also tried to use a AnimatedImageView class found here: AnimationDrawable not playing
but still the same issue.
I noticed that the animation works in almost all my classes. I have a activity that contains fragment and in there the loader does not animate. Tried putting the loader inside the fragment, and inside the activity, but the results are the same. Any ideeas?
try following method if it works,
public static void setLoaderVisible(ImageView loader)
{
loader.setImageResource(R.drawable.loading_animation);
loader.setVisibility(View.VISIBLE);
loader.bringToFront();
final AnimationDrawable anim = (AnimationDrawable) loader.getDrawable();
loader.post(new Runnable() {
#Override
public void run() {
anim.start();
}
});
}

Animation works only on first button click

I was wondering how to handle animations correctly. The code below is working fine but the animation starts only for the first click. It does not work again after the first click.
Layout:
<Button
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/speaker" />
Animation File 'anim.xml':
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="#drawable/choose12"
android:duration="100"/>
<item
android:drawable="#drawable/choose12_1"
android:duration="100"/>
<item
android:drawable="#drawable/choose12_2"
android:duration="100"/>
<item
android:drawable="#drawable/choose12"
android:duration="100"/>
</animation-list>
Activity:
final Button speakButton = (Button)findViewById(R.id.play);
speakButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String words = getResources().getString(R.string.select_age);
speakWords(words);
speakButton.setBackgroundResource(R.drawable.anim.xml);
AnimationDrawable AppNameAnimation = (AnimationDrawable) speakButton.getBackground();
AppNameAnimation.start();
}
});
In the code above the animation is working fine only for the first click, but it won't start at the second (or third, or Nth) click.
How can I start the animation each time the button is clicked?
I had a similar problem.
I fixed it by calling stop on the animation first if I had already played it
mAnimationDrawable.stop();
mImageView.setImageDrawable(mAnimationDrawable);
mAnimationDrawable.start();
Try the following way... Maybe it is because you already set the BackgroundResource to R.drawable.anim.xml. So again this line could not be compiled by the compiler. I think AnimationDrawable would not start for the same resource. If you change it dynamically you can get it.
final Button speakButton = (Button)findViewById(R.id.play);
speakButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String words = getResources().getString(R.string.select_age);
speakWords(words);
speakButton.setBackgroundResource(R.drawable.anim.xml);
AnimationDrawable AppNameAnimation = (AnimationDrawable) speakButton.getBackground();
AppNameAnimation.start();
speakButton.post(new Runnable() {
#Override
public void run() {
if(AppNameAnimation.getCurrent() != AppNameAnimation.getFrame(AppNameAnimation.getNumberOfFrames() - 1))
{
speakButton.post(this);
}else
{
speakButton.removeCallbacks(this);
speakButton.setBackgroundResource(R.drawable.speaker);
}
}
});
}
});

with animated images, replace the code from OnCreat function to setOnClickListener function

I am attempting to animate images, and this my code in xml file
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="#drawable/father11" android:duration="200" />
<item android:drawable="#drawable/father22" android:duration="200" />
<item android:drawable="#drawable/father33" android:duration="200" />
<item android:drawable="#drawable/father44" android:duration="200" />
<item android:drawable="#drawable/father55" android:duration="200" />
</animation-list>
but in my application i have to run the animated images when the button clicked but i got sentax error
btn_father.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
SignImage = (ImageView) findViewById(R.id.Sign);
SignImage.setImageResource(R.drawable.fatherr);
lightsAnimation=(AnimationDrawable) SignImage.getDrawable();
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
lightsAnimation.start();
}
});
I read this sentece in the link but did not understand it "(if could easily have been started with a Button or other type of input). Replace the main class in the main.java file in the src tree with this class"
Ok, View.OnClickListener has only one method onClick, so if you want to start animation on click then do the following:
btn_father.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
SignImage = (ImageView) findViewById(R.id.Sign);
SignImage.setImageResource(R.drawable.fatherr);
lightsAnimation=(AnimationDrawable) SignImage.getDrawable();
lightsAnimation.start();
}
});
If you want the animation to be applied on focus change too, then apply onFocusChanged listener on View.

Android animation not playing

I am trying to get a frame by frame animation to run on android 2.2 in a 2.2 vm machine using eclipse and i cannot for the life of me get it to run. It just sits there on the first frame of the animation.
Here is my activity code:
public class SpriteAnimationActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView lView = (ImageView) findViewById(R.id.imageView1);
AnimationDrawable lAnimation = (AnimationDrawable)lView.getBackground();
lAnimation.start();
}
}
Here is my animation list xml file:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="#drawable/a1" android:duration="100" />
<item android:drawable="#drawable/a2" android:duration="100" />
<item android:drawable="#drawable/a3" android:duration="100" />
</animation-list>
I am using the imageView widget on my main screen to display the image by setting the background. I tried to follow the guide here http://developer.android.com/guide/topics/graphics/drawable-animation.html but I can't seem to get it to work
Here you can get help:
Simple tween animation example
Android animation example on Google Code
You have to start animation forcefully...
ImageView lView = (ImageView) findViewById(R.id.imageView1);
lView.setBackgroundResource(R.drawable.my_animation_list);
final AnimationDrawable lAnimation = (AnimationDrawable)lView.getBackground();
// Button btn=
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
lAnimation.start();
}
});
You have to start the animation manually through button click listener or use the onWindowFocusChange listener like below
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus) {
lAnimation.start();
}
}

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

Categories

Resources