![](/img/trans.png)
[英]Android - ImageView: setImageBitmap VS setImageDrawable
[英]Best practice for changing between two appearances of an ImageButton in Recycler List View: setImageBitmap vs setImageResource vs setImageDrawable
我是初學者,想知道 setImageBitmap 與 setImageResource 與 setImageDrawable 中的哪一個是在 Recycler List View 中在 ImageButton 的兩種外觀(狀態)之間切換的最佳實踐。 就像 Androids AlarmClock 中的時鍾符號一樣,它有 2 個不同的狀態並且位於 RecyclerView 中:我需要 10 個聲望才能發布圖像,所以這里有一個帶有鬧鍾列表圖片的網站: https : //www.pctipp.ch/ androidtipp/artikel/android-coole-wecker-features-in-samsung-und-aehnlichen-smartphones-81200/
我已經找到了一個類似案例的公認答案: setImageResource Vs setDrawable它說 setImageResource 可能是最糟糕的解決方案,但如果我采用其他解決方案,我的 ImageButton 有問題,例如它會失去以前的大小。 更重要的是,它看起來必須做得更多,例如“decodeResource”。 我認為它的性能不好,因為它對每一行都執行。 我知道 setImageDrawable 總是在 setImageBitmap 時被調用,所以我認為 setImageBitmap 不是正確的答案。
此代碼是我的 RecyclerView 適配器中的 onBindViewHolder 的一部分:
if (alarmAtPosition.isActive()){
//Bitmap bm = BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.ic_access_alarms_black_24dp);
Drawable myDrawable = Resources.getSystem().getDrawable(R.drawable.ic_access_alarms_black_24dp);
viewHolder.imageButton.setImageDrawable(myDrawable);
//viewHolder.imageButton.setImageResource(R.drawable.ic_access_alarms_black_24dp);
}
else {
//Bitmap bm2 = BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.ic_settings_black_24dp);
Drawable myDrawable2 = Resources.getSystem().getDrawable(R.drawable.ic_settings_black_24dp);
viewHolder.imageButton.setImageDrawable(myDrawable2);
//viewHolder.imageButton.setImageResource(R.drawable.ic_settings_black_24dp);
}
在我的 row.xml 中,如果我使用 setImageResource,我不需要放置android:src="@drawable/ic_settings_black_24dp
。所以我采用這個解決方案會容易android:src="@drawable/ic_settings_black_24dp
,但接受的答案讓我不確定。請告訴我:哪個性能最好?
我有一個更好的方法來切換圖像(僅適用於 2 個狀態)。
創建一個 drawable 讓我們說favourite.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/your_checked_drawable"/>
<item android:drawable="@drawable/your_unchecked_drawable"/>
</selector>
現在,無論您在何處添加了要更改圖像的imageButton
,請將其替換為:
<CheckBox
android:id="@+id/favourite"
android:button="@null"
android:checked="false"
android:clickable="true"
android:focusable="true"
android:background="@drawable/favourite"
android:layout_width="32dp"
android:layout_height="32dp" />
你完成了!
這基本上會使用您的自定義復選框更改默認復選框可繪制對象,並且您有 2 個狀態。
checkbox.isChecked
!checkbox.isChecked
編輯:您可以將任何動畫附加到復選框,我使用的是:
在 res/anim 下添加這個名為scale.xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.7"
android:toYScale="0.7"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"
android:repeatMode="reverse"
android:repeatCount="1"/>
</set>
然后在您的活動中:
private lateinit var mAnimation: Animation
mAnimation = AnimationUtils.loadAnimation(this, R.anim.scale)
checkbox.setOnClickListener {
it.startAnimation(mAnimation)
}
你去吧。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.