Android:以编程方式释放位图内存资源 [英] Android: free up bitmap memory resources programmatically
问题描述
我有一个对话框,用户可以在其中自由绘制对话框.
I am having a dialog where users can freely draw inside the dialog.
对话框正在扩展视图,并且绘图区域是通过
The dialog is extending a view, and the drawing area is created by
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
但是,不需要保留图形,并且当用户关闭对话框并再次重新打开时,不需要保留其先前的图形并将其从null重新绘制.
However, the drawing need not to be kept, and when the user close the dialog and reopen again, his previous drawing is not needed to be kept and redraw from null.
// DoodleView constructor initializes the DoodleView
public DoodleView(Context context, AttributeSet attrs)
{
super(context, attrs); // pass context to View's constructor
paintScreen = new Paint(); // used to display bitmap onto screen
// set the initial display settings for the painted line
paintLine = new Paint();
paintLine.setAntiAlias(true); // smooth edges of drawn line
paintLine.setColor(Color.BLACK); // default color is black
paintLine.setStyle(Paint.Style.STROKE); // solid line
paintLine.setStrokeWidth(25); // set the default line width
paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
pathMap = new HashMap<Integer, Path>();
previousPointMap = new HashMap<Integer, Point>();
} // end DoodleView constructor
// Method onSizeChanged creates BitMap and Canvas after app displays
@Override
public void onSizeChanged(int w, int h, int oldW, int oldH)
{
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(bitmap);
bitmap.eraseColor(Color.parseColor("#80FFFFFF")); // erase the BitMap with white
} // end method onSizeChanged
// clear the painting
public void recycling()
{
bitmap.recycle();
}
写字板:
public void write_board ()
{
writing_dialog = new Dialog(Apple.this, android.R.style.Theme_Translucent_NoTitleBar);
WindowManager.LayoutParams lp = writing_dialog.getWindow().getAttributes();
lp.dimAmount = 0.5f;
writing_dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Window window = writing_dialog.getWindow();
window.setGravity(Gravity.CENTER);
writing_dialog.setContentView(R.layout.alert_drawing_pad);
writing_dialog.setCancelable(true);
writing_dialog.show();
writing_dialog.setVolumeControlStream(AudioManager.STREAM_MUSIC);
doodleView = (DoodleView) writing_dialog.findViewById(R.id.doodleView);
alert_close.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
writing_dialog.dismiss();
doodleView.recycling();
return;
}
});
问题:
我发现几次打开对话框后(使用Samsung Note2进行了7至8次),对话框将滞后几秒钟,并且手机无响应,然后再再按一会儿,对话框将再次出现一切都很好.
Question:
I discover that after opening the dialog several times (7 to 8 times using Samsung Note2), the dialog would lag for a few second and the phone without response, and then further press again some while later, the dialog can appear again and everything is ok.
此时的logcat报告E/OpenGLRenderer(25296): Out of memory!
The logcat at this time reports E/OpenGLRenderer(25296): Out of memory!
它持续了一段时间而没有挂起.
and it goes on without hanging for some time.
但是,如果进一步按下绘图板,则会出现严重的内存错误,并且此时系统会挂起.
Yet if further press out for the drawing board, it goes with a serious memory error and this time the system hangs.
FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:726)
at android.graphics.Bitmap.createBitmap(Bitmap.java:703)
at android.graphics.Bitmap.createBitmap(Bitmap.java:670)
at com.abc.abc.DoodleView.onSizeChanged(DoodleView.java:60)
at android.view.View.sizeChange(View.java:15326)
at android.view.View.setFrame(View.java:15290)
at android.view.View.layout(View.java:15201)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:15204)
at android.view.ViewGroup.layout(ViewGroup.java:4793)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2263)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2009)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1251)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6379)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
at android.view.Choreographer.doCallbacks(Choreographer.java:591)
at android.view.Choreographer.doFrame(Choreographer.java:561)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
at dalvik.system.NativeStart.main(Native Method)
我已经研究并添加了
bitmap.recycle();
每当用户关闭对话框时.但是它仍然报告相同的错误.
I have researched and added
bitmap.recycle();
whenever the user closes the dialog. But it still reports the same error.
每当用户关闭对话框时,有没有办法处置位图?
Are there any way to dispose the bitmap whenever the user closes the dialog?
谢谢!
推荐答案
您应该在关闭对话框时调用它
You should call this on dismiss of your dialog
bitmap.recycle();
bitmap = null;
bitmap.recycle();
释放在位图中使用的本机堆.
并将其设置为null是为了帮助GC快速收集您的参考.
bitmap.recycle();
releases the native heap that is used in bitmaps.
And setting it to null is to assist the GC to quickly collect your reference.
这篇关于Android:以编程方式释放位图内存资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!