How to get the whole bitmap attached to an ImageView?

前端 未结 3 405
渐次进展
渐次进展 2021-02-04 14: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 th

相关标签:
3条回答
  • 2021-02-04 15:13

    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.

    0 讨论(0)
  • 2021-02-04 15:29

    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!

    0 讨论(0)
  • 2021-02-04 15:30

    Easiest way is to set tag in ImageView.

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

    To get Tag from it

    imageView.getTag();
    
    0 讨论(0)
提交回复
热议问题