gpt4 book ai didi

android - 使用线程或异步任务的位图工厂动画

转载 作者:行者123 更新时间:2023-12-03 13:12:39 25 4
gpt4 key购买 nike

这个问题是我在这个论坛上问过的多个问题的跟进,有关为什么我一直尝试的动画不起作用。

简要介绍一下以前的问题:

  • 我的动画作为2个类的单独项目工作,但不起作用
    当包含在我的包含多个类的项目中时。
  • 使用“finish()”类
  • 关闭了导致动画 Activity 的所有 Activity
  • 该应用程序永远不会关闭或冻结,动画不会播放,但意图发生了
    然后下一个 Activity 将成功打开/显示
  • 首先使用10张图片,每张图片的大小为70-80 kb,然后使用,
    每张10张30-34 kb大小的图像,效果仍然相同。

  • 此后得出的结论是,我的主线程可能做了过多的工作,这可能是动画无法正确进行的原因。

    因此,我使用2个线程对以下序列进行了编码,但仍得到相同的结果。
    有人可以解释一下发生了什么吗,为什么我不能制作一个简单的动画呢???

    我的ButtonAnime类
    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);
    }

    }

    使用anime_loading ..之后的日志猫。在单击按钮期间加载动画
     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)

    使用anime_loading时发生错误msf的日志猫
      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

    最佳答案

    与其在运行时解码资源,不如创建一个动画列表,如下所示:

    进入动画文件夹

    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>

    这在布局内
    <ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@anim/anim_button" />

    这是运行它的代码
    private void animateImageView() {

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

    }

    关于android - 使用线程或异步任务的位图工厂动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24341876/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com