In Android I have an ImageView which user can manipulate the position and zoom level in the UI.
Is it possible to get the zoomed image (changed image) from the ImageView instead of original one.
Here are the methods I tried but no use.
imageView.buildDrawingCache(true);
Bitmap bmp = imageView.getDrawingCache(true);
I can get the Matrix but don't know how to use it.
Matrix m = imageView.getImageMatrix();
The zoom effect is applied at the Canvas level. So, what you can do is get the current Canvas augmentations and then draw your Bitmap into a new one with the changes.
Matrix transformMatrix = imageView.getImageMatrix()
Bitmap original = bmp;
Bitmap adjusted = Bitmap.createBitmap(original.getWidth(),
original.getHeight(),
original.getConfig());
Canvas canvas = new Canvas(adjusted);
canvas.setMatrix(transformMatrix);
canvas.drawBitmap(original, 0, 0, null);
//at this point adjusted bitmap contains the zoomed image
When you get a Matrix from an ImageView, make changes to t, then use imageView.setImageMatrix(m) apply the changes back to the ImageView. You will want to look into using one of the postScale() matrix methods to make a change in the size of image rendered.
I had go through lot of links but still I am confuse how to do it.
I have total 60 images of one object with different angle.When user swipe image right or left then I should be show the particular angle image like image is rotating automatically.
Please suggest me the right way to do so.
Use Matrix rotation.
Matrix matrix = new Matrix();
matrix.postRotate(orientation);
Bitmap rotated = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
I am trying to flip an image view horizontally after putting a Sobel Filter on it. The filter converts the image in black and white and then then I do this to flip the image horizontally.
Matrix matrix = new Matrix();
matrix.preScale( - 1, 1 );
Bitmap resizedBitmap =
Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false );
What happens is really weird. My image flips correctly but the dark spots get whiter. I have attached two images for comparison.
I'm drawing a bunch of icons on a map. Actually the icons come from the same image rotated. But on the map the images take on two different sizes, I don't know why. This is what the result looks like: http://orangesoftware.net/iconmap.png
The image file looks like this: http://orangesoftware.net/arrow18.png
The code to rotate the icon:
Matrix mtx = new Matrix();
mtx.postRotate(unit.heading);
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
R.drawable.arrow18);
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mtx, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
Any magically insights appreciated, thanks
The cause of the variation in sizes is when a rotation is not a multiple of 90 degrees. The bmp becomes a diamond who's corners stick out beyond the ImageView holding it, thus it gets resized to fit the ImageView.
The easiest way to take care of this discrepancy is to set the ImageView's scaleType to CENTER. This will simply center the image inside without scaling it to fit.
While rotating a bitmap using matrix, vertex is not stable..
Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(), mCompPic.getHeight(), matrix, true);
mCompassHud.setImageBitmap(mCompasstemp);
Output of my code is like
-bitmap will rotate.
-vertex of my bitmap is not stable.
-Bitmap is resizing
I need disable image resizing and make the rotation stable.Can you please suggest a solution for this?
Rather than creating your new Bitmap directly from the original, another (more straight-forward, imho) option is to create the resultant Bitmap, create a Canvas with that Bitmap, then do your rotation/translation/scaling on the Canvas and draw the original Bitmap onto the new Bitmap via the Canvas.
Basically, you're looking, then, at:
scaledImage = Bitmap.createBitmap (croppedWidth, croppedHeight, Bitmap.Config.ARGB_8888);
Canvas offscreenCanvas = new Canvas (scaledImage);
Matrix matrix = new Matrix();
matrix.setRotate (rotations, centreX, centreY);
matrix.postScale(scaleX, scaleY);
offscreenCanvas.setMatrix (matrix);
offscreenCanvas.drawBitmap (pickedImage, 0, 0, new Paint(Paint.DITHER_FLAG));
Not sure if this is what your looking for but it might help.
Android uses its built in compatibility features to scale and render a bitmap appropriately for screens with different pixel densities. There are two methods of scaling, pre-scaling and auto-scaling.
It will pre-scale bitmaps' from resources and auto-scales when the bitmap is being drawn internally (which is what your doing be using createBitmap).
Go to http://developer.android.com/guide/practices/screens_support.html and check under:
4.Use density and/or size-specific resources:
Pre-scaling and auto-scaling of bitmaps and nine-patches
I have tried this code, and the rotate is stable at the center of the bitmap
matrix.reset();
matrix.setRotate(degree, Xpos+bitmap.getWidth()/2, Ypos+bitmap.getHeight()/2);
and then in canvas doDraw()
canvas.setMatrix(matrix);
canvas.drawBitmap(bitmap, Xpos, Ypos, null);
canvas.setMatrix(null);
The Xpos and Ypos is the X and Y position of the bitmap
The setMatrix(null), set the matrix to null, so that the rotate didn't affect the after bitmap
And it didn't always create new bitmap, so it's great for performance
I hope that help
I know its an old question but, all answers with code imply a canvas, so heres a solution without a canvas that worked for me :
Matrix matrix = new Matrix();
matrix.postRotate(mDegree,100,100);
mCompasstemp = Bitmap.createBitmap(mCompPic, 0, 0, mCompPic.getWidth(),
mCompPic.getHeight(), matrix, true);
mCompasstemp = Bitmap.createScaledBitmap(mCompassTemp, mCompPic.getWidth(),
mCompic.getHeight(), false);
mCompassHud.setImageBitmap(mCompasstemp);
So basically after rotating your bitmap you rescale it to the desired size. Might not be best practice, but at least you dont have to create an extra canvas if you dont want/need to, since the involving Paint() Object is not an inexpensive operation either.