xml布局文件里直接使用背景图导致爆内存的情况处理

前端之家收集整理的这篇文章主要介绍了xml布局文件里直接使用背景图导致爆内存的情况处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在产品发布出去后,经常收到布局文件里边使用被背景图导致到内存的情况,一般报错日志如下: StackTrace : Unable to start activity ComponentInfo{com.gtp.nextlauncher.trial/com.gtp.nextlauncher.trial.recommand.RecommandGoSmsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayoutjava.lang.RuntimeException: Unable to start activity ComponentInfo{com.gtp.nextlauncher.trial/com.gtp.nextlauncher.trial.recommand.RecommandGoSmsActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) at android.app.ActivityThread.access$1500(ActivityThread.java:121) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3701) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout at android.view.LayoutInflater.createView(LayoutInflater.java:518) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) at android.view.LayoutInflater.inflate(LayoutInflater.java:386) at android.view.LayoutInflater.inflate(LayoutInflater.java:320) at android.view.LayoutInflater.inflate(LayoutInflater.java:276) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) at android.app.Activity.setContentView(Activity.java:1657) at com.gtp.nextlauncher.trial.recommand.RecommandGoSmsActivity.onCreate(RecommandGoSmsActivity.java:30) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) ... 11 more Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:415) at android.view.LayoutInflater.createView(LayoutInflater.java:505) ... 21 more Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715) at android.content.res.Resources.loadDrawable(Resources.java:1713) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.(View.java:1969) at android.view.View.(View.java:1917) at android.view.ViewGroup.(ViewGroup.java:304) at android.widget.RelativeLayout.(RelativeLayout.java:173) ... 24 more android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout at android.view.LayoutInflater.createView(LayoutInflater.java:518) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) at android.view.LayoutInflater.inflate(LayoutInflater.java:386) at android.view.LayoutInflater.inflate(LayoutInflater.java:320) at android.view.LayoutInflater.inflate(LayoutInflater.java:276) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) at android.app.Activity.setContentView(Activity.java:1657) at com.gtp.nextlauncher.trial.recommand.RecommandSmsActivity.onCreate(RecommandSmsActivity.java:30) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) at android.app.ActivityThread.access$1500(ActivityThread.java:121) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3701) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:415) at android.view.LayoutInflater.createView(LayoutInflater.java:505) ... 21 more Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715) at android.content.res.Resources.loadDrawable(Resources.java:1713) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.(View.java:1969) at android.view.View.(View.java:1917) at android.view.ViewGroup.(ViewGroup.java:304) at android.widget.RelativeLayout.(RelativeLayout.java:173) ... 24 more java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:415) at android.view.LayoutInflater.createView(LayoutInflater.java:505) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) at android.view.LayoutInflater.inflate(LayoutInflater.java:386) at android.view.LayoutInflater.inflate(LayoutInflater.java:320) at android.view.LayoutInflater.inflate(LayoutInflater.java:276) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) at android.app.Activity.setContentView(Activity.java:1657) at com.gtp.nextlauncher.trial.recommand.RecommandSmsActivity.onCreate(RecommandSmsActivity.java:30) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) at android.app.ActivityThread.access$1500(ActivityThread.java:121) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3701) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715) at android.content.res.Resources.loadDrawable(Resources.java:1713) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.(View.java:1969) at android.view.View.(View.java:1917) at android.view.ViewGroup.(ViewGroup.java:304) at android.widget.RelativeLayout.(RelativeLayout.java:173) ... 24 more java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715) at android.content.res.Resources.loadDrawable(Resources.java:1713) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.(View.java:1969) at android.view.View.(View.java:1917) at android.view.ViewGroup.(ViewGroup.java:304) at android.widget.RelativeLayout.(RelativeLayout.java:173) at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:415) at android.view.LayoutInflater.createView(LayoutInflater.java:505) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) at android.view.LayoutInflater.inflate(LayoutInflater.java:386) at android.view.LayoutInflater.inflate(LayoutInflater.java:320) at android.view.LayoutInflater.inflate(LayoutInflater.java:276) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) at android.app.Activity.setContentView(Activity.java:1657) at com.gtp.nextlauncher.trial.recommand.RecommandSmsActivity.onCreate(RecommandSmsActivity.java:30) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) at android.app.ActivityThread.access$1500(ActivityThread.java:121) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:123) at android.app.ActivityThread.main(ActivityThread.java:3701) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) at dalvik.system.NativeStart.main(Native Method) 很长,但关键日志是在: java.lang.OutOfMemoryError: bitmap size exceeds VM budget at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:715) at android.content.res.Resources.loadDrawable(Resources.java:1713) at android.content.res.TypedArray.getDrawable(TypedArray.java:601) at android.view.View.(View.java:1969) at android.view.View.(View.java:1917) at android.view.ViewGroup.(ViewGroup.java:304) at android.widget.RelativeLayout.(RelativeLayout.java:173) 这是由于解析xml时背景图标导致爆内存,至于最小是多大的图片会导致爆内存,这个无法确定,猜测应该跟机型和实际运行情况有关, 因为产品发布前测试过的机型都没有问题,用户的反馈信息中看到最低90多K会爆。 至于解决方案: 1)直接找设计师更话小一点的图片,不过设计师也难为,因为一张图片光亮,渐变等元素去掉之后,效果也没那么好了,鱼跟熊掌,不可兼得。 2)能不能从代码层面去减少图片的大小呢??? 可以的。可能通过代码来设置背景,然后通过trycatch来捕获爆内存; 继续优化的,可以通过动态缩放图标来减低爆内存机率。 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.recommand_sms); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(),R.drawable.recommand_sms_bg,opts); opts.inSampleSize = computeSampleSize(opts,-1,480 * 800); opts.inJustDecodeBounds = false; try { Bitmap bmp = BitmapFactory.decodeResource(getResources(),opts); relativeLayout.setBackgroundDrawable(new BitmapDrawable(bmp)); } catch (OutOfMemoryError err) { } /** * <br>功能简述: * <br>功能详细描述: * <br>注意: * @param options * @param minSideLength * @param maxNumOfPixels 最大显示像素 * @return */ public static int computeSampleSize(BitmapFactory.Options options,int minSideLength,int maxNumOfPixels) { int initialSize = computeInitialSampleSize(options,minSideLength,maxNumOfPixels); int roundedSize; if (initialSize <= 8) { roundedSize = 1; while (roundedSize < initialSize) { roundedSize <<= 1; } } else { roundedSize = (initialSize + 7) / 8 * 8; } return roundedSize; } private static int computeInitialSampleSize(BitmapFactory.Options options,int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels)); int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( Math.floor(w / minSideLength),Math.floor(h / minSideLength)); if (upperBound < lowerBound) { // return the larger one when there is no overlapping zone. return lowerBound; } if ((maxNumOfPixels == -1) && (minSideLength == -1)) { return 1; } else if (minSideLength == -1) { return lowerBound; } else { return upperBound; } } 原文链接:/xml/299605.html

猜你在找的XML相关文章