Reputation: 36289
I have a custom map (canvas) that I am drawing a map pointer on. I am updating it in my onLocationChanged method of my main class, however I am struggling to get the bitmap to rotate. getBearing() doesn't seem to work (at least not for me) and so I am working to find the slope between points on the map. Any help would be greatly appreciated.
public void setBearing(Point prev, Point curr){
float slope = 1;
if (prev.x - curr.x !=0){
slope = (float) ((y1-y2)/(x1-x2));
bearing = (float) Math.atan(slope);
}
}
...
Paint p = new Paint();
Matrix matrix = new Matrix();
matrix.postRotate(bearing, coords.x, coords.y);
Bitmap rotatedImage = Bitmap.createBitmap(image, 0, 0, image.getWidth(),
image.getHeight(), matrix, true);
canvas.drawBitmap(rotatedImage, x-image.getWidth()/2, y-image.getHeight()/2, p);
Edit:
Using latitude and longitude coordinates to find a bearing is more difficult than simply between two points. This code however (modified from code found here) works well:
public void setBearing(Location one, Location two){
double lat1 = one.getLatitude();
double lon1 = one.getLongitude();
double lat2 = two.getLatitude();
double lon2 = two.getLongitude();
double deltaLon = lon2-lon1;
double y = Math.sin(deltaLon) * Math.cos(lat2);
double x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(deltaLon);
bearing = (float) Math.toDegrees(Math.atan2(y, x));
}
Upvotes: 2
Views: 4973
Reputation: 2775
in case you want to rotate ImageView
private void rotateImage(ImageView imageView, double angle) {
Matrix matrix = new Matrix();
imageView.setScaleType(ScaleType.MATRIX); // required
matrix.postRotate((float) angle, imageView.getDrawable().getBounds()
.width() / 2, imageView.getDrawable().getBounds().height() / 2);
imageView.setImageMatrix(matrix);
}
Upvotes: 0
Reputation: 21639
Here is how to rotate a bitmap: Android: How to rotate a moving animated sprite based on the coordinates of its destination
Upvotes: 2
Reputation: 8361
To correctly compute the angle with a minimum of fuss and division by zero risks, atan2()
should be preferred to atan()
. The following function returns the angle relative to the x-axis of a non-zero vector from a
to b
:
public float getBearing(Point a, Point b) { // Valid for a != b.
float dx = b.x - a.x;
float dy = b.y - a.y;
return (float)Math.atan2(dy, dx);
}
I can't give advice on how to rotate the bitmap by a given angle, since I am not familiar with your API.
Upvotes: 2