Java 导致内存不足异常的动画

Java 导致内存不足异常的动画,java,android,animation,out-of-memory,Java,Android,Animation,Out Of Memory,请帮忙 当动画开始播放时,我的应用程序会提供outofmemory异常。。我能做什么? 组合动画中的图像大小为512kb,即10个图像 我的活动课是巴顿动画 public class Button_Anime extends Activity{ private static int SPLASH_TIME_OUT = 500; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(s

请帮忙

当动画开始播放时,我的应用程序会提供outofmemory异常。。我能做什么? 组合动画中的图像大小为512kb,即10个图像

我的活动课是巴顿动画

 public class Button_Anime extends Activity{

private static int SPLASH_TIME_OUT = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_about);
    ImageView loading =(ImageView) findViewById(R.id.iVArcRail_2);
    loading.setBackgroundResource(R.drawable.about_button_anime);
    AnimationDrawable load =(AnimationDrawable)loading.getBackground();
    load.start();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
               Intent i = new Intent(Button_Anime.this, Home.class); 
            startActivity(i);
            finish();
        }
    }, SPLASH_TIME_OUT);
  }
 }
  <?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/ab1"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab2"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab3"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab4"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab5"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab6"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab7"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab8"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab9"
      android:duration="50"/>
  <item
      android:drawable="@drawable/ab10"
      android:duration="50"/>

  </animation-list>
我的活动\u splash\u about.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/withoutlogo" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:background="@drawable/logoline" />

<ImageView
    android:id="@+id/iVArcRail_2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/imageView1"/>
 </RelativeLayout>
我怎样才能让这个动画运行???请帮忙。。。绝望在这里

很抱歉没有早点把这个放在这里

 i have already provided largeHeap="true"

这会很有魅力

确保将图像放在资产文件夹中

public class AnimationActivily extends Activity {

private Bitmap log_imgs[];
private Bitmap tempBitmap;
private Handler uiHandler;
private ImageView logo_view;
int img_cnt = 1;
protected int _splashTime = 91;
Thread splashTread;

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

    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_animation_activily);
    logo_view = (ImageView) findViewById(R.id.logo_view);
    log_imgs = new Bitmap[91];
    for (int i = 1; i <= 90; i++) {
        get_Images_Assest(i, "logo_pngs");
    }
    logo_view.setImageBitmap(log_imgs[70]);
    uiHandler = new Handler();
    // thread for displaying the SplashScreen
    splashTread = new Thread() {
        @Override
        public void run() {
            try {
                while (img_cnt < _splashTime) {
                    sleep(30);
                    if (img_cnt <= 90)
                        update_view(img_cnt);
                    img_cnt++;

                }
            } catch (InterruptedException e) {
                // do nothing
            } finally {

                Intent i = new Intent(AnimationActivily.this,
                        MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(i);
                AnimationActivily.this.finish();
                // overridePendingTransition(R.anim.slide_in,
                // R.anim.slide_out);
                // *** Put this in another
                // Activity***//overridePendingTransition(R.anim.slide_in,
                // R.anim.slide_out);
            }
        }
    };
    splashTread.start();

}

public Bitmap tempImg;

private void get_Images_Assest(int cnt, String folder_name) {
    try {

        InputStream is = getAssets().open(
                folder_name + "/" + "logo" + cnt + ".png");
        if (tempImg != null) {
            tempImg = null;
        }
        tempImg = BitmapFactory.decodeStream(is);
        // tempBitmap = Bitmap.createScaledBitmap(tempImg,
        // tempImg.getWidth() * 2, tempImg.getHeight() * 2, true);
        log_imgs[cnt] = tempImg;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void update_view(final int cnt) {
    uiHandler.post(new Runnable() {
        public void run() {
            try {
                logo_view.setImageBitmap(log_imgs[cnt]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}

public void onBackPressed() {
}

public void onDestroy() {
    super.onDestroy();

    for (int i = 1; i <= 90; i++) {
        log_imgs[i].recycle();
    }
    log_imgs = null;
    splashTread = null;
}
}
public类动态扩展活动{
私有位图日志_imgs[];
私有位图;
私人处理者;
私人图像视图徽标_视图;
int img_cnt=1;
受保护的时间=91;
线溅胎面;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
setContentView(R.layout.activity\u animation\u activily);
logo_view=(ImageView)findviewbyd(R.id.logo_view);
log_imgs=新位图[91];

对于(int i=1;i我也遇到了同样的问题,因此我创建了一个类来避免OutOfMemoryError。Android一次加载所有可绘制的内容,因此具有多帧的动画会导致此错误

FasterAnimationsContainer FasterAnimationsContainer将帮助您避免OutOfMemoryError。此类在后台线程上加载、设置和释放图像

这是

易于实现
在内存不足的设备上,我的背景动画也有类似的问题。 我设法修复它的方法是通过编程方式实例化动画,而不是通过xml动画列表

    AnimationDrawable animationDrawable = new AnimationDrawable();
    animationDrawable.setOneShot(false); // True = only once, False = continuous

    int[] resources = {R.drawable.ticket_background_animation_1, R.drawable.ticket_background_animation_2, R.drawable.ticket_background_animation_3};

    int duration = 30;

    for (int resource : resources) {
        animationDrawable.addFrame(ContextCompat.getDrawable(context, resource), duration);
    }

    relativeLayout.setBackground(animationDrawable);
    animationDrawable.start();
当然,记住在onDestroy()中再次停止动画也是非常重要的。

    ((AnimationDrawable) relativeLayout.getBackground()).stop();
    // Sets background to null to make sure the animation is garbage collected
    relativeLayout.setBackground(null);

添加android:largeHeap=“true”
androidmanifeat。它可以帮助您在get\u Images\u Assest(i,“logo\u pngs”)中的“logo\u pngs”是什么;以及logo\u view.setImageBitmap(log\u imgs[70]中的log\u imgs是什么);嘿,很抱歉回复晚了,logo_pngs是文件夹名称,如果位图数组是从资源创建的,则记录imgs。--您可以在资源文件夹中创建文件夹,给它起任何名称--用新名称替换名称--现在将您的图像放在文件夹中,同名文件夹刚刚出现_0..u 10…\u 14根据您的顺序,您可以给它起任何名称例如,你的文件名是img_0.png、img_1.png、img_3.png和img_4.png,然后更改:InputStream is=getAssets().open(文件夹名+“/”+“img”+cnt+.png”);for循环将用于(int i=1;我尝试过使用您的类,但当我返回到实现帧动画的屏幕时,它什么也没有显示。它显示空白图像视图。它重复运行,如何使它只运行一次此库不工作!此外,还从工作线程访问UI线程!
    AnimationDrawable animationDrawable = new AnimationDrawable();
    animationDrawable.setOneShot(false); // True = only once, False = continuous

    int[] resources = {R.drawable.ticket_background_animation_1, R.drawable.ticket_background_animation_2, R.drawable.ticket_background_animation_3};

    int duration = 30;

    for (int resource : resources) {
        animationDrawable.addFrame(ContextCompat.getDrawable(context, resource), duration);
    }

    relativeLayout.setBackground(animationDrawable);
    animationDrawable.start();
    ((AnimationDrawable) relativeLayout.getBackground()).stop();
    // Sets background to null to make sure the animation is garbage collected
    relativeLayout.setBackground(null);