【问题标题】:How to create custom compass in Android?如何在 Android 中创建自定义指南针?
【发布时间】:2012-09-09 08:53:14
【问题描述】:

我有两个图像,一个圆圈和一个箭头。我计算我当前的 GPS 坐标和地图上选择的坐标之间的距离。现在我需要将箭头放在圆形图像上并放在箭头计算距离上。我该如何实施? 类似的东西

编辑: 如何将箭头图像放置在中心并在箭头上放置文字?

 public class CompassOverlay extends Overlay
{  
private float angle;

public CompassOverlay(float angle)
{
    this.angle=angle;

}

  @Override
  public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
    super.draw(canvas, mapView, shadow);           

   Bitmap back =BitmapFactory.decodeResource(mapView.getResources(),R.drawable.tfm_compass_back);
   Bitmap arrowBitmap = BitmapFactory.decodeResource(mapView.getResources(),R.drawable.tfm_compass_arrow);

// Create blank bitmap of equal size for back
   Bitmap canvasBitmap = back.copy(Bitmap.Config.ARGB_8888, true);
   canvasBitmap.eraseColor(0x00000000);

   // Create canvas
    Canvas canvasBack = new Canvas(canvasBitmap);

    // Create rotation matrix
    Matrix rotateMatrix = new Matrix();
    rotateMatrix.setRotate(angle, canvasBack.getWidth()/2, canvasBack.getHeight()/2);

    canvas.drawBitmap(back, 0.0f, 0.0f, null);

   // Draw bitmap onto canvas using matrix
    canvasBack.drawBitmap(arrowBitmap, rotateMatrix, null);

    BitmapDrawable bitmapDrawable= new BitmapDrawable(canvasBitmap);
    canvas.drawBitmap(bitmapDrawable.getBitmap(), 0.0f, 0.0f, null);
    return true;
  }


} 

【问题讨论】:

标签: android overlay


【解决方案1】:

您有一个示例应用程序。转到新建-> 项目-> Android -> Android 示例项目。那里选择 ApiDemoes。那里有一个包含完整代码的 Compass 应用程序

【讨论】:

    【解决方案2】:

    这看起来很有希望:

      // Create rotation matrix
      Matrix rotateMatrix = new Matrix();
      rotateMatrix.setRotate(angle, canvas.getWidth()/2, canvas.getHeight()/2);
    
      // Draw bitmap onto canvas using matrix
      canvas.drawBitmap(arrowBitmap, rotateMatrix, null);
    

    在这里找到:How can I draw an Arrow showing the driving direction in MapView?

    【讨论】:

    • 我改变了,但图片是一样的。我从背面或箭头创建了新的画布,但我得到了相同的结果。我哪里错了?你能检查编辑代码吗?
    猜你喜欢
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 2014-04-25
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多