4

これは、サークル内にサークルを描画し、現在の場所をサークルの中心として使用する以下のコードです。そして、私は現在の場所に星座マーカーを表示しています。

問題文:-

以下のコードは正常に機能していますが、私が抱えている唯一の問題は、Star Sign Marker円の中心に来ていないことです。私はStar Sign Marker (android.R.drawable.star_on)常に円の中心にいる必要があります。

imageNames[0]以下のコードの円の中心(星座マーカー)です。

どんな提案も私に大いに役立つでしょう。

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

        Point screenPts = new Point();
        mapView.getProjection().toPixels(pointToDraw, screenPts);
        //--------------draw circle----------------------
        Point pt = mapView.getProjection().toPixels(pointToDraw,screenPts);
        Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(0x30000000);
        circlePaint.setStyle(Style.FILL_AND_STROKE);

        int totalCircle=4;
        int radius=40;
        int centerimagesize=13;

        for (int i = 1; i <= totalCircle; i ++) { 
            canvas.drawCircle(screenPts.x,screenPts.y, i*radius, circlePaint); 
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),imageNames[i]), ((screenPts.x)+(i*radius)),(screenPts.y), null);
        } 

        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),imageNames[0]), (screenPts.x-(centerimagesize/2)),(screenPts.y-(centerimagesize/2)), null);

        super.draw(canvas,mapView,shadow);

        return true;
    }

以下は、エミュレータで現在取得しているスクリーンショットです。常に円の中心に星座が必要です。私はアンドロイド4.1を実行しています

ここに画像の説明を入力してください

4

1 に答える 1

1

私には、マーカーのサイズを考慮に入れるのを忘れていたようです。おそらく次のような場所から描画を開始する必要があります。

screenPts.x - (centerimagesize + starSignWidth)/2
screenPts.y - (centerimagesize + starSignHeight)/2

実際に行うことは、画面中央の左上隅にマーカーを描画し始めるようにキャンバスに指示することだけです。これは、スター マーカーの中心が画面の中心と同じではないことを意味します。

Canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint) の API は次のように述べています。

左上隅を (x,y) にして、指定されたビットマップを描画します。

http://developer.android.com/reference/android/graphics/Canvas.html#drawBitmap%28android.graphics.Bitmap,%20float,%20float,%20android.graphics.Paint%29

これはまさにあなたがしていることです。マーカーの左上隅が画面の中央にあります。

于 2012-08-16T17:11:01.170 に答える