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();
}
}
Related
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.
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);
}
}
});
}
});
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.
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();
}
I have made custom buttons in Android for a while. Things were simple, just made image resources for button states and made a selector for it. Everything went smooth and nice. Now I encountered a new situation.
I have made a animation drawable and set it as a background for my button.
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="#drawable/frame1" android:duration="600" />
<item android:drawable="#drawable/frame2" android:duration="300" />
<item android:drawable="#drawable/frame3" android:duration="500" />
</animation-list>
If I set the animation as button's Background it works fine. If I try to make a simple selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="#drawable/animation" />
<item
android:state_pressed="true"
android:drawable="#drawable/pressed" />
</selector>
where normal state of the button would have animation as background and the pressed state a static image, things don't work right.
On my main activity, on onWindowFocus I get the button background and start the animation
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
btn = (Button)findViewById(R.id.btnAnim);
btnAnimation = (AnimationDrawable) btnAnim.getBackground();
btnAnimation.start();
}
Here appears to be the problem, because my animation won't be taken correctly from the selector and I get the following error:
03-14 15:21:16.146: ERROR/AndroidRuntime(440): FATAL EXCEPTION: main
03-14 15:21:16.146: ERROR/AndroidRuntime(440): java.lang.ClassCastException: android.graphics.drawable.StateListDrawable
03-14 15:21:16.146: ERROR/AndroidRuntime(440): at com.bebenjoy.MainActivity.onWindowFocusChanged(MainActivity.java:53)
03-14 15:21:16.146: ERROR/AndroidRuntime(440): at ...
Any idea on how to fix this ? Thanks.
You're doing incorrect cast -- your background drawable is StateListDrawable, not AnimationDrawable. I'd rather do something like:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
btn = (Button)findViewById(R.id.btnAnim);
StateListDrawable background = (StateListDrawable) btn.getBackground();
Drawable current = background.getCurrent();
if (current instanceof AnimationDrawable) {
btnAnimation = (AnimationDrawable) current;
btnAnimation.start();
}
}
My answer is a bit late, I know, but I faced the same issue. I checked a lot of solutions, but found only one.
I have tried to start the animation in onWindowFocusChanged(), start the animation in aseparate thread, but it doesn't help.
I solved this issue using setVisible (boolean visible, boolean restart)
So you can try this:
private Button ImgBtn;
private AnimationDrawable btnAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
StateListDrawable background = (StateListDrawable) btn.getBackground();
btnAnimation = (AnimationDrawable) background.getCurrent();
btnAnimation.setVisible(true, true); // it works even in onCreate()
}
Hope this will help somebody :)