2012-09-09 27 views
1

我有兩個圖像一個圓形和一個箭頭。我計算當前GPS座標和地圖上選定的座標之間的距離。現在我需要將箭頭放在圓形圖像上並放在箭頭計算的距離上。我怎樣才能實現這個? 喜歡的東西如何在Android中創建自定義指南針?

enter image description here

編輯: 如何放置箭頭圖像中心和文字上的箭頭?

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


} 

enter image description here

+1

http://developer.android.com/reference/android/graphics/Canvas.html #drawBitmap%28android.graphics.Bitmap,%20android.graphics.Rect,%20android.graphics.RectF,%20android.graphics.Paint%29 – 2012-09-09 09:02:36

回答

0

您有一個示例應用程序。轉到新建 - >項目 - > Android - > Android示例項目。在那裏選擇ApiDemoes。有你有全碼的羅盤應用

0

這看起來很有希望:

// 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?

+0

我改變了,但圖片是一樣的。我從後面或從箭頭創建了新的畫布,但是我得到了相同的結果。我錯在哪裏?你能檢查編輯代碼嗎? – cashmere