2012-05-01 37 views
1

我有一些图片,我加载到我的程序,似乎是导致OutOfMemoryError。我更喜欢使用.png文件,因为它们有一个明确的alpha层,但根据this SO link,最好使用BitmapFactory命令来避免OoME。有没有可能在.png文件上使用BitmapFactory命令?坚持使用.png文件还是尝试将它们全部转换为位图或图像的其他格式更好?只有约393k的图像价值。获取少量图像的java.lang.OutOfMemoryError,使用pngs还是bitmaps更好?

错误代码:

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) 

编辑:我的图像加载带有几分自定义的ImageView

<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); 
    } 
} 
+1

添加代码,你如何使用你的PNG。 –

+0

http://developer.android.com/intl/zh-CN/training/displaying-bitmaps/load-bitmap.html - 看起来像这可能是有用的。 –

+0

我正在尝试加载小的PNG文件,虽然 – clayton33

回答

1

试着做Bitmap.recycle()当你不再需要你的形象

+0

不是大的位图,但只要装载了带有图像的xml布局,就会收到错误。 – clayton33

+0

如何加载图片? –

相关问题