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,
OK. The biggest problem and the easiest solution I got to go with after so many days. I would never expect that it would be so easy to do... :D
I've used both handler and timer to achieve with just an image view, no flipper, no animator nothing else at all... Here is my solution:
----- main.xml file -----
And here is the way I have done it:
}
Note: I've placed all my images to asset directory.
Its so simple as it can, nothing big to do...
I hope it'll be helpful for someone looking to go like this :)
Well, I'm using viewFlipper, switching between views. Good thing about this one is that u can see previous picture sliding out while the new one slides in.
Inside image displaying method:
And inside XML file:
Use a FrameAnimation, eg in
res/drawable/movie.xml
:And then in Java:
Loading several images is very expensive.
I think it's better to load a single image containing all the movements of that certain animation. An animation strip.
private Bitmap animation = BitmapFactory.decodeResource(getResources(), R.drawable.myPng);
The idea is to traverse the bitmap.