Igor
Igor

Reputation: 1341

What would be a better way to create Android animation?

I'm trying to create an Android app that will include many (sometimes up to about 200) small images that will be animated (relocate and change size) about 20 at a time. After that they'll remain still.

Should I use the simple solution of the View class animation or should I use Drawable animation?

EDIT: I should be more specific..

There are a lot of tutorial out there and a lot of different ways to do the same thing. I'm looking for the best way to implement the next scenario:

Say I have 50 different small (30x30) images currently drawn on the screen. I need to animate them so they will translate to a different DYNAMIC position. And while they are moving I need the image to be resized up and down (so I get kind of a jump effect if looking from top). They need to move within a specific timeframe. For example: After the first image starts to move, the second will begin moving 50ms after the last and so on (wave effect)... After one group of images is translated, another group will be formed, but the last group will still be on screen.

So what I'm asking is a little specifics about the best way to do this. For example: Should I create a XML file for each Image or should I just load them in code? Should I load all the images (there could be up to 200 small images, maybe more) at application start or will it be ok to load them on demand? What would be the best animation technique? Stuff like that.

Upvotes: 1

Views: 682

Answers (2)

Igor
Igor

Reputation: 1341

The easiest solution I found: (API 16+)

Runnable endAction = new Runnable() {
     public void run() {
         tv.animate().x(400).y(1400).scaleX(1).scaleY(1);
     }
 };
tv.animate().x(600).y(100).scaleX(3).scaleY(3).withEndAction(endAction);

Upvotes: 1

Gonzalo Solera
Gonzalo Solera

Reputation: 1192

I would use Drawable animation but it doesn´t matter so much. The important thing you should do if the app runs very slow, is to use diferents threads using this code for example:

Handler mHandler = new Handler();
        mHandler.post(new Runnable() {
                   public void run() {
                       //YOUR ANIMATION HERE
                    }
                 });            

In this way, you will be able to process the animation of a lot of images at the same time because the phone will execute the code in different computing threads.

You can use too AsyncTask like that (adding the class into your activity class):

private class doAnimation extends AsyncTask<ImageView, Void, Void>{


     @Override
     protected Void doInBackground(ImageView... image) {
         image.startAnimation(animation);
         return null;
    }

}

And calling it using:

    new doAnimation().execute(image1);
    new doAnimation().execute(image2);
    new doAnimation().execute(image3);
    ...

Upvotes: 0

Related Questions