Android: how to convert whole ImageView to Bitmap?

前端 未结 6 1783
北荒
北荒 2020-11-28 21:37

I have my application that is displaying images with different ratio, resized inside (centerInside) imageView. What I need is to create bitmap from the ImageView including t

相关标签:
6条回答
  • 2020-11-28 22:05

    This is a working code

    imageView.setDrawingCacheEnabled(true);
    imageView.buildDrawingCache();
    Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
    
    0 讨论(0)
  • 2020-11-28 22:07
    try {
            photo.setImageURI(Uri.parse("Location");
            BitmapDrawable drawable = (BitmapDrawable) photo.getDrawable();
            Bitmap bitmap = drawable.getBitmap();
            bitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
            photo.setImageBitmap(bitmap);
    
        } catch (Exception e) {
    
        }
    
    0 讨论(0)
  • 2020-11-28 22:11

    You could just use the imageView's image cache. It will render the entire view as it is layed out (scaled,bordered with a background etc) to a new bitmap.

    just make sure it built.

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

    there's your bitmap as the screen saw it.

    0 讨论(0)
  • 2020-11-28 22:13

    Have you tried:

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    
    0 讨论(0)
  • 2020-11-28 22:15

    Just thinking out loud here (with admittedly little expertise working with graphics in Java) maybe something like this would work?:

    ImageView iv = (ImageView)findViewById(R.id.imageview);
    Bitmap bitmap = Bitmap.createBitmap(iv.getWidth(), iv.getHeight(), Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    iv.draw(canvas);
    

    Out of curiosity, what are you trying to accomplish? There may be a better way to achieve your goal than what you have in mind.

    0 讨论(0)
  • 2020-11-28 22:17

    It works in Kotlin after buildDrawingCache() being deprecated

     // convert imageView to bitmap
    val bitmap = (imageViewId.getDrawable() as BitmapDrawable).getBitmap()
    
    0 讨论(0)
提交回复
热议问题