我有一些图片,我正在加载到我的程序中,似乎导致OutOfMemoryError。我更喜欢使用.png文件,因为它们有一个清晰的alpha层,但是根据this SO link,使用BitmapFactory命令来避免OoME可能会更好。是否可以在.png文件上使用BitmapFactory命令?是不是更好地坚持.png文件或尝试将它们全部转换为位图或其他一些图像格式?只有大约393k的图像。
错误代码:
05-01 03:07:36.197: E/AndroidRuntime(561): Caused by: java.lang.OutOfMemoryError
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.Bitmap.nativeCreate(Native Method)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:524)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:499)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:351)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.content.res.Resources.loadDrawable(Resources.java:1937)
05-01 03:07:36.197: E/AndroidRuntime(561): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
编辑:我的图片加载了一种自定义图像视图
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:layout_weight="0.55"
android:id="@+id/dicemenu">
<com.surreall.yacht.SquareButton
android:layout_height="fill_parent"
android:layout_width="0dip"
android:layout_weight="1"
>
<ImageView
android:id="@+id/die1"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_margin="9dp"
/>
</com.surreall.yacht.SquareButton>
并且自定义imageview看起来像这样(有点长故事,是我从问题中得到的解决方案found here:
package com.surreall.yacht;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareButton extends LinearLayout {
public SquareButton(Context context) {
super(context);
}
public SquareButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
// This is used to make square buttons.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
答案 0 :(得分:1)
当您不再需要图片时尝试Bitmap.recycle()