I'm trying to get the AnimationDrawable on my Android app to repeat.
I configured the android:oneshot on false. (tried both with java and with XML)
Still, whenever the animation is played, once it's done, it goes back to the first frame, and stops
This is my XML file
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/network_scanning" android:oneshot="false">
<item android:drawable="#drawable/network_wireless_0" android:duration="300" />
<item android:drawable="#drawable/network_wireless_1" android:duration="300" />
<item android:drawable="#drawable/network_wireless_2" android:duration="300" />
<item android:drawable="#drawable/network_wireless_3" android:duration="300" />
<item android:drawable="#drawable/network_wireless_4" android:duration="300" />
</animation-list>
and this is the code to start the animation:
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
LinearLayout network = (LinearLayout) findViewById(R.id.wifi_anim);
AnimationDrawable anim = (AnimationDrawable) network.getBackground();
if (hasFocus)
{
anim.setOneShot(false);
anim.setCallback(network);
anim.setVisible(true, true);
anim.start();
}
}
hi you can get solution from here
http://docs.mono-android.net/Android.Graphics.Drawables.AnimationDrawable
Related
I created a custom ProgressDialog which has a lively ImageView. But this giving error for use within a AsyncTask execution.
AsyncTask out of work. I tried to use the animation with a runOnUiThread and not worked. How can I use this animation while running AsyncTask?
layout_progress.xml (only image)
<ImageView
android:id="#+id/ivLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#anim/loading"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>
loading.xml (animation)
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="#+id/animation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/load5_1" android:duration="150" />
<item android:drawable="#drawable/load5_2" android:duration="150" />
<item android:drawable="#drawable/load5_3" android:duration="150" />
<item android:drawable="#drawable/load5_4" android:duration="150" />
<item android:drawable="#drawable/load5_5" android:duration="150" />
<item android:drawable="#drawable/load5_6" android:duration="150" />
<item android:drawable="#drawable/load5_7" android:duration="150" />
<item android:drawable="#drawable/load5_8" android:duration="150" />
<item android:drawable="#drawable/load5_9" android:duration="150" />
<item android:drawable="#drawable/load5_10" android:duration="150" />
</animation-list>
method show of the custom progress dialog
public void show() {
super.show();
setContentView(layoutId);
final ImageView imageView = (ImageView) findViewById(R.id.ivLoading);
final AnimationDrawable anim = (AnimationDrawable) imageView.getDrawable();
final Runnable run = new Runnable() {
#Override
public void run() {
anim.start();
}
};
imageView.post(run);
//test 2 - doesnt work
/*
final Runnable run = new Runnable() {
#Override
public void run() {
anim.start();
imageView.post(this);
}
};
activity.runOnUiThread(run);
*/
}
You should not do this in an AsyncTask. Manipulation of the UI should be done only in the UI thread.
There is a simple example of what you are trying to achieve here:
http://developer.android.com/guide/topics/graphics/drawable-animation.html
Try and start the animation after you are sure that the ImageView has been attached to the view hierarchy. Quoting the link I posted:
It's important to note that the 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. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus.
I was following the Google provided example of how to use AnimationDrawable with an ImageView. You can find it here: http://developer.android.com/guide/topics/graphics/drawable-animation.html
imageView.setBackgroundResource(R.drawable.animation);
AnimationDrawable animation = (AnimationDrawable)imageView.getBackground();
animation.start();
When I run it I get the error:
java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Google seems to think this should work, but if you cannot cast a BitmapDrawable to AnimationDrawable I am not sure how this is supposed to work?
I figured out the solution to this problem.
imageView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
AnimationDrawable animation = (AnimationDrawable) imageView.getDrawable();
animation.start();
I have no idea why Google's documentation says to use the background, but using setImageDrawable and getDrawable works. Honestly it makes more sense it would work this way than the other way anyways.
I had the same problem. I know this thread is some month old, but maybe somebody what to read about my experience.
I dont know why, but google doesnt accept Spacemarks like "_" in his Picturenames while using it for animation. I uses names like "loading_frame1", and it doesnt work. I changed the names to something like "loadingframe1" and it works....
Before:
<?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/loading_frame1" android:duration="100" />
<item android:drawable="#drawable/loading_frame2" android:duration="100" />
<item android:drawable="#drawable/loading_frame3" android:duration="100" />
<item android:drawable="#drawable/loading_frame4" android:duration="100" />
<item android:drawable="#drawable/loading_frame5" android:duration="100" />
<item android:drawable="#drawable/loading_frame6" android:duration="100" />
<item android:drawable="#drawable/loading_frame7" android:duration="100" />
<item android:drawable="#drawable/loading_frame8" android:duration="100" />
<item android:drawable="#drawable/loading_frame9" android:duration="100" />
<item android:drawable="#drawable/loading_frame10" android:duration="100" />
<item android:drawable="#drawable/loading_frame11" android:duration="100" />
<item android:drawable="#drawable/loading_frame12" android:duration="100" />
<item android:drawable="#drawable/loading_frame13" android:duration="100" />
<item android:drawable="#drawable/loading_frame14" android:duration="100" />
<item android:drawable="#drawable/loading_frame15" android:duration="100" />
<item android:drawable="#drawable/loading_frame16" android:duration="100" />
<item android:drawable="#drawable/loading_frame17" android:duration="100" />
<item android:drawable="#drawable/loading_frame18" android:duration="100" />
<item android:drawable="#drawable/loading_frame19" android:duration="100" />
<item android:drawable="#drawable/loading_frame20" android:duration="100" />
</animation-list>
After:
<?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/loadingframe1" android:duration="100" />
<item android:drawable="#drawable/loadingframe2" android:duration="100" />
<item android:drawable="#drawable/loadingframe3" android:duration="100" />
<item android:drawable="#drawable/loadingframe4" android:duration="100" />
<item android:drawable="#drawable/loadingframe5" android:duration="100" />
<item android:drawable="#drawable/loadingframe6" android:duration="100" />
<item android:drawable="#drawable/loadingframe7" android:duration="100" />
<item android:drawable="#drawable/loadingframe8" android:duration="100" />
<item android:drawable="#drawable/loadingframe9" android:duration="100" />
<item android:drawable="#drawable/loadingframe10" android:duration="100" />
<item android:drawable="#drawable/loadingframe11" android:duration="100" />
<item android:drawable="#drawable/loadingframe12" android:duration="100" />
<item android:drawable="#drawable/loadingframe13" android:duration="100" />
<item android:drawable="#drawable/loadingframe14" android:duration="100" />
<item android:drawable="#drawable/loadingframe15" android:duration="100" />
<item android:drawable="#drawable/loadingframe16" android:duration="100" />
<item android:drawable="#drawable/loadingframe17" android:duration="100" />
<item android:drawable="#drawable/loadingframe18" android:duration="100" />
<item android:drawable="#drawable/loadingframe19" android:duration="100" />
<item android:drawable="#drawable/loadingframe20" android:duration="100" />
</animation-list>
And here the LoadingAnimation.class Listing
package com.justkidding.animation;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
public class LoadingAnimation extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loading_animation);
}
#Override
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
ImageView animation = (ImageView)findViewById(R.id.aniimage);
animation.setBackgroundResource(R.drawable.loading_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.loading_animation, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Google's code works. The "can not be cast issue" which lead me here, was because I was not paying attention and put my animation.xml in res.anim instead of res.drawable.
However I agree using setImageDrawable and getDrawable works better.
About this problem, I had a little oversight on the detail on google's example code in the documentation and this might be the case for a couple of persons using the guide.
There is an separate xml file that holds the drawables, indicating the transitions and has the tag:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="#drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="#drawable/rocket_thrust3" android:duration="200" />>
</animation-list>
The above file is named rocket_thrust, and it is this same file that is set as backgroundDrawable in the following lines:
ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
Give a try at this and be sure that documentation has no errors.
Best of luck.
Complete process to do animation is :
1. Create XML layout with imageView
and
2. Create XML file for animation suppose drawable/animation.xml
<?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/twit" android:duration="120"></item>
<item android:duration="120" android:drawable="#drawable/a111"></item>
<item android:duration="120" android:drawable="#drawable/a2"></item>
<item android:duration="120" android:drawable="#drawable/a3"></item>
<item android:duration="120" android:drawable="#drawable/a4"></item>
<item android:duration="120" android:drawable="#drawable/a5"></item>
<item android:duration="120" android:drawable="#drawable/a6"></item>
</animation-list>
now
3. Create Main Activity
Then Type this code
public class AnimationMe extends AppCompatActivity {
private ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logoo);
imgView = (ImageView) findViewById(R.id.imgView);
// the frame-by-frame animation defined as a xml file within the drawable folder
/*imgView.setBackgroundResource(R.drawable.animation);*/
imgView.setImageDrawable(getResources().getDrawable(R.drawable.animation));
// It's not possible to start the animation during the onCreate.
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable animationDrawable = (AnimationDrawable)imgView.getDrawable();
if(hasFocus)
{
animationDrawable.start();
}
else
{
animationDrawable.stop();
}
}
}
*
Note : ImageView have background as drawable and give a name of
animation.xml not for a particular image and then call with
imageview.getDrawable in AnimationDrawable.
---- You can't Run Animation in onCreate Method. Set drawable property in Imageview in onCreate() but call AnimationDrawable method out of
block of onCreate().
*
Sure it will work !
Just to add more answer to this page based on my experience because Stackoverflow seems has a very limited answer for this issue
I my case I tried to animate my background layout which using drawable for rounded radius. I got logcat error
java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.AnimationDrawable
Turn out I must set the background attribute on my layout file to this drawable file
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
<item android:drawable="#drawable/rounded_corner_gray_background" android:duration="80" />
<item android:drawable="#drawable/rounded_corner" android:duration="80" />
And then call this code on my Main Activity
val backgroundAnim = info_layout?.background as AnimationDrawable
backgroundAnim.start()
My mistake is previously I put #drawable/rounded_corner on layout file as background attribute.
Hope this can help somebody, as I spend 3 hours just to solve this issue.
i want to ask that i want to make this animation as single image and wanted to apply some other view animation on it like move or alpha etc. how this can be done ? any idea ? simply i wanted to apply view animation on drawable animation.
i have some drawable like s1,s2,s3,s4 to s16 the XML is
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/s1" android:duration="200" />
<item android:drawable="#drawable/s2" android:duration="200" />
<item android:drawable="#drawable/s3" android:duration="200" />
<item android:drawable="#drawable/s12" android:duration="200" />
<item android:drawable="#drawable/s13" android:duration="200" />
<item android:drawable="#drawable/s14" android:duration="200" />
<item android:drawable="#drawable/s15" android:duration="200" />
<item android:drawable="#drawable/s16" android:duration="200" />
</animation-list>
and java code is
anim = (ImageView) findViewById(R.id.black);
anim.setBackgroundResource(R.drawable.smurf);
AnimationDrawable hello = (AnimationDrawable) anim.getBackground();
hello.start();
here smurf in drawable is the xml file for drawable animation.
we can do that animation but we have to maintain handler for that.
private int index = 0;
private Handler handler = new Handler();
private AnimationDrawable drawable;
Runnable runnable = new Runnable() {
#Override
public void run() {
imageView.setImageDrawable(drawable.getFrame(index));
index++;
if (index == drawable.getNumberOfFrames()) {
index = 0;
}
handler.postDelayed(runnable, drawable.getDuration(index));
}
};
in your onclick or where you want to start animation write like this:
handler.postDelayed(runnable, drawable.getDuration(index));
and your onDestroy() remove the handeler call backs like this:
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
I have a AnimationDrawable which shows the numbers 0-9.
When the Activity is started, the animation starts, too.
it starts with 0, then 1,2,3 ....... and stops at picture "9";
How can I stop this little animation when f.e. "5" is displayed?
Is there any solution with a while-loop?
ImageView view = (ImageView)findViewById(R.id.iv);
AnimationDrawable animation = (AnimationDrawable)iv.getBackground();
animation.start();
You can try to do it the following way:
final ImageView imageView = (ImageView)findViewById(R.id.image);
if (null != imageView) {
final AnimationDrawable bgAnimation = (AnimationDrawable) imageView.getBackground();
bgAnimation.start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
bgAnimation.stop();
}
}, 5500);
}
Assuming that You have animation xml like the following:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/choose12"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_1"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_2"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_3"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_4"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_5"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_6"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_7"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_8"
android:duration="1000"/>
<item
android:drawable="#drawable/choose12_9"
android:duration="1000"/>
</animation-list>
However, this way will work not stable with small delays. So, I would suggest to take a look at PropertyAnimation if You need better control over animation process.
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();
}