Kevin Prichard wrote:chonada wrote:I am doing a photoeditor application and we have a button to rotate the image in the canvas (which is actually an image switcher), now while i did the rotation using the Matrix, i found that the image is shrinking when rotating (noticeable when rotating continuously), code snippet given below, any suggestions will be appreciated.Using java Syntax Highlighting
mSwitcher.setDrawingCacheEnabled(true); // mSwitcher is the ImageSwitcher bmp = mSwitcher.getDrawingCache(); aMatrix = new Matrix(); aMatrix.setRotate(90.0f); <span style="font-weight: bold">flippedBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.width(), bmp.height(), aMatrix, false)</span> mSwitcher.setImageDrawable(new BitmapDrawable(flippedBmp)); mSwitcher.setDrawingCacheEnabled(false); Parsed in 0.011 seconds, using GeSHi 1.0.8.4
Thanx in advance.
chonada,
Assuming you are running the above code for each step of a rotation, you are first grabbing bmp from mSwitcher, then rotating it, then putting it back into mSwitcher, right? Once the bmp is rotated into flippedBmp, its dimensions are changed, and are greater. But I think that the classes used will fit the rotated image within the target drawing area, which means shrinking.
Think of the diagonal distances, from corner to corner: if you rotate bmp 45 deg, the width and height of flippedBmp are now greater than the original bmp.
You need to have a bmp that is at least as large as the maximum line distance found in the original bmp, in each dimension. Remember the Pythagorean theorem. With the original image spec of w * h, the image resulting from the rotation will need to be sqrt(w^2 + h^2) in both dimensions, to prevent shrinkage, to give it the maximum room needed to arbitrarily rotate without shrinking.
Try changingUsing java Syntax Highlighting
flippedBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.width(), bmp.height(), aMatrix, false) Parsed in 0.010 seconds, using GeSHi 1.0.8.4
toUsing java Syntax Highlighting
flippedBmp = Bitmap.createBitmap(bmp, 0, 0, Math.hypot(bmp.width(), bmp.height()), Math.hypot(bmp.width(), bmp.height()), aMatrix, false) Parsed in 0.010 seconds, using GeSHi 1.0.8.4
This is just a wild guess, haven't run your code, but I have encountered this in the past.
I'm also trying to do this but with the same problem. The think is that if it doesn't shrink, it doesn't get aligned with a textview that i have before the imageview.
Anyone found an answer to this problem? The hypot doesn't help 'cause it FC with "java.lang.IllegalArgumentException: x + width must be <= bitmap.width()"