I want to Rotate Image according to a specific angle in android ,some thing like a compass...
I have this code...it works on drawPath() but i want to replace the path and the Drawing thing with image.. I tried to create a bitmap image ,DrawBitmapImage , but the image does not Rotate like the path..Any Help PLease?
public void draw(Canvas canvas) { double angle = calculateAngle(currentLongitude, currentLatitude, targetLongitude, targetLatitude); //Correction; angle-=90; //Correction for azimuth angle-=azimuth; if((getContext() instanceof Activity) && ((Activity)getContext()).getWindowManager().getDefaultDisplay().getOrientation()==Configuration.ORIENTATION_PORTRAIT)angle-=90; while(angle<0)angle=angle+360; Rect rect = canvas.getClipBounds(); int height = rect.bottom-rect.top; int width = rect.right-rect.left; int left = rect.left; int top = rect.top; if(height>width){ top+=(height-width)/2; height=width; } if(width>height){ left+=(width-height)/2; width=height; } float centerwidth = width/2f; float centerheight = height/2f; Paint p = new Paint(); p.setColor(color); p.setStyle(Paint.Style.FILL); p.setAntiAlias(true); float startX = left+(float)(centerwidth+Math.cos(deg2rad(angle))*width/3.0); float startY = top+(float)(centerheight+Math.sin(deg2rad(angle))*height/3.0); Path path = new Path(); path.moveTo( startX, startY); path.lineTo( left+(float)(centerwidth+Math.cos(deg2rad(angle+140))*width/4.0), top+(float)(centerheight+Math.sin(deg2rad(angle+140))*height/4.0)); path.lineTo( left+(float)centerwidth, top+(float)centerheight ); path.lineTo( left+(float)(centerwidth+Math.cos(deg2rad(angle+220))*width/4.0), top+(float)(centerheight+Math.sin(deg2rad(angle+220))*height/4.0) ); path.lineTo( startX, startY ); canvas.drawPath(path, p); }
The rotate() method allows you to rotate a drawing object on the canvas. The rotate() method accepts a rotation angle in radians. If the angle is positive, the rotation is clockwise. In case the angle is negative, the rotation is counterclockwise.
To change the photo's perspective, tap Transform . Drag the dots to the edges of your desired photo or tap Auto. To rotate a photo 90 degrees, tap Rotate . To make minor adjustments to straighten the photo, use the dial above Rotate .
Option 3. Press Ctrl + T within your Photoshop canvas and enter the Free Transform mode. Then right-click on the image and you'll have options to flip it.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With