get bitmap from visible zoomed image android

2019-03-15 23:33发布

问题:

I have an Imageview which is zoomed and rotated(I have used multitouch zooming). So how can i get bitmap of only visible content(I mean when i zoom the image, The part of the bitmap may go out of the screen. So, what i want is the bitmap that is visible on the screen). So is there any built in feature in api to create a small bitmap from a big one? or Is there any function to crop the image using x,y coordinates? actually i want the zoomed or rotated part to go to the next activity

回答1:

If you know all the scale and rotation-values, you can create a Matrix with those values, and apply them to your bitmap via the Bitmap.createBitmap() method.
Example:

Bitmap original = ((BitmapDrawable) yourImageView.getDrawable()).getBitmap();
Matrix matrix = new Matrix();
matrix.setRotate(degrees);
matrix.postScale(scale, scale);
Bitmap result = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);

A faster, but maybe not as pretty solution is to create a bitmap and draw your currently visible view onto that:

Bitmap result = Bitmap.createBitmap(yourImageView.getWidth(), yourImageView.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(result);
yourImageView.draw(c);

After which result should contain exactly what you see on screen.