Home > android > Rotate a bitmap and bitmap is getting blur in android

# Rotate a bitmap and bitmap is getting blur in android

December 22Hits:2

I am developing an android game. and in this I want to move an object along a Bezier path.

my object is rotating with some angle on this curve.

I used this piece of code:

````public void update() {          // check collision with bottom wall if heading down         if (droid.getSpeed().getyDirection() == Speed.DIRECTION_DOWN                 && droid.getY() + droid.getBitmap().getHeight() / 2 >= getHeight()/2 &&                  droid.getY() + droid.getBitmap().getHeight() / 2 <= getHeight()/2+ 100 ) {               int pointX[], pointY[];             pointX = new int[4];             pointY = new int[4];             int  X , Y;                    pointX[0] = getWidth()/2;                 pointX[1] = getWidth()/2 + 50;                 pointX[2] = getWidth()/2 + 25;                 pointX[3] = getWidth()/2;                 pointY[0] = getHeight()/2;                 pointY[1] = getHeight()/2 + 25;                 pointY[2] = getHeight()/2 + 50;                 pointY[3] = getHeight()/2 + 100;                   Log.i(TAG, "In the While Zone of Last Half Zone");                 X = (int) ((pointX[3] * u * u * u) + (3 * pointX[2] * u * u * (1-u)) + (3 * pointX[1] * u * (1-u) * (1-u)) +                      (pointX[0] * (1-u) * (1-u) * (1-u)));                  Y = (int) ((pointY[3] * u * u * u) + (3 * pointY[2] * u * u * (1-u)) + (3 * pointY[1] * u * (1-u) * (1-u)) +                          (pointY[0] * (1-u) * (1-u) * (1-u)));                  Log.i(TAG+" Value of X: ", Double.toString(X));                 Log.i(TAG+" Value of Y: ", Double.toString(Y));                  double deltaX = X - droid.getX();                 double deltaY = Y - droid.getY();                  Log.i("deltaX :", Double.toString(deltaX));                 Log.i("deltaY :", Double.toString(deltaY));                  double angle = Math.atan2(deltaY, deltaX)  ;                 Log.i(TAG+" Angle: ", Double.toString(angle));                 Matrix mtx = new Matrix();                 mtx.preRotate((float) angle);                 mtx.postScale(1, 1);                  Bitmap rotatedBMP = Bitmap.createBitmap(droid.getBitmap(), droid.getX(), droid.getY(),  X - droid.getX(), Y - droid.getY(), mtx, true);                 droid.setBitmap(rotatedBMP);                 droid.curveUpdate(X, Y);                 u = u + 0.001;                 Log.i(TAG+" Value of U: ", Double.toString(u));            }         // check collision with top wall if heading up          // Update the lone droid         else{             droid.update();         }      } `
```

my object is moving and rotating as i supposed but image is getting blur. am not getting why is so.

Please suggest what can be the problem.

Thanks.

Rotating a bitmap will always result in a blurred image - there is no way around that. (Except for rotations of 90/270/360 degrees.) Even high-end programs like photoshop cannot work around the issue - but they mitigate it.

First, a bitmap stores its information pixel per pixel. So you have a large grid of pixels that composes the image. To see it in action, open you favorite bitmap painting tool and zoom in to the max. At that point, you can see the individual pixel that composes an image.

Those pixels are always arranged in a grid of horizontal rows and vertical columns. Always vertical and horizontal. This is where the rotation problem arise...

The image above shows what is going on. In the top left you have an example bitmap. Notice that its rows and column are horizontal and vertical. In the bottom right, you first have the same bitmap, with a slight rotation. But as I explained earlier, you cannot leave rows non-horizontal, so you have to transcribe the rotated image to an horizontal/vertical grid (shown in red in the image).

By the way, each square represents a pixel. As you can see the pixels of the red grid do not align cleanly with the bitmap behind. (In fact the bitmap is now a little bit larger than it used to be - another effect of the rotation.) To figure how each of the grid pixel should be colored, you take an average of the pixels in the bitmap overlapping the pixel you want to draw.

Which, of course, results is colors that are slightly different and therefore a blurred image.

The technique you use to average the color are varied and some result it much much better images, but they also take longer to evaluate.

I suspect that the Android .createBitmap image does do a good job at it. You have a few solutions:

• there may be a different library for bitmap manipulation for Android
• use opengl quad's, they do a good job at rotations
• use image magick to create sprites of pre-rotated images

If you are not using OpenGL, then most games load sprite sheets with rotation already done (see other image).
. You only need a quadrant worth of rotation pre-calculated. It saves memory and you can rotate on the fly to 90/180/270 without loosing quality to cover the other 4 quadrant (going back to the first figure, notice that the grids will align perfectly with rotations of 90/180/270 degrees).

I'm not an android developer but after taking a quick look in to the documentation I will try to speculate...

The last parameter of Bitmap.createBitmap is filter. If it's set then the function will do bi-linear or tri-linear (if there is scale) filter, which will look like blur. I'm afraid that if you disable this you will like the result even less. My guess is that you are using some pixelated art style that will just not work well.

If a pixelated art style is what you are using and is what you want I suggest that you manually draw a few frames on different angles and pick the closest one depending of the normal of the curve.

Note: Bi-linear and tri-linear filtering are what games use these days but the textures should not be super high frequency. Like if you make a checkerboard texture with pixel size squares after transformation it will look really bad no matter what hardware or drawing API you are using.

Tags:android

## Related Articles

• ### Rotate a bitmap and bitmap is getting blur in androidDecember 22

I am developing an android game. and in this I want to move an object along a Bezier path. my object is rotating with some angle on this curve. I used this piece of code: public void update() { // check collision with bottom wall if heading down if (

• ### how to get quality cropped bitmap when straighten applied for imageview in androidFebruary 1

I think it should be duplicate but i didn't get any solution for this question. and my question is : I am developing an application which is having features like Crop with aspect ratios and crop with Rotation and Straightening. for that I'm using thi

• ### Create a bitmap using double values ​​instead of int androidJanuary 27

In my code i need create a Bitmap using double values. It´s important use the correct value. I´m using: Bitmap bmp = Bitmap.createBitmap(int, int, Bitmap.Config.ARGB_8888); ... When i would need something like: Bitmap bmp = Bitmap.createBitmap(double

• ### Why is this bitmap not animating across the screen? Android AppFebruary 13

I'm trying to make a simple ball move across the screen. I have the ball as a bitmap, and I have strings set up for the x and y values. I have a small code saying, if x is less than width add 10, until its greater, then reset to 0. same for y. But th

• ### Using Bitmap for saving image to Gallery in Android application with camera appJanuary 18

I have problems when i use some code from Capture Image from Camera and Display in Activity and https://developer.android.com/training/camera/photobasics.html First, this is in MainActivity.java. I create onClickevent when user press button "snap&quo

• ### Rotate canvas along its center based on user touch - AndroidAugust 13

I want to rotate the canvas circularly on its center axis based on user touch. i want to rotate based on center but its rotating based on top left corner . so i am able to see only 1/4 for rotation of image. any idea.. Like a old phone dialer . I hav

• ### Rotation of bitmap using a frame by frame animationMarch 8

I have one large bitmap that has four frames drawn on it and I only draw one at a time by looping through the bitmap by increments to animate walking. I can get the bitmap to rotate correctly when it is not moving but once the animation starts it sta

• ### Need a good quality bitmap rotation algorithm for AndroidApril 24

I am creating a kaleidoscopic effect on an android tablet. I am using the code below to rotate a slice of an image, but as you can see in the image when rotating a bitmap 60 degrees it distorts it quite a lot (red rectangles) – it is smudging the ima

• ### Scale & rotate Bitmap using Matrix in AndroidJanuary 4

I'm trying to scale and rotate in single operation before creting the final bitmap but the preRotate, postConcat doesn't seem to work. Bitmap bmp = ... original image ... Matrix m = new Matrix() m.setScale(x, y); m.preRotate(degrees, (float) width /

• ### Drawing and rotating bitmaps (with transparency) efficiently in AndroidOctober 9

I just started making a game for Android and I'm already having some issues regarding performance. You can imagine the game as being some kind of Tower Defense game with a top-view, and the enemies can move in all directions, so I need to be able to

• ### How can I scale a Bitmap when rotating the screenMarch 13

I'm working on an application where the user can draw on the screen. The application uses a custom View to handle the drawing (similar to FingerPaint.java). Currently to handle screen rotation, I'm storing the canvases bitmap in a byte array. I'm stu

• ### Avoid rotating bitmap twice from Android cameraJanuary 25

At the moment I have the code: onPreviewFrame(byte[] data) int[] rgbs = new int[480*800]; decodeYUV(rgbs, data, 480, 800); Bitmap bitmap = Bitmap.createBitmap(rgbs, 800, 480, Bitmap.Config.ARGB_8888); Matrix matrix = new Matrix(); matrix.postRotate(9

• ### Trying to rotate a bitmap without succesFebruary 7

I`m editing a bitmap to optimize it for an OCR scan. One of the things I need to do is to rotate the image 270 degrees. I'm using the following code: Matrix matrix = new Matrix(); matrix.PostRotate (270); canvas.DrawBitmap(alteredBitmap, matrix, pain

• ### Drawing mirrored bitmaps in androidOctober 28

I'm trying to learn how to make an animated sprite in android and couldn't figure out how to go about organising my bitmaps. I have a sprite sheet of my character walking to the Right: a bitmap of five copies of a character, equally spaced (every 45p

• ### Android bitmap placingMarch 27

I've recently added an explosion in my game, for when enemies get killed, and it works fine and all, but the explosion wont "spawn" on the enemies x and y. The problem is that I'm not drawing the bitmap from the center, but from its 0,0 position

• ### Trouble understanding Android's doc about downscaling large bitmapsFebruary 11

My question is: how come this doc exists and is posted and re-posted around a lot, and no one has ever encountered a problem with it. My conclusion is that I have misunderstood the logic of the code or the whole topic, but then can someone please exp

• ### Remove overlay in bitmap image April 9

How can I write an application that will crop images in C#? --------------Solutions------------- You can use Graphics.DrawImage to draw a cropped image onto the graphics object from a bitmap. Rectangle cropRect = new Rectangle(...); Bitmap src = Imag

• ### What does the "Bitmap Caching" option do in the Remote Desktop Client?July 17

This setting has always confused me: What exactly does the Bitmap Caching option in the Windows Remote Desktop Client do? I understand that it is supposed to improve connection performance, but how? Is it really that much of a difference? -----------

• ### How to convert poor quality bitmap image to vector?December 5

I'm designing a website for a group which has lost the original digital image for their logo. The only file they have of it is a jpg which was embedded into a word document. The image has everything possible wrong with it: Anti-aliased onto a white b

• ### How to convert a Drawable to a Bitmap?June 14

I would like to set a certain Drawable as the device's wallpaper, but all wallpaper functions accept Bitmaps only. I cannot use WallpaperManager because I'm pre 2.1. Also, my drawables are downloaded from the web and do not reside in R.drawable. ----