【发布时间】:2016-03-02 03:24:40
【问题描述】:
我正在尝试执行一些内存管理,看来我需要先缩放图像才能使用它 - 问题是 - 我不确定如何在保持我创建的镜像/透明效果。
应用程序在此行崩溃:
Bitmap bitmapWithReflection = Bitmap.createBitmap(width
, (height + height/2), Config.ARGB_8888);
我已经尝试实现:
Bitmap reflectionImage = Bitmap.createScaledBitmap(originalImage, height/2, height/2, false);
但是,这是不正确的,并导致另一个致命的崩溃,在我的 logcat 中没有任何价值:
11-27 17:30:58.206: I/art(2111): Clamp target GC heap from 99MB to 96MB
11-27 17:31:01.354: I/Process(2111): Sending signal. PID: 2111 SIG: 9
11-27 17:31:42.609: I/Process(2235): Sending signal. PID: 2235 SIG: 9
如何避免这种情况?
Logcat:
11-27 17:26:00.490: D/AndroidRuntime(1953): Shutting down VM
11-27 17:26:00.492: E/AndroidRuntime(1953): FATAL EXCEPTION: main
11-27 17:26:00.492: E/AndroidRuntime(1953): Process: com.cb, PID: 1953
11-27 17:26:00.492: E/AndroidRuntime(1953): java.lang.OutOfMemoryError: Failed to allocate a 10898412 byte allocation with 1855424 free bytes and 1811KB until OOM
11-27 17:26:00.492: E/AndroidRuntime(1953): at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.graphics.Bitmap.nativeCreate(Native Method)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.graphics.Bitmap.createBitmap(Bitmap.java:812)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.graphics.Bitmap.createBitmap(Bitmap.java:789)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.graphics.Bitmap.createBitmap(Bitmap.java:756)
11-27 17:26:00.492: E/AndroidRuntime(1953): at com.cb.CoverFlowExample$ImageAdapter.createReflectedImages(CoverFlowExample.java:133)
11-27 17:26:00.492: E/AndroidRuntime(1953): at com.cb.CoverFlowExample.onCreate(CoverFlowExample.java:47)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.app.Activity.performCreate(Activity.java:5990)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.app.ActivityThread.access$800(ActivityThread.java:151)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.os.Handler.dispatchMessage(Handler.java:102)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.os.Looper.loop(Looper.java:135)
11-27 17:26:00.492: E/AndroidRuntime(1953): at android.app.ActivityThread.main(ActivityThread.java:5254)
11-27 17:26:00.492: E/AndroidRuntime(1953): at java.lang.reflect.Method.invoke(Native Method)
11-27 17:26:00.492: E/AndroidRuntime(1953): at java.lang.reflect.Method.invoke(Method.java:372)
11-27 17:26:00.492: E/AndroidRuntime(1953): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
11-27 17:26:00.492: E/AndroidRuntime(1953): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
11-27 17:26:04.564: I/Process(1953): Sending signal. PID: 1953 SIG: 9
来源:
int index = 0;
for (int imageId : mImageIds) {
Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
imageId);
int width = originalImage.getWidth();
int height = originalImage.getHeight();
//This will not scale but will flip on the Y axis
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
//Create a Bitmap with the flip matrix applied to it.
//We only want the bottom half of the image
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);
//Create a new bitmap with same width but taller to fit reflection
Bitmap bitmapWithReflection = Bitmap.createBitmap(width
, (height + height/2), Config.ARGB_8888);
//Create a new Canvas with the bitmap that's big enough for
//the image plus gap plus reflection
Canvas canvas = new Canvas(bitmapWithReflection);
//Draw in the original image
canvas.drawBitmap(originalImage, 0, 0, null);
//Draw in the gap
Paint deafaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
//Draw in the reflection
canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);
//Create a shader that is a linear gradient that covers the reflection
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff,
TileMode.CLAMP);
//Set the paint to use this shader (linear gradient)
paint.setShader(shader);
//Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
//Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width,
bitmapWithReflection.getHeight() + reflectionGap, paint);
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(bitmapWithReflection);
imageView.setLayoutParams(new CoverFlow.LayoutParams(1900, 1500));
imageView.setScaleType(ScaleType.FIT_XY);
mImages[index++] = imageView;
}
return true;
}
【问题讨论】:
-
先缩放,然后应用效果? IE。在初始
decodeResource步骤加载较少 -
我试过了,但它仍然崩溃:Bitmap originalImage = BitmapFactory.decodeResource(getResources(), imageId); int 宽度 = originalImage.getWidth(); int height = originalImage.getHeight(); Bitmap.createScaledBitmap(originalImage, height, width, false);
-
您能告诉我如何实现吗?我也试过这个:Bitmap preoriginalImage = BitmapFactory.decodeResource(getResources(), imageId); int width = preoriginalImage.getWidth(); int height = preoriginalImage.getHeight();位图 originalImage = Bitmap.createScaledBitmap(preoriginalImage, width, height, false);
-
stackoverflow.com/questions/16408505/… 有一个例子你可以告诉
decodeResource加载一个缩小的图像。inSampleSize因素也在 developer.android.com/training/displaying-bitmaps/… 中解释 -
感谢您的帮助-我感觉我越来越近了,但它仍然崩溃-您能看一下吗? pastebin.com/SSMgE4Fj
标签: java android memory-management out-of-memory fatal-error