位图的旋转矩阵 - Android
我有一个自定义地图(画布),我正在其上绘制地图指针。我正在主类的 onLocationChanged 方法中更新它,但是我正在努力让位图旋转。 getBearing() 似乎不起作用(至少对我来说不起作用),所以我正在努力找到地图上各点之间的坡度。任何帮助将不胜感激。
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);
编辑:
使用纬度和经度坐标来查找方位比简单地在两点之间查找方位更困难。然而,这段代码(根据此处找到的代码修改)效果很好:
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));
}
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));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下是如何旋转位图: Android:如何根据目的地坐标旋转移动的动画精灵
Here is how to rotate a bitmap: Android: How to rotate a moving animated sprite based on the coordinates of its destination
为了以最小的麻烦和除以零风险来正确计算角度,
atan2()
应优先于atan()
。以下函数返回从a
到b
的非零向量相对于 x 轴的角度:我无法提供有关如何旋转位图的建议给定的角度,因为我不熟悉你的API。
To correctly compute the angle with a minimum of fuss and division by zero risks,
atan2()
should be preferred toatan()
. The following function returns the angle relative to the x-axis of a non-zero vector froma
tob
:I can't give advice on how to rotate the bitmap by a given angle, since I am not familiar with your API.
如果你想旋转 ImageView
in case you want to rotate ImageView