Bitmap Factory animation using threads or async task

一曲冷凌霜 提交于 2020-01-24 17:44:12

问题


This question is a follow up on multiple question's that I asked on this forum on why an animation that I have been trying is not working.

Just a brief on the previous questions:

  1. My animation works as an individual project of 2 classes, but will not work when included in my project containing multiple classes.
  2. All activities leading up to my animation activity were closed using "finish()" class
  3. The app never closes or freezes, the animation just doesn't play, but the intent occurs and the next activity opens/displays successfully
  4. First used 10 images each of 70-80 kb size for the animation and later used, 10 images of 30 - 34 kb size each and still got the same effect.

I have since come to the conclusion that my main thread may be doing excessive work, and this may be the reason why the animation is not occurring properly.

Thus using 2 threads I coded the following sequence, but still am getting the same result. Can some one please explain what is going on and why can't I get a simple animation working???

my ButtonAnime class

public class ButtonAnime extends Activity{

private static int SPLASH_TIME_OUT = 500;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);         
        final AnimationDrawable animate= new AnimationDrawable();           
        final ImageView imageView = (ImageView) findViewById(R.id.iVArcRail_2);

        new Thread(new Runnable() {   <---- thread 1

            @Override
            public void run() {
                // TODO Auto-generated method stub
                final BitmapFactory.Options options = new BitmapFactory.Options();
                 options.inSampleSize = 24;
                 options.inPurgeable = true;
                 options.inScaled = true;

                  Bitmap b;
                     Drawable d;

                    int i =5;
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab1, options);
                    d = new BitmapDrawable(getResources(),b);   
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab2, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab3, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab4, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab5, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab6, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab7, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab8, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab9, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    b = BitmapFactory.decodeResource(getResources(),R.drawable.ab10, options);
                    d = new BitmapDrawable(getResources(),b); 
                    animate.addFrame(d, i);
                    imageView.setBackground(animate);
                    imageView.post(new Runnable() {

                                @Override
                                public void run() {
                                    // TODO Auto-generated method stub
                                    animate.start();   
                                }

                            }); 
        }

        });

        new Handler().postDelayed(new Runnable() {  <-- thread 2

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Intent i = new Intent(MainActivity.this , Home.class);
                startActivity(i);
                finish();
                System.gc();
            }
        }, 50);
    }

}

log cat following use of anime_loading.. during the button click for loading of animation

 GC_EXPLICIT freed 10K, 1% free 27351K/27444K, paused 6ms+7ms, total 71ms
 GC_FOR_ALLOC freed 6098K, 22% free 23220K/29404K, paused 125ms, total 126ms
 GC_FOR_ALLOC freed <1K, 15% free 25169K/29404K, paused 185ms, total 189ms
 GC_FOR_ALLOC freed <1K, 8% free 27118K/29404K, paused 215ms, total 218ms
 GC_FOR_ALLOC freed <1K, 2% free 29068K/29404K, paused 139ms, total 140ms
 Grow heap (frag case) to 30.367MB for 1995856-byte allocation
 Clamp target GC heap from 32.367MB to 32.000MB
 GC_FOR_ALLOC freed <1K, 2% free 31017K/31356K, paused 235ms, total 235ms
 Clamp target GC heap from 32.368MB to 32.000MB
 GC_FOR_ALLOC freed <1K, 2% free 31018K/31356K, paused 232ms, total 233ms
 Forcing collection of SoftReferences for 1995856-byte allocation
 Clamp target GC heap from 32.368MB to 32.000MB
 GC_BEFORE_OOM freed <1K, 2% free 31018K/31356K, paused 290ms, total 290ms
 Out of memory on a 1995856-byte allocation.
 "main" prio=5 tid=1 RUNNABLE
    | group="main" sCount=0 dsCount=0 obj=0xb3a90c90 self=0xb8f64380
    | sysTid=1723 nice=0 sched=0/0 cgrp=apps handle=-1225301676
    | state=R schedstat=( 5530000000 8170000000 2275 ) utm=486 stm=67 core=0
 at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:575)
 at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:410)
 at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
 at android.content.res.Resources.loadDrawable(Resources.java:2110)
 at android.content.res.Resources.getDrawable(Resources.java:700)
 at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
 at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
 at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
 at android.content.res.Resources.loadDrawable(Resources.java:2092)
 at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
 at android.widget.ImageView.<init>(ImageView.java:129)
 at android.widget.ImageView.<init>(ImageView.java:119)
 at java.lang.reflect.Constructor.constructNative(Native Method)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at android.view.LayoutInflater.createView(LayoutInflater.java:594)
 at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
 at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
 at android.app.Activity.setContentView(Activity.java:1928)
 at com.example.thomas.anime.Button_Anime.onCreate(Button_Anime.java:21)
 at android.app.Activity.performCreate(Activity.java:5243)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
 at android.app.ActivityThread.access$700(ActivityThread.java:135)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4998)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
 at dalvik.system.NativeStart.main(Native Method)
  --- allocation failed for scaled bitmap
 D/AndroidRuntime(1723): Shutting down VM
  threadid=1: thread exiting with uncaught exception (group=0xb3a8fb90)

log cat for error msf that occured while using anime_loading

  FATAL EXCEPTION: main
  Process: com.example.thomas, PID: 1723
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thomas/com.example.thomas.anime.Button_Anime}: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
  E/AndroidRuntime(1723):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
 at android.app.ActivityThread.access$700(ActivityThread.java:135)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:4998)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
 at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class <unknown>
 at android.view.LayoutInflater.createView(LayoutInflater.java:620)
 at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
 at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
 at android.app.Activity.setContentView(Activity.java:1928)
 at com.example.thomas.anime.Button_Anime.onCreate(Button_Anime.java:21)
 at android.app.Activity.performCreate(Activity.java:5243)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
 ... 11 more
 Caused by: java.lang.reflect.InvocationTargetException
 at java.lang.reflect.Constructor.constructNative(Native Method)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
 at android.view.LayoutInflater.createView(LayoutInflater.java:594)
 ... 24 more
 Caused by: java.lang.OutOfMemoryError
 at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
 at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:575)
 at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:410)
 at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
 at android.content.res.Resources.loadDrawable(Resources.java:2110)
 at android.content.res.Resources.getDrawable     
 at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282)
 at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
 at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
 at android.content.res.Resources.loadDrawable(Resources.java:2092)
 at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
 at android.widget.ImageView.<init>(ImageView.java:129)
 at android.widget.ImageView.<init>(ImageView.java:119)
 ... 27 more

回答1:


Instead of decoding the resources at runtime, why not create an animation list like this:

goes into the anim folder

anim_button.xml

<?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/animation1"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation2"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation3"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation4"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation5"
android:duration="@integer/frame_animation_time_ms"/>
<item
android:drawable="@drawable/animation6"
android:duration="@integer/frame_animation_time_ms"/>

</animation-list>

This goes inside the layout

<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/anim_button" />

This is the code to run it

private void animateImageView() {

AnimationDrawable imageViewAnimation = (AnimationDrawable) imageView.getBackground();
imageViewAnimation.setVisible(true, true);

}


来源:https://stackoverflow.com/questions/24341876/bitmap-factory-animation-using-threads-or-async-task

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