Forumsee logo
Forumsee » Mobility » Windows phone » Read message
RSS: Subscribe to RSS

Programmatic frame by frame animation examle - AnimationDrawable

Windows Phone Source - winsource.com
Hello,

I think many android developers encountered this problem when dealing with AnimationDrawable.

If I try to start the animation from onCreate() method of my activity class, it won't work and only the first image of the animation is visible.

However, I found a way that you can start the frame by frame animation, from the UI thread. ( Even if you exit your application, when you re-enter, the animation will still work).

The trick is to start the animation in the run() method of a class (for ex. Starter)

that implements Runnable, and then call the post(new Starter()) method of the image

(or other view) which has the background set with your animation. In this way, the start()

method of the AnimationDrawable will run on the same thread (which is the UI thread) as the onCreate() method of the activity, but only after the onCreate() is finished and the Activity is initialized with the layout.

Here is my code:

Code (Text):

- public class AnimationTest

-  extends Activity {

-    

-     AnimationDrawable animation;

-    

-     /** Called when the activity is first created. */

-     @Override

-     public void onCreate(Bundle savedInstanceState) {

-         super.onCreate(savedInstanceState);

-         setContentView(R.layout.main);

-        

-         animation = new AnimationDrawable();

-         animation.addFrame(getResources().getDrawable(R.drawable.ball1), 100);

-         animation.addFrame(getResources().getDrawable(R.drawable.ball2), 1000);

-         animation.addFrame(getResources().getDrawable(R.drawable.ball3), 1000);

-         animation.setOneShot(false);

-        

-         ImageView imageAnim =  (ImageView) findViewById([URL="

-         imageAnim.setBackgroundDrawable(animation);

-        

-         // run the start() method later on the UI thread

-         imageAnim.post(new Starter());

-        

-     }

-    

-     class Starter implements Runnable {

-  

-         public void run() {

-             animation.start();        

-         }

-        

-  

-     }

- }

the main.xml file is:

Code (Text):

- <?xml version="1.0" encoding="utf-8"?>

- <LinearLayout xmlns:android="[URL]

-     android:orientation="vertical"

-     android:layout_width="fill_parent"

-     android:layout_height="fill_parent"

-     >

- <TextView  

-     android:layout_width="wrap_content"

-     android:layout_height="wrap_content"

-     android:text="@string/hello"

-     />

-     <ImageView     android:id="@+id/img"

-             android:layout_width="154px"

-             android:layout_height="217px"

-  />

- </LinearLayout>

cheers,

cLight

 


Read responses in winsource.com

Tags


Last videos:

YouTube picture in picture mode for any phone!
YouTube picture in picture mode for any phone!
After unlockin network iphone 7 just blinked
After unlockin network iphone 7 just blinked
samsung galaxy s3 dead solution
samsung galaxy s3 dead solution
How to flash Gretel G9 stock rom
How to flash Gretel G9 stock rom
Bypass Google all Samsung Devices Android 7.0 - 7.1.1 by bluetooth, very easy
Bypass Google all Samsung Devices Android 7.0 - 7.1.1 by bluetooth, very easy
Coolpad Dazen 1 Error : device can't open fastboot channel
Coolpad Dazen 1 Error : device can't open fastboot channel
VIDEO Bypass FRP Samsung S8, S8 Plus by ODIN
VIDEO Bypass FRP Samsung S8, S8 Plus by ODIN
How To Flash Qmobile Z8
How To Flash Qmobile Z8
Cars · 
Mobility · 
Hardware · 
Motorbikes · 
Travel · 
Boating · 
Aviation · 
production-frontend
About Forumsee · Contact · Privacy policy · Request content removal
We use cookies for navigation analysis and personalized advertisement. By using our site, you agree to our use of cookies.
Accept  ·  Learn more