How to animate an Animation in Android - 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);
}

Related

Run sequence of images / animation within a AsyncTask

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.

Stop animation until specific drawable is displayed

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.

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

How can I change the duration for an Android AnimationDrawable animation on-the-fly?

I'm new to Android platform. I'm using the following to animate a set of 16 "frames" using AminationDrawable in my app:
In the XML file I have:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/image_1" android:duration="200" />
<item android:drawable="#drawable/image_1_25" android:duration="200" />
<item android:drawable="#drawable/image_1_5" android:duration="200" />
<item android:drawable="#drawable/image_1_75" android:duration="200" />
<item android:drawable="#drawable/image_2" android:duration="200" />
<item android:drawable="#drawable/image_2_25" android:duration="200" />
<item android:drawable="#drawable/image_2_5" android:duration="200" />
<item android:drawable="#drawable/image_2_75" android:duration="200" />
<item android:drawable="#drawable/image_3" android:duration="200" />
<item android:drawable="#drawable/image_3_25" android:duration="200" />
<item android:drawable="#drawable/image_3_5" android:duration="200" />
<item android:drawable="#drawable/image_3_75" android:duration="200" />
<item android:drawable="#drawable/image_4" android:duration="200" />
<item android:drawable="#drawable/image_4_25" android:duration="200" />
<item android:drawable="#drawable/image_4_5" android:duration="200" />
<item android:drawable="#drawable/image_4_75" android:duration="200" />
</animation-list>
In the Java code I have the following
first I'm declaring the class and adding an onCreate() method where I set up the animation.
public class MyNewActivity extends Activity
{
// member variables (accessible from within class methods below).
AnimationDrawable mainAnimation;
long mSpeed = 50;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_widget);
// set up image
ImageView mainImage = (ImageView) findViewById(R.id.image_widget);
mainImage.setBackgroundResource(R.drawable.animated_image);
mainAnimation = (AnimationDrawable) mainImage.getBackground();
};
<...snip...>
...then later on I start my drawing when the user presses a button I call the following to start the animation moving:
private void start()
{
// start the image rotating.
if (mainAnimation.isRunning())
mainAnimation.stop();
int numFrames = mainAnimation.getNumberOfFrames();
for (int ii = 0; ii < numFrames; ii++ )
{
// change the animation speed.
Drawable d = mainAnimation.getFrame(ii);
mainAnimation.scheduleDrawable(d, mainAnimation, mSpeed);
}
}
<...snip...>
So elsewhere in the code I have a place to adjust the member variable mSpeed. If I do this and then call start(), the animation will start, however the speed is always the same (essentially what was defined in the XML above. My question is, how can I modify the "duration" of the frames to move this animation faster/slower based on user input? I see no way to modify a "duration" state, and was under the assumption the ScheduleDrawable() call above would change the drawing's frames duration.
I had the same problem but I managed to solve it when reading the source code of AnimationDrawable by implementing my own AnimationDrawable class that extends AnimationDrawable and override the Run() method and add setDuration() method which allows me to set the duration as follow:
By reviewing the original run method we see that it do the same but by calling scheduleSelf(this, SystemClock.uptimeMillis() + duration); with the duration you specified when adding the frame so I changed it. I also add duration because I use the same for all my frames but you can use array of new duration.
import android.graphics.drawable.AnimationDrawable;
import android.os.SystemClock;
public class MyAnimationDrawable extends AnimationDrawable {
private volatile int duration;//its volatile because another thread will update its value
private int currentFrame;
public MyAnimationDrawable() {
currentFrame = 0;
}
#Override
public void run() {
int n = getNumberOfFrames();
currentFrame++;
if (currentFrame >= n) {
currentFrame = 0;
}
selectDrawable(currentFrame);
scheduleSelf(this, SystemClock.uptimeMillis() + duration);
}
public void setDuration(int duration) {
this.duration = duration;
//we have to do the following or the next frame will be displayed after the old duration
unscheduleSelf(this);
selectDrawable(currentFrame);
scheduleSelf(this, SystemClock.uptimeMillis()+duration);
}
}
It's my first answer so I hope it helps you and it's explained well.

image animation in android

I have to repeat the image sequence I am using with Thread and AnimationDrawable but it is not working continuously. I don't want to stop this animation until next activity is started through button click event.
Here is my java code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);{
final ImageView splashImage=(ImageView)findViewById(R.id.heartFunction);
splashImage.setBackgroundResource(R.drawable.slide_right);
splashAnimation = (AnimationDrawable) splashImage.getBackground();
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if ( isFocused ) {
//isFocused = false;
splashAnimation.start();
var=false;
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(SPLASH_DISPLAY_LENGTH);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
slide_right.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/heartcolored0" android:duration="200" />
<item android:drawable="#drawable/heartcolored2" android:duration="200" />
<item android:drawable="#drawable/heartcolored4" android:duration="200" />
<item android:drawable="#drawable/heartcolored5" android:duration="200" />
<item android:drawable="#drawable/heartcolored6" android:duration="200" />
<item android:drawable="#drawable/heartcolored7" android:duration="200" />
<item android:drawable="#drawable/heartcolored8" android:duration="200" />
<item android:drawable="#drawable/heartcolored9" android:duration="200" />
<item android:drawable="#drawable/heartcolored10" android:duration="200" />
<item android:drawable="#drawable/heartcolored11" android:duration="200" />
<item android:drawable="#drawable/heartcolored12" android:duration="200" />
<item android:drawable="#drawable/heartcolored13" android:duration="200" />
</animation-list>
If you want your animation to contiuously run then you need to set android:oneshot="false"
You were saying before to only run through once.
If you want an animation to run until you click the screen to go to the next activity. Start the animation when the onWindowFocusChanged function
#Override
public void onWindowFocusChanged(boolean hasFocus){
splashanimation.start();
}
Then use an onTouchEvent to catch the touch, start a new activity and finish the old activity.
#Override
public boolean onTouchEvent(MotionEvent event){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Intent i = new Intent(Anim.this, Main.class);
startActivity(i);
finish();
}
return true;
}
Hope this helps, your question is very hard to read/understand.

Categories

Resources