1.SharedPreferences.Editor commit與apply的區別
abstract void |
apply() Commit your preferences changes back from this Editor to the
SharedPreferences object
it is editing. |
abstract SharedPreferences.Editor |
clear() Mark in the editor to remove all values from the preferences.
|
abstract boolean |
commit() Commit your preferences changes back from this Editor to the
SharedPreferences object
it is editing. |
這兩個方法的區別在於:
1. apply沒有返回值而commit返回boolean表明修改是否提交成功
2. apply是將修改數據原子提交到內存, 而後異步真正提交到硬件磁盤, 而commit是同步的提交到硬件磁盤,因此,在多個併發的提交commit的時候,他們會等待正在處理的commit保存到磁盤後在操作,從而降低了效率。而apply只是原子的提交到內容,後面有調用apply的函數的將會直接覆蓋前面的內存數據,這樣從一定程度上提高了很多效率。
3. apply方法不會提示任何失敗的提示。
由於在一個進程中,sharedPreference是單實例,一般不會出現併發衝突,如果對提交的結果不關心的話,建議使用apply,當然需要確保提交成功且有後續操作的話,還是需要用commit的。
2.ImageView src屬性與background屬性區別
background會根據ImageView組件給定的長寬進行拉伸,而src就存放的是原圖的大小,不會進行拉伸 。
src是圖片內容(前景),bg是背景,可以同時使用。
此外: scaleType只對src起作用;bg可設置透明度,比如在ImageButton中就可以用android:scaleType控制圖片的縮放方式