Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: make animation from still images

I've a series of still images and total of more than 500 images presented in drawable directory. I need to make an animation (load about 20 images per second). I want it to run smoothly and with no Out Of Memory Exception.

I've idea to do this that images for 2 to 3 seconds (40 to 60 images) should load in memory and displayed and then they should disposed off (release the memory) and then images for next 2 to 3 seconds should load. This technique can prevent Out Of Memory Exception. Its just an idea, I dont know whether its a good idea or not. Please guide me some better idea with some code to go with... If my idea is much better and can work then please tell me some helping code to do that.

After reading replies and doing as you suggest, I've written some code like this:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/llMain">

<ViewFlipper android:id="@+id/imageflipper"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<ImageView android:id="@+id/ImageView01"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"        
        android:scaleType="centerInside"        
        android:layout_gravity="center" />
    </ViewFlipper>
    </LinearLayout>

and here is my code for doing animation:

public class Animation extends Activity {
ViewFlipper flipper;
int myIndex = 216;
private final Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    flipper=(ViewFlipper)findViewById(R.id.imageflipper); 
    doTheAutoRefresh();
        //displayData();
}

private void doTheAutoRefresh() {
    handler.postDelayed(new Runnable() {
        public void run(){
            displayData(); // this is where you put your refresh code
            doTheAutoRefresh();
             }
         }, 30);

}

private void displayData()
{
    Resources r = getResources();
    if(myIndex > 230){
        myIndex = 216;
        ImageView myImg = (ImageView)findViewById(R.id.ImageView01);
        myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));

        myIndex += 1;
        flipper.showNext();
    }
    else{
        ImageView myImg = (ImageView)findViewById(R.id.ImageView01);

        myImg.setImageResource(r.getIdentifier("drum0" + myIndex, "drawable", "com.vt.animation"));

        myIndex += 1;
        flipper.showNext();
    }
}

}

but its very slow. I've set up the refresh time to 30 milliseconds but actually its not refreshing too fast rather its refresh time is about 1 second. Any suggestion to make it fast to feel like real animation?

Thanks,

like image 680
Mudasser Hassan Avatar asked Apr 10 '11 10:04

Mudasser Hassan


People also ask

Can you animate still pictures?

PixaMotion is a free photo animation app that can pretty much do it all. Thanks to the sleek modern design that will probably remind you of other photo editors, the app is easy to navigate. You can animate your photo, add audio and moving elements from the app's gallery to it, as well as apply photo filters.


1 Answers

Use a FrameAnimation, eg in res/drawable/movie.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/frame1" android:duration="50" />
    <item android:drawable="@drawable/frame2" android:duration="50" />
    <item android:drawable="@drawable/frame3" android:duration="50" />
    etc...
</animation-list>

And then in Java:

imageView.setBackgroundResource(R.drawable.movie);
AnimationDrawable anim = (AnimationDrawable) imageView.getBackground();
anim.start();
like image 73
olivierg Avatar answered Sep 28 '22 07:09

olivierg