Android: how to convert a view to a bitmap without capturing the background screen

For my application, I used the zoom zoom function, adapting to the tutorial and creating a custom view http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6- implementing-the-pinch-zoom-gesture / 1847

I am currently working on capturing an enlarged image as a bitmap.

I partially managed to get a bitmap using

setDrawingCacheEnabled(true); Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache()); setDrawingCacheEnabled(false); 

Using this approach, I get both an enlarged image and a background on the screen.

Is there a way to capture only the enlarged image as a bitmap?

+4
source share
3 answers

After you searched a lot, I found a โ€œtemporaryโ€ solution in the same Community, and also thanks for the code from โ€œweakwireโ€ provided in the same post.

Getting coordinates and width / height from matrix

The idea is simple, all I have is to remove the screen background from the scaled image by cropping. The code is a bit long, but it worked for me.

  • First get the size of the original bitmap

     float imgWidth = imgBitmap.getWidth() float imgHeight = imgBitmap.getHeight() 
  • Get the matrix (the zoom function used in my application uses the "trick" values) the matrices used to scale the image and get the scaledWidth and scaledHeight of the original image.

     float[] values = new float[9]; matrix.getValues(values); float globalX = values[2]; float globalY = values[5]; float scaledWidth = values[0]*imgWidth; float scaledHeight = values[4]*imgHeight; // If zoomed Image gets out of screen, I'm trying to crop the image available in the screen by considering image from (0,0) if(globalX<0) globalX = 0; if(globalY<0) globalY = 0; 
  • Finally, grab the view and crop the background In my case ("finalView" is the usual name of the view where scaling takes place)

     setDrawingCacheEnabled(false); destroyDrawingCache(); finalView.setDrawingCacheEnabled(true); Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache()); finalView.setDrawingCacheEnabled(false); //Final Image Width & Height int finalWidth = Math.min((int)width,(int)finalView.getWidth()); int finalHeight = Math.min((int)height,(int)finalView.getHeight()); 

    // crop to get a scaled image

      Bitmap finalBitmap = Bitmap.createBitmap(bm, (int)globalX, (int)globalY, finalWidth ,finalHeight); 

Although this is a workaround, it works for me. If there is an alternative solution, send it.

0
source

Try

 setDrawingCacheEnabled(false) finalView.setDrawingCacheEnabled(true); Bitmap bm = Bitmap.createBitmap(finalView.getDrawingCache()); finalView.setDrawingCacheEnabled(false); 
0
source

This original answer is here , just replace the background part of the background. Hope this helps

 public static Bitmap getBitmapFromView(View view) { if (view == null) return null; //Define a bitmap with the same size as the view Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); //Bind a canvas to it Canvas canvas = new Canvas(returnedBitmap); // draw white background on the canvas canvas.drawColor(Color.WHITE); // draw the view on the canvas view.draw(canvas); //return the bitmap return returnedBitmap; } 
0
source

Source: https://habr.com/ru/post/1385385/


All Articles