你不能得到位圖作爲參考這是從來沒有保存在ImageView的。 然而使用你可以得到它:如果您將默認的圖像或錯誤的圖像或任何其他圖像中的任何其他
/**
* Sets a Bitmap as the content of this ImageView.
*
* @param bm The bitmap to set
*/
@android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
但是:
((BitmapDrawable)this.getDrawable()).getBitmap();
怎麼一回事,因爲當你凌空設置ü做到這一點例如,你可能無法獲得BitmapDrawable,但NinePatchDrawable。
這裏是如何檢查:
Drawable dd = image.getDrawable();
if(BitmapDrawable.class.isAssignableFrom(dd.getClass())) {
//good one
Bitmap bb = ((BitmapDrawable)dd).getBitmap();
} else {
//cannot get that one
}
非常感謝您的回答,那麼你的意思是我可以使用這樣的事情: 位圖btmap =((BitmapDrawable)this.getDrawable())getBitmap (); –
是否正確? –
是的,我已經使用它自己,但更好的檢查類型也相應地,你可以得到一個casttype異常 – djodjo