Rotating Image on A canvas in android
You can either rotate your bitmap when you draw it by using a matrix:
Matrix matrix = new Matrix();
matrix.setRotate(angle, imageCenterX, imageCenterY);
yourCanvas.drawBitmap(yourBitmap, matrix, null);
You can also do it by rotating the canvas before drawing:
yourCanvas.save(Canvas.MATRIX_SAVE_FLAG); //Saving the canvas and later restoring it so only this image will be rotated.
yourCanvas.rotate(-angle);
yourCanvas.drawBitmap(yourBitmap, left, top, null);
yourCanvas.restore();
Pick the one that suits you the best.
Rotate Bitmap on Android Canvas
You just have to call
canvas.rotate(90) :) // 90 is degree.
Canvas returns black image on rotate
It is about the coordinant system. Canvas property has a fill style default:"black". Since your image gets out of the scene you have a black image.
In your situation you are rotating it around (0,0). You should change your image's origin to middle of the image(width/2,height/2).
rotate image in around its center point in canvas
You can use a matrix to rotate. First you set the position (I'm using the coordinates of the bitmap's centre). Then apply a rotation. Then draw using your matrix.
Matrix transform = new Matrix();
transform.setTranslate(xOfCentre, yOfCentre);
transform.preRotate(turnDegrees, width/2, height/2);
canvas.drawBitmap(bitmap, transform, null);
If you want your turning to be animated, then see my answer to "animating and rotating an image...".
How to rotate a bitmap in Android about images center smoothly without oscillatory movement
Here is an example.
I broke it to 3 steps.
The first translate moves the bitmap so that it's center is at 0,0
Then a rotation,
and finally move the bitmap center to where you want it on the canvas.
You don't need the second bitmap.
Matrix matrix = new Matrix();
rotation += 10;
float px = this.viewWidth/2;
float py = this.viewHeight/2;
matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2);
matrix.postRotate(rotation);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, null);
As an optimization, create the Matrix once outside this method and replace the creation with a call to matrix.reset()
Android - paint rotated bitmap into a specific location on canvas
assume you want to draw the bitmap where the center of the bitmap will be at (px,py) canvas coordinates. Have a member variable
Matrix matrix = new Matrix();
and in your onDraw:
matrix.reset();
matrix.postTranslate(-bitmap.getWidth() / 2, -bitmap.getHeight() / 2); // Centers image
matrix.postRotate(angle);
matrix.postTranslate(px, py);
canvas.drawBitmap(bitmap, matrix, null);
Scaling and rotating images on a Canvas on Android
This might help you do what you want to do.
Android Canvas. Moving and Rotating Bitmap along Circular Path based on Touch?
Update: Full example posted on GitHub at https://github.com/jselbie/xkcdclock
Every time you get a touch event, grab the touch point's x,y coordinates and compute the angle of the rotation relative to the center of bitmap. Use that value to determine how much to rotate the bitmap you want draw.
First, let's assume a logical coordinate system in which the center point of your element above is at (0,0) in x,y space.
Therefore, the angle (in degrees) between any touch point relative to the center can be computed as follows:
double ComputeAngle(float x, float y)
{
final double RADS_TO_DEGREES = 360 / (java.lang.Math.PI*2);
double result = java.lang.Math.atan2(y,x) * RADS_TO_DEGREES;
if (result < 0)
{
result = 360 + result;
}
return result;
}
Note - the normalization of negative angles to positive angles. So if the touch point is (20,20), this function above will return 45 degrees.
To make use of this method, your Activity will need the following member variables defined:
float _refX; // x coordinate of last touch event
float _refY; // y coordinate or last touch event
float _rotation; // what angle should the source image be rotated at
float _centerX; // the actual center coordinate of the canvas we are drawing on
float _centerY; // the actual center coordinate of the canvas we are drawing on
Now let's examine how to keep track of touch coordinates to we can always have an up to date "_rotation" variable.
So our "touch handler" for Android will look something like this:
boolean onTouch(View v, MotionEvent event)
{
int action = event.getAction();
int actionmasked = event.getActionMasked();
if (!_initialized)
{
// if we haven't computed _centerX and _centerY yet, just bail
return false;
}
if (actionmasked == MotionEvent.ACTION_DOWN)
{
_refX = event.getX();
_refY = event.getY();
return true;
}
else if (actionmasked == MotionEvent.ACTION_MOVE)
{
// normalize our touch event's X and Y coordinates to be relative to the center coordinate
float x = event.getX() - _centerX;
float y = _centerY - event.getY();
if ((x != 0) && (y != 0))
{
double angleB = ComputeAngle(x, y);
x = _refX - _centerX;
y = _centerY - _refY;
double angleA = ComputeAngle(x,y);
_rotation += (float)(angleA - angleB);
this.invalidate(); // tell the view to redraw itself
}
}
There's some fine details left out such as drawing the actual bitmap. You might also want to handle the ACTION_UP and ACTION_CANCEL events to normalize _rotation to always be between 0 and 360. But the main point is that the above code is a framework for computing the _rotation at which your Bitmap should be drawn on the View. Something like the following:
void DrawBitmapInCenter(Bitmap bmp, float scale, float rotation, Canvas canvas)
{
canvas.save();
canvas.translate(canvas.getWidth()/2, canvas.getHeight()/2);
canvas.scale(scale, scale);
canvas.rotate(rotation);
canvas.translate(-bmp.getWidth()/2, -bmp.getHeight()/2);
canvas.drawBitmap(bmp, 0, 0, _paint);
canvas.restore();
}
Related Topics
How to Debug Android Application Line by Line Using Eclipse
Rotating Image on a Canvas in Android
Android: Mediaplayer Setvolume Function
How to Shrink the Drawable on a Button
Onrequestpermissionsresult Not Being Called in Fragment If Defined in Both Fragment and Activity
How to Call Wi-Fi Settings Screen from My Application Using Android
Error:(26, 0) Gradle Dsl Method Not Found: 'Runproguard()'
Encryption Error on Android 4.2
Sha1 Key for Debug & Release Android Studio MAC , How to Generate Sha1 Release Keys in MAC
Movecamera with Cameraupdatefactory.Newlatlngbounds Crashes
Show Download Progress Inside Activity Using Downloadmanager
How to Detect Whether the Android Phone in Silent Mode Programmatically
What Is Android Keystore File, and What Is It Used For
Scrollview Can Host Only One Direct Child
Android - Get Text Out of Webview
Checking Internet Connection on Android