3 回答
TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
依靠getConstantState()單獨(dú)可導(dǎo)致假陰性。
我采用的方法是嘗試在第一個(gè)實(shí)例中比較ConstantState,但是如果該檢查失敗,則退回到Bitmap比較中。
這在所有情況下都應(yīng)適用(包括不是資源的圖像),但請(qǐng)注意,這會(huì)占用大量?jī)?nèi)存。
public static boolean areDrawablesIdentical(Drawable drawableA, Drawable drawableB) {
Drawable.ConstantState stateA = drawableA.getConstantState();
Drawable.ConstantState stateB = drawableB.getConstantState();
// If the constant state is identical, they are using the same drawable resource.
// However, the opposite is not necessarily true.
return (stateA != null && stateB != null && stateA.equals(stateB))
|| getBitmap(drawableA).sameAs(getBitmap(drawableB));
}
public static Bitmap getBitmap(Drawable drawable) {
Bitmap result;
if (drawable instanceof BitmapDrawable) {
result = ((BitmapDrawable) drawable).getBitmap();
} else {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// Some drawables have no intrinsic width - e.g. solid colours.
if (width <= 0) {
width = 1;
}
if (height <= 0) {
height = 1;
}
result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
}
return result;
}
TA貢獻(xiàn)1770條經(jīng)驗(yàn) 獲得超3個(gè)贊
我的問(wèn)題是僅比較兩個(gè)可繪制對(duì)象,我嘗試但無(wú)法獲得直接比較兩個(gè)可繪制對(duì)象的任何方法,但是對(duì)于我的解決方案,我將可繪制對(duì)象更改為位圖,然后比較兩個(gè)位圖,此方法是可行的。
Bitmap bitmap = ((BitmapDrawable)fDraw).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)sDraw).getBitmap();
if(bitmap == bitmap2)
{
//Code blcok
}
- 3 回答
- 0 關(guān)注
- 430 瀏覽
添加回答
舉報(bào)
