3 回答
TA贡献1853条经验 获得超9个赞
依靠getConstantState()单独可导致假阴性。
我采用的方法是尝试在第一个实例中比较ConstantState,但是如果该检查失败,则退回到Bitmap比较中。
这在所有情况下都应适用(包括不是资源的图像),但请注意,这会占用大量内存。
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贡献1770条经验 获得超3个赞
我的问题是仅比较两个可绘制对象,我尝试但无法获得直接比较两个可绘制对象的任何方法,但是对于我的解决方案,我将可绘制对象更改为位图,然后比较两个位图,此方法是可行的。
Bitmap bitmap = ((BitmapDrawable)fDraw).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)sDraw).getBitmap();
if(bitmap == bitmap2)
{
//Code blcok
}
- 3 回答
- 0 关注
- 384 浏览
添加回答
举报