-1

I need to show animation in image view. for that i have used following code. This is my animation image list xml file.

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">

<item android:drawable="@drawable/read_by_myselfenglish_1" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_2" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_3" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_4" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_5" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_6" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_7" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_8" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_9" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_10" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_11" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_12" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_13" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_14" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_15" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_16" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_17" android:duration="210" />
<item android:drawable="@drawable/read_by_myselfenglish_18" android:duration="210" />

and this my android activity class .

package com.www.froginthewhole;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

 public class MainClass extends Activity {

private AnimationDrawable frameAnimation;
    private ImageView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_class);

    view = (ImageView) findViewById(R.id.imageView1);
    view.setBackgroundResource(R.anim.home_book);    
    frameAnimation = (AnimationDrawable) view.getBackground();

    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
             frameAnimation.start();                
        }
    });
}

}

Here I need to change images in image view on-click event. but unfortunately i got java.lang.OutOfMemoryError runtime exception. I can not handles this. This is my log.

12-03 20:18:58.492: E/AndroidRuntime(1187): java.lang.OutOfMemoryError
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.content.res.Resources.loadDrawable(Resources.java:1965)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.content.res.Resources.getDrawable(Resources.java:660)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.content.res.Resources.loadDrawable(Resources.java:1950)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.content.res.Resources.getDrawable(Resources.java:660)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.view.View.setBackgroundResource(View.java:14463)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at com.www.froginthewhole.MainClass.onCreate(MainClass.java:22)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.app.Activity.performCreate(Activity.java:5104)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.os.Looper.loop(Looper.java:137)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at java.lang.reflect.Method.invokeNative(Native Method)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at java.lang.reflect.Method.invoke(Method.java:511)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-03 20:18:58.492: E/AndroidRuntime(1187):     at dalvik.system.NativeStart.main(Native Method)

Can someone please help me.

Lahiruzz
  • 665
  • 1
  • 12
  • 24
  • 2
    Check out this link: http://developer.android.com/training/displaying-bitmaps/index.html. OutofMemory is common while displaying lots of images. – user2511882 Dec 03 '13 at 20:44

1 Answers1

3

Out of memory is one of Android most common issues, as devices have different heap size. The reason for small heap size is that Android is designed to work even on low end devices.

Since you run your app in the native form (java), it is subject to the heap's size.

What can you do? you can create your own mechanism of animation, downsample, use a different format for bitmaps (565 instead of 8888), and use the minimal memory that you can, hoping there is no device that can't handle it. You could also ask the OS to give you a larger heap (using the largeHeap flag), but it might not give you the amount that you need, if at all.

Minimizing the memory footprint on bitmaps is a very common solution, and is very needed in the Android platform, as the minimal heap size on all devices is still just 16MB (but the common one is much larger). here's a post i've made about it.

However, The best thing to use it not to use the native form of apps for heavy bitmaps usage, and instead use either C/C++ or openGL solutions.

For openGL solutions, you can use third party libraries like LibGDX or AndEngine.

If you insist on using the native form of apps (java), please let us know more about the images (resolution, file type and where you've put them). it's important in order to know how much memory they will use, as what i think is happening is that android loads all of the images into memory at once.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • I got it. I never used OPENGL. does it support for animation. I just only need to change images in image view in onclick event. do u same some sample tutorials links . thanks for your answer. – Lahiruzz Dec 03 '13 at 20:51
  • you usually use openGL for games and heavy apps. if all you want is small button with animation, it should be ok to use the native form (java). can you please give some more information about the images and where you've put them? also please re-read my answer. i've edited it multiple times. – android developer Dec 03 '13 at 20:54
  • i put images in drawable folder. Important things is i need to used serveral animation like this. but when i test my frist animation i got unexpected error. Can you suggest me to good method for this purpose. – Lahiruzz Dec 03 '13 at 21:00
  • Yes. it seems like a very similar case to this post: http://stackoverflow.com/a/18280989/878126 . what i think is that you've put the files on "res/drawable", which is not a recommended place for images, especially nowadays. the reason is that the default density is mdpi, and you've probably run your app on a high-density device. as an example, if you have 18 images, each of size 400x400, yet they run on galaxy S4 (with XXhdpi density), you get : 4*18*(400*3)*(400*3) = ~103 MB ... and all this for a single animation. you should read the post i've written about this issue. – android developer Dec 03 '13 at 21:09
  • Can i keep my images in separate folder instead of drawable folder? – Lahiruzz Dec 03 '13 at 21:13
  • yes I read. I can put them in drawable-xxhdpi folder. in my case total image size may be more than 50mb. does this problem or do I have to go for solution like OPENGL ? – Lahiruzz Dec 03 '13 at 21:28
  • I Put them in drawable-xxhdpi folder and now it works. problem is when i used 50mb files does it works or does it give error like memory leak issue, Please give me your idea ? – Lahiruzz Dec 03 '13 at 21:35
  • openGL is quite a hardcore solution, yet 50MB is a lot for a single animation. please describe the files. what is their resolution? what is their content? maybe you don't even need them, and can use a better ,alternative animation ? – android developer Dec 03 '13 at 21:38
  • not one. i used lot of animation. see this :https://www.dropbox.com/s/zxkug0fg0akdsjv/InteractiveBook-Demo.mp4 i'm going to develop this – Lahiruzz Dec 03 '13 at 21:45
  • Then I highly suggests to use OpenGL solutions, and if your knowledge of OpenGL is small, I'd suggest using any of the libraries i've written about (AndEngine or LibGdx). each has its own advantages and disadvantages. Libgdx is fast, light and can run on multi-platforms (even on your PC - easy to develop this way), but AndEngine has more high-level classes and functions, and it might be easier to learn and use. BTW, cool app. Wish I had it in Hebrew, for my young relatives. – android developer Dec 03 '13 at 21:49
  • One Last thing. If I used this way does it give errors ? I mean error like memory leak? – Lahiruzz Dec 03 '13 at 21:51
  • 1
    memory leaks are bugs. you can have memory leaks on any platform and programming language. do you mean out of memory issues? if so, now it doesn't depend on the heap size, but on the real memory of the device (like how real games work). most of the devices nowadays have at least 512 MB of RAM . I suggest that you will always load & run animations when needed, and dispose of them as soon as possible. also try to downscale them in case the device's screen can't show high quality images anyway. and of course, make the animation's frames as small as possible. – android developer Dec 03 '13 at 21:54
  • BTW, i don't say it's impossible to make your app run on the native form (java), but i think it's very challenging, and you might get low performance due to the thing you might need to do in order to minimize the memory usage. – android developer Dec 03 '13 at 21:59
  • another possible solution is to use videos instead of animations, where the user will be able to interact between them. did you also consider this solution? – android developer Dec 03 '13 at 22:04
  • i can not used video. I'll try this way if not i'll go for your method like openGL. – Lahiruzz Dec 04 '13 at 04:12
  • 1
    If you use the native form, test your app on as many devices as you can, and always check how much memory the app takes. think how to deal with devices that have too small heap so that users won't complain about it crashing. – android developer Dec 04 '13 at 06:26