Best Way to Animate images frame by frame in Android

筅森魡賤 提交于 2019-12-11 09:03:43

问题


I have 3 sets of 50 images and I have to create the animations for each set of images in Android application. I am able to create a simple application which animate first set of 50 images using the below method,

Added Animation-list xml in drawable folder and called it using frameAnimation.start().

This method didn't work until I kept the following "android:largeHeap="true" in manifest file.

I am not sure whether this is the good way to animate the images (if we have more number of images and each image of more size like 60 KB. Image is JPG format) or not

I browsed and I found that, if we are able to clear the memory and if we are able to maintain less number of images in memory, then our application will work very fine. So I want to know how to clear the memory?

Please let me know do I need to follow different method to animate the images other than I explained above, so that I can clear the memory.

Thanks for your help.


回答1:


    Do this way..
    ---------------------            
         ImageView img = (ImageView)findViewById(R.id.some layout);
         AnimationDrawable frameAnimation =    (AnimationDrawable)img.getDrawable();
         frameAnimation.setCallback(img);
         frameAnimation.setVisible(true, true);
         frameAnimation.start();


    and to add animation you can do something like 
   -----------------------------------------------
  <animation-list   android:id="@+id/my_animation" android:oneshot="false" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/frame1" android:duration="150" />
    <item android:drawable="@drawable/frame2" android:duration="150" />

 </animation-list>



回答2:


After looking into several posts and doing much research, I finally ended up modifying the images in Imageview programmatically to simulate frame animation. That way I am just maintaining three images in cache at any point of time (previous, current and next one). I hope that this will be useful to others.




回答3:


It is not a good thing to process a large amount of images in a frame like manner in Android itself as it will trigger the dreaded "Out of Memory" exception.

Clearing the memory itself is not possible. The best fix for this is proper bitmap handling of the app.

I'm not sure but you might want to check on PhoneGap.

Its an HTML5 Engine that we used before to create a game. By drawing into the canvas itself, we've recreated frame animation with it. It just requires WebDev skills though.



来源:https://stackoverflow.com/questions/18910903/best-way-to-animate-images-frame-by-frame-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!