
在drawable文件夹中有大量的图像说超过100,我正在开发图像的动画应用程序.我使用imagevIEw来显示gif图像.我已经将Split gif图像的概念用于多个PNG格式的图像,然后使用它.
每当用户进入应用程序时,我可以看到内存越来越多,直到用户获取java.lang.OutOfMemoryError为止.
那么处理许多图像的最佳/正确的方法是什么?
以下是我的代码:
dog_animation.xml
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:ID="@+ID/DogVIEw"androID:orIEntation="vertical" ><ImageVIEw androID:ID="@+ID/dog_animation" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_weight="0.19" />
dog_animation.xml(可绘制文件夹)
<?xml version="1.0" enCoding="utf-8"?>
<item androID:drawable="@drawable/image" androID:duration="50"/><item androID:drawable="@drawable/image1" androID:duration="50"/><item androID:drawable="@drawable/image2" androID:duration="50"/><item androID:drawable="@drawable/image3" androID:duration="50"/><item androID:drawable="@drawable/image4" androID:duration="50"/><item androID:drawable="@drawable/image5" androID:duration="50"/><item androID:drawable="@drawable/image6" androID:duration="50"/><item androID:drawable="@drawable/image7" androID:duration="50"/><item androID:drawable="@drawable/image8" androID:duration="50"/><item androID:drawable="@drawable/image9" androID:duration="50"/><item androID:drawable="@drawable/image10" androID:duration="50"/><item androID:drawable="@drawable/image11" androID:duration="50"/><item androID:drawable="@drawable/image12" androID:duration="50"/><item androID:drawable="@drawable/image13" androID:duration="50"/><item androID:drawable="@drawable/image14" androID:duration="50"/><item androID:drawable="@drawable/image15" androID:duration="50"/><item androID:drawable="@drawable/image16" androID:duration="50"/><item androID:drawable="@drawable/image17" androID:duration="50"/><item androID:drawable="@drawable/image18" androID:duration="50"/><item androID:drawable="@drawable/image19" androID:duration="50"/><item androID:drawable="@drawable/image20" androID:duration="50"/><item androID:drawable="@drawable/image21" androID:duration="50"/><item androID:drawable="@drawable/image22" androID:duration="50"/><item androID:drawable="@drawable/image23" androID:duration="50"/><item androID:drawable="@drawable/image24" androID:duration="50"/><item androID:drawable="@drawable/image25" androID:duration="50"/><item androID:drawable="@drawable/image26" androID:duration="50"/><item androID:drawable="@drawable/image27" androID:duration="50"/><item androID:drawable="@drawable/image28" androID:duration="50"/><item androID:drawable="@drawable/image29" androID:duration="50"/><item androID:drawable="@drawable/image30" androID:duration="50"/><item androID:drawable="@drawable/image31" androID:duration="50"/><item androID:drawable="@drawable/image32" androID:duration="50"/><item androID:drawable="@drawable/image33" androID:duration="50"/><item androID:drawable="@drawable/image34" androID:duration="50"/><item androID:drawable="@drawable/image35" androID:duration="50"/><item androID:drawable="@drawable/image36" androID:duration="50"/><item androID:drawable="@drawable/image37" androID:duration="50"/><item androID:drawable="@drawable/image38" androID:duration="50"/><item androID:drawable="@drawable/image39" androID:duration="50"/><item androID:drawable="@drawable/image40" androID:duration="50"/><item androID:drawable="@drawable/image41" androID:duration="50"/><item androID:drawable="@drawable/image42" androID:duration="50"/><item androID:drawable="@drawable/image43" androID:duration="50"/><item androID:drawable="@drawable/image44" androID:duration="50"/><item androID:drawable="@drawable/image45" androID:duration="50"/><item androID:drawable="@drawable/image46" androID:duration="50"/><item androID:drawable="@drawable/image47" androID:duration="50"/><item androID:drawable="@drawable/image48" androID:duration="50"/><item androID:drawable="@drawable/image49" androID:duration="50"/><item androID:drawable="@drawable/image50" androID:duration="50"/><item androID:drawable="@drawable/image51" androID:duration="50"/><item androID:drawable="@drawable/image52" androID:duration="50"/><item androID:drawable="@drawable/image53" androID:duration="50"/><item androID:drawable="@drawable/image54" androID:duration="50"/><item androID:drawable="@drawable/image55" androID:duration="50"/><item androID:drawable="@drawable/image56" androID:duration="50"/>
Dog_Animation.java
public class Dog_Animation extends Activity {Timer timer = new Timer();@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestwindowFeature(Window.FEATURE_NO_Title); getwindow().setFlags(WindowManager.LayoutParams.FLAG_FulLSCREEN,WindowManager.LayoutParams.FLAG_FulLSCREEN); setContentVIEw(R.layout.dog_animation); ImageVIEw img = (ImageVIEw) findVIEwByID(R.ID.dog_animation); img.setBackgroundResource(R.drawable.dog_animation); AnimationDrawable frameAnimation = (AnimationDrawable) img .getBackground(); frameAnimation.start(); timer.schedule(new TimerTask() { public voID run() { Intent intent = new Intent(Dog_Animation.this,Man_Animation.class); startActivity(intent); } },10000);}} 现在的问题是当我尝试从一个活动移动到另一个具有相同的其他图像的动画,它给我的错误java.lang.OutOfMemory.
我尝试过如此多的不同解决方案
@OverrIDeprotected voID onDestroy() { super.onDestroy(); unbindDrawables(findVIEwByID(R.ID.DogVIEw)); System.gc();}private voID unbindDrawables(VIEw vIEw) { if (vIEw.getBackground() != null) { vIEw.getBackground().setCallback(null); } if (vIEw instanceof VIEwGroup) { for (int i = 0; i < ((VIEwGroup) vIEw).getChildCount(); i++) { unbindDrawables(((VIEwGroup) vIEw).getChildAt(i)); } ((VIEwGroup) vIEw).removeAllVIEws(); }} 所以别的,但任何解决方案都不适合我.请帮我解决这个问题.即使我已经提到http://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/这个链接,但没有解决问题.
解决方法 在您的AndroIDManifest.xml中,保留内部的应用程序标签,添加largeHeap,如下所示:<applicationandroID:allowBackup="true"androID:icon="@mipmap/ic_launcher"androID:label="@string/app_name"androID:largeHeap="true"/>总结
以上是内存溢出为你收集整理的Android:java.lang.OutOfMemoryError:全部内容,希望文章能够帮你解决Android:java.lang.OutOfMemoryError:所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)