1

我有一些图片正在加载到我的程序中,这些图片似乎会导致 OutOfMemoryError。我更喜欢使用 .png 文件,因为它们有一个清晰的 alpha 层,但根据这个 SO 链接,最好对图像使用 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>

并且自定义图像视图看起来像这样(有点长篇大论,是我从这里发现的问题中得到的解决方案:

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);
    }
}
4

1 回答 1

1

Bitmap.recycle()当你不再需要你的图像时尝试做

于 2012-05-01T08:26:48.433 回答