Run sequence of images / animation within a AsyncTask - android

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.

Related

How to animate an Animation in Android

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

Gif in android xml not working

Hi I am looking to create gif animation in xml.I separated gif images and used the below code
myxm.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/a" android:duration="100" />
<item android:drawable="#drawable/c" android:duration="100" />
<item android:drawable="#drawable/e" android:duration="100" />
</animation-list>
and in my layout i used the code
<ImageView
android:id="#+id/imageButton1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:src="#anim/myxm" />
But it doesn't works animation.Is it the right way to create gif animation in xml ? please help me thanks in advance :)
You can try the following,
AnimationDrawable frameAnimation;
ImageView view;
view = (ImageView) findViewById(R.id.imageButton1);
// Setting myxm.xml as the background of the image view
view.setBackgroundResource(R.anim.myxm);
// Typecasting the Animation Drawable
frameAnimation = (AnimationDrawable) view.getBackground();
// Called when Activity becomes visible or invisible to the user
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// Starting the animation when in Focus
frameAnimation.start();
} else {
// Stoping the animation when not in Focus
frameAnimation.stop();
}
}

animation-list frame duration is wrong on Froyo

In my app, I have a blinking play-pause button which I've made using an animation-list, as follows:
<?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/play_pause" android:duration="750" />
<item android:drawable="#drawable/play_pause_play" android:duration="750" />
</animation-list>
Both play_pause and play_pause_play are PNG files. This animation is used (as #drawable/play_pause_ready) inside a level-list:
<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:maxLevel="1" android:drawable="#drawable/play_pause_pause" />
<item android:maxLevel="2" android:drawable="#drawable/play_pause_play" />
<item android:maxLevel="3" android:drawable="#drawable/play_pause" />
<item android:maxLevel="10" android:drawable="#drawable/play_pause_ready" />
</level-list>
and the level-list in turn is used as a compound drawable in a TextView. In case it's relevant, the code to operate it looks like this:
private void updatePlayPauseButton(...) {
if (getView() != null) {
LevelListDrawable ld = (LevelListDrawable) timeField.getCompoundDrawables()[2];
ld.setLevel(...);
Drawable child = ld.getCurrent();
if (child instanceof AnimationDrawable) {
AnimationDrawable ad = (AnimationDrawable) child;
ad.start();
}
}
}
and is called from onStart() and other places (the behaviour is the same when it's called in response to a button press).
On ICS and Jellybean, everything works perfectly, but on my HTC Desire running Froyo, the animation displays wrong. The first frame of the animation displays for roughly the right duration (750 ms), but the second frame displays for a very short period, no more than 20 ms.
This looks very much like a bug in Froyo, but I can't find anything about it on Internet. Has anyone seen this problem before, and if so, is there a workaround?
i had similar issues with animation-list on Android 2.2, 3.2
solved with AnimationDrawable.setCallback()
void startAnimation(ImageView iv) {
final AnimationDrawable ad = (AnimationDrawable) iv.getDrawable();
ad.setCallback(iv);
ad.setVisible(true, true);
iv.post(new Runnable() {
#Override public void run() {
ad.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();
}

Android AnimationDrawable starts, and when done hangs on first frame

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

Categories

Resources