How to get the whole bitmap attached to an ImageView?

心不动则不痛 提交于 2019-12-03 09:40:37

问题


I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.

I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.

Could I get the whole bitmap attached to a ImageView?


回答1:


If you just want the Bitmap from a ImageView the following code may work for you:-

Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();

I think that's what you wanted.




回答2:


If your drawble is not always an instanceof BitmapDrawable

Note: ImageView should be set before you do this.

Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
    bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
    Drawable d = mImageView.getDrawable();
    bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    d.draw(canvas);
}

Your bitmap is stored in bitmap.

Voila!




回答3:


Easiest way is to set tag in ImageView.

imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap); 

To get Tag from it

imageView.getTag();


来源:https://stackoverflow.com/questions/9741749/how-to-get-the-whole-bitmap-attached-to-an-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!