位图的旋转矩阵 - Android

发布于 2024-11-09 10:54:16 字数 1331 浏览 0 评论 0原文

我有一个自定义地图(画布),我正在其上绘制地图指针。我正在主类的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我做我的改变 2024-11-16 10:54:16

为了以最小的麻烦和除以零风险来正确计算角度,atan2() 应优先于 atan()。以下函数返回从 ab 的非零向量相对于 x 轴的角度:

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);
}

我无法提供有关如何旋转位图的建议给定的角度,因为我不熟悉你的API。

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.

荒路情人 2024-11-16 10:54:16

如果你想旋转 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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文