2016-04-03 61 views
0

在我的項目中,我使用(Volley + NetworkImageView)來下載一些圖像和文本,並在列表視圖中顯示..直到這裏,我沒有任何問題。如何從NetworkImageView獲取位圖?

現在,我想獲得位圖形式NetworkImageView,我嘗試了很多像下面這樣的方法,但其中沒有一個適合我。

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); 
Bitmap bitmap = drawable.getBitmap(); 

另一種方法:

imageView.buildDrawingCache(); 
Bitmap bmap = imageView.getDrawingCache(); 

他們的非工作..

任何幫助表示讚賞,,

回答

1

你不能得到位圖作爲參考這是從來沒有保存在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 
    } 
+0

非常感謝您的回答,那麼你的意思是我可以使用這樣的事情: 位圖btmap =((BitmapDrawable)this.getDrawable())getBitmap (); –

+0

是否正確? –

+0

是的,我已經使用它自己,但更好的檢查類型也相應地,你可以得到一個casttype異常 – djodjo