[英]Recycle drawable of Imagebutton
In my 'homescreen' fragment I have a couple of Imagebuttons. 在我的“主屏幕”片段中,我有几个Imagebuttons。 XML for the Imagebutton is:
Imagebutton的XML是:
<ImageButton
android:id="@+id/merkzettel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#00000000"
android:scaleType="fitXY"
android:src="@drawable/dashboard_merkzettel_icon__selector" />
And the drawable XML selector looks like: 可绘制的XML选择器如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_startseite_merkzettel_on"
android:state_pressed="true" />
<item android:drawable="@drawable/icon_startseite_merkzettel_off" />
</selector>
On an older device (Nexus S) when I flip the the device around for a while (changing screen orientation) I get the exception: 在较旧的设备(Nexus S)上翻转设备一段时间(更改屏幕方向)时,出现以下异常:
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:697)
at android.content.res.Resources.loadDrawable(Resources.java:1709)
at android.content.res.Resources.getDrawable(Resources.java:581)
etcetc
This seems to be a common problem beause Android recreated the bitmap drawable every time the Activity / Fragment is recreated and for Drawables the recommended solution is to drawable.recycle()
them in onDestroy()
but I can't find a suitable method to get a hold on the drawable of the ImageButton. 这似乎是一个常见的问题,因为每次重新创建Activity / Fragment时,Android都会重新创建可绘制的位图,对于Drawables,建议的解决方案是在
onDestroy()
中将它们drawable.recycle()
为drawable.recycle()
,但我找不到合适的方法来获取按住ImageButton的可绘制对象。
Anyone knows a solution for this? 有人知道解决方案吗?
In manifest file's application tag add android:largeHeap = "true" then run your project. 在清单文件的应用程序标签中添加android:largeHeap =“ true”,然后运行您的项目。
Like Below 像下面
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Light.NoTitleBar"
android:largeHeap="true">
<application/>
In Destroy method 销毁方法
@Override
protected void onDestroy() {
super.onDestroy();
((BitmapDrawable)imagebutton.getDrawable()).getBitmap.recycle();
}
and in onCreate method check this. 并在onCreate方法中进行检查。
if (imagebutton.getDrawable() == null){
// set your image button image here.
} else {
// nothing to do.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.