2015-05-31 31 views
0

有一個自定義按鈕控制,我已經有問題。 的按鈕佈局定義是這樣的:不明白爲什麼OutOfMemory上setImageDrawable

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<ImageView 
    android:layout_width="180dp" 
    android:layout_height="180dp" 
    android:layout_centerInParent="true" 
    android:layout_margin="20dp" 
    android:background="#fff" 
    android:contentDescription="@string/my_button_info" 
    android:src="@drawable/button_appearance" /> 

</merge> 

和button_appearance的定義是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/button_pic_down" 
    android:state_pressed="true" /> 
<item android:drawable="@drawable/button_pic" 
    android:state_focused="true" /> 
<item android:drawable="@drawable/button_pic" /> 
</selector> 

現在,當我把我的按鈕(點擊的LinearLayout)我展示它作爲一個不同位圖和動畫它作爲旋轉,這裏是動畫:

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:duration="1666" 
android:fromDegrees="0" 
android:interpolator="@android:anim/linear_interpolator" 
android:pivotX="50%" 
android:pivotY="50%" 
android:repeatCount="infinite" 
android:toDegrees="359" /> 

,我這樣稱呼它:

public void animateButton() { 
    myButton.setEnabled(false); 
    myButton.setImageDrawable(getContext().getResources().getDrawable(R.drawable.button_pic_spin)); 
    myButton.startAnimation(AnimationUtils.loadAnimation(context, R.anim.spinner_animation)); 
} 

那麼,當它揭開序幕操作完成後我停下來:

public void stopAndEnableButton() { 
    myButton.setEnabled(true); 
    myButton.setImageDrawable(getContext().getResources().getDrawable(R.drawable.button_appearance)); 
    ttcButton.clearAnimation(); 
} 

我已經收到了來自Crashlytics幾個崩潰就行剛好高於我打電話setImageDrawable。 Crashlytics說:

無法分配與1275632個免費字節,1245KB一個2869648字節分配,直到OOM

奇怪的是,這裏的PNG圖像的尺寸都大約25K - 而且只有3 (在button_appearance.xml中引用了2個,而在普通的button_pic_spin中引用了2個)當我嘗試交換最多隻有50K的內容時,如何達到2兆字節的要求?

在setImageDrawable之前我應該​​調用ClearAnimation的問題是什麼?系統是否真的試圖爲部分旋轉的位圖做一堆分配?

+1

什麼是圖像的尺寸?考慮到磁盤中的大小是壓縮大小。圖像是未壓縮使用它 –

+0

635x635像素 - 會這麼解釋嗎? – Kibi

+0

它本身似乎並不具有戲劇性,但您遇到了一些內存問題。你有任何其他記憶密集型活動?應用程序在崩潰時使用了多少內存? –

回答

1

Android中圖像的內存分配取決於圖像的像素寬度和高度。不是磁盤大小。

影像存儲器大小=寬度×高度×4個字節(用於紅色字節,字節爲 綠色,藍色字節和字節用於α-)

+0

謝謝 - 沒有意識到。縮小PNG並希望能夠解決問題 – Kibi

+0

稍後我將擴展我的答案以解釋如何顯示高規格。圖片。這需要一個縮放功能才能真正發揮其高規格的優勢。如果你明白我的意思。因爲有些設備即使因屏幕分辨率低而設置爲適合屏幕,也不會獲得優勢。 – hasan83

0

您的圖片太大,請嘗試縮小尺寸,或嘗試使用第二帖子here的提示加載它。 (我正在談論那些按鈕圖片)

-1

如果你迫切需要一個解決方案在應用程序清單中提供「largeHeap」:true。

+0

不,謝謝,那不是我想要解決的問題,我需要了解實際的東西,而不是粘貼它 – Kibi

-1

此錯誤是由Java虛擬機拋出( JVM)當由於缺少內存空間而無法分配對象時,垃圾收集器也無法釋放一些空間。

參考:https://stackoverflow.com/a/32245018/6111927

添加下面的實體清單檔案中的:android:hardwareAccelerated="false"android:largeHeap="true"

<application 
android:allowBackup="true" 
android:hardwareAccelerated="false" 
android:icon="@mipmap/ic_launcher" 
android:label="@string/app_name" 
android:largeHeap="true" 
android:supportsRtl="true" 
android:theme="@style/AppTheme">