In this example, there is an ImageView with it's "src" value set to an image. The ImageView's background is set with you own AnimationDrawable, basically an xml in your res/drawable folder. In the activity, the ImageView has a click listener, which creates an AnimationDrawable from the ImageView's background, and just calls the start method of the AnimationDrawable class.
Here is a sample AnimationDrawable described through XML.
<?xml version="1.0" encoding="utf-8"?>And here is the code that gets your animation started.
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/black" android:duration="200" />
<item android:drawable="@drawable/cyan" android:duration="200" />
<item android:drawable="@drawable/green" android:duration="200" />
<item android:drawable="@drawable/magenta" android:duration="200" />
<item android:drawable="@drawable/navy" android:duration="200" />
<item android:drawable="@drawable/orange" android:duration="200" />
<item android:drawable="@drawable/pink" android:duration="200" />
<item android:drawable="@drawable/white" android:duration="200" />
<item android:drawable="@drawable/yellow" android:duration="200" />
</animation-list>
imageView = (ImageView) findViewById(R.id.ImageButton01);One point to notice is that, you have to remove the ImageView's "src" value, so that the animation is visible, since the animation works by changing the background of the ImageView, else, your animation would be blocked by the ImageView's "src" image.
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AnimationDrawable animator = (AnimationDrawable) imageView.getBackground();
imageView.setImageDrawable(null);
animator.start();
}
});
Once you are done, you can stop the animation, and reset the ImageView's "src" to it's original image.
// Call this method to stop the animationYou can find the whole source code for this example here.
public void stopAnimation(){
AnimationDrawable animator = (AnimationDrawable) imageView.getBackground();
animator.stop();
imageView.setImageResource(R.drawable.icon);
}
hi kumar, i have seen lot of ur solutions given to people are really adorable!
ReplyDeletebut i want to very very basi thing here, i am using windows to play with ANDROID( eclipse env)!
once i download ur code how can i just run directly ?
Everytime i need to create new project ? then copy paste code ? or is there some easy way out
One more thing i am very fascinated about using webservice (JSON or XML) now a days , i want to display imge and title from a site. how it can be done.
ReplyDelete@jaspreet: You can checkout the code directly into eclipse once you add the SVN repossitory into eclipse.
ReplyDeleteAdd this SVN repository in your eclipse.
https://myandroidwidgets.googlecode.com/svn/trunk/
Once you do this, you can find all the projects under this. Right click and "checkout" any project.
lets say i have downloaded your code/project !
ReplyDeleteAnd i have kept this in my C:/Eclipse-projects/ folder after unzipping it. Will it run directly ? in my local AVD
@jaspreet: No, you will have to import it into eclipse, and only then the apk will be generated, which you can push into your AVD.
ReplyDeletecode doesnt work.. bad programmer
ReplyDeletekoko kok oko ko k o k
ReplyDelete