3

マップビューにマーカーを配置し、マーカーに数字を書き込む必要があります。私はそれを行いましたが、テキストの配置は解像度によって異なります。以下は参照コードです

        float xVal = (float) curScreenCoords.x;  // Point curScreenCoords
        float yVal = (float) curScreenCoords.y-20; // Point curScreenCoords
        Bitmap bitmap = BitmapFactory.decodeResource ( context.getResources() , ( R.drawable.pin_number ) ) ;
        canvas.drawBitmap(bitmap, xVal, yVal, getInnerPaint()); 

        public Paint getInnerPaint() {
         if (innerPaint == null) {
             innerPaint = new Paint();
         }
        innerPaint.setARGB(255, 117, 161, 220); // blue
        innerPaint.setAntiAlias(true);  
        innerPaint.setStyle(Style.FILL);
        return innerPaint;
        }
        canvas.drawText(String.valueOf(10), xVal+20, yVal+22, getCountPaint()); // 10 is just for example, it can vary to one digit to two to three 
        public Paint getCountPaint() {
        if (innerPaint == null) {
        innerPaint = new Paint();
        }
        innerPaint.setARGB(255, 255, 255, 255); 
        innerPaint.setAntiAlias(true);  
        innerPaint.setStyle(Style.FILL);
        innerPaint.setTextSize(12f);
        innerPaint.setTextAlign(Align.CENTER);
        return innerPaint;
       }

テキストの配置を除いて、すべてが正常に機能します。このコードは、480*800の解像度で正常に機能します。テキストはキャンバスの中央に完全に配置されます。x、yの位置は画像では完璧ですが、320*480では完璧に見えません。この解像度では、テキストのx位置とy位置が異なって見えます。誰かが私に何が間違っているのかを正確に提案できますか?異なるサイズのデバイスで同じことを行うための基本はありますか?前もって感謝します。

4

6 に答える 6

5

テキストがキャンバスに書き込まれたときの幅と高さを測定し、それを使用して中央に配置できると思います。何かのようなもの:

String text = "whatever";
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
canvas.drawText(text, (canvas.getWidth() - bounds.width()) / 2, (canvas.getHeight() - bounds.height()) / 2, paint);
于 2012-12-06T13:10:14.573 に答える
5

こんにちは私は上記の答えのどれも十分に良いとは思わないので、私は私の答えを投稿してみてくださいみんなはすべてのデバイスで動作し、まったく複雑ではありません

Canvas canvas = new Canvas(bitmap);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//paint.setTextAlign(Align.CENTER);
paint.setColor(activity.getResources().getColor(R.color.white));
paint.setTextSize(30);

// draw text to the Canvas center
Rect boundsText = new Rect();
paint.getTextBounds(String.valueOf(cluster.getMarkerList().size()), 0, 
    String.valueOf(cluster.getMarkerList().size()).length(), boundsText);
int x = (bitmap.getWidth() - boundsText.width()) / 2;
int y = (bitmap.getHeight() + boundsText.height()) / 2;

canvas.drawText(String.valueOf(cluster.getMarkerList().size()), x, y, paint);
于 2013-11-06T06:48:48.297 に答える
2

値はすべての解像度で一定のピクセルオフセットcurScreenCoords.y-20xVal+20, yVal+22持っていますが、次のようにデバイスのピクセル密度に依存する必要があります。

xOffset = (int) (13.33f * context.getResources().getDisplayMetrics().density + 0.5f);
yOffset = (int) (14.67f * context.getResources().getDisplayMetrics().density + 0.5f);
canvas.drawText(String.valueOf(10), xVal + xOffset, yVal + yOffset, getCountPaint());
于 2012-12-06T12:51:44.997 に答える
1

ハードウェアアクセラレーションがオフになっていることを確認してください。4.1.2およびその他のデバイス(samsung Galaxy Tag 2.0)では、FatalSignal11エラーが発生します。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

これにより、このコードで発生していた問題が修正されます。canvas.drawTextがエラーの原因でした。

于 2013-09-03T16:47:53.223 に答える
0

Imageクラスを拡張し、そのonDrawをオーバーライドできます。以下のように

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.widget.ImageView;

/**
 * @author amit
 * 
 */
public class CustomImageView extends ImageView {
    private int notificationCount;
    private Paint paint;

    /**
     * @param context
     */
    public CustomImageView(Context context) {
        super(context);
        notificationCount = 0;
        paint = new Paint();
        paint.setColor(Color.RED);
        ColorFilter cf = new ColorFilter();
        paint.setColorFilter(cf);
        paint.setStyle(Paint.Style.FILL);
        paint.setFakeBoldText(true);
        paint.setTextSize(15);
    }

    public synchronized void incrementNotification() {
        notificationCount--;
        this.invalidate();
    }

    public synchronized void decrementNotification() {
        notificationCount++;
        this.invalidate();
    }

    /**
     * @return the notificationCount
     */
    public synchronized int getNotificationCount() {
        return notificationCount;
    }

    /**
     * @param notificationCount
     *            the notificationCount to set
     */
    public synchronized void setNotificationCount(int notificationCount) {
        this.notificationCount = notificationCount;
        this.invalidate();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.ImageView#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        // System.out.println("OnDraw is called");
        super.onDraw(canvas);
        if (notificationCount == 0) {
            return;
        }
        canvas.drawText(String.valueOf(notificationCount), 0, 0, paint);
    }

}

次のメソッドのいずれかを呼び出すことができます

incrementNotification(); 
decrementNotification();
setNotification(int number);

コンストラクター内でお好みの色を選択してください。幸運を祈ります!

于 2012-12-06T12:48:51.487 に答える
0

私も同じ問題を抱えていました。Googleマップでクラスタリングするために、ビットマップ内にテキストを描画したかったのです。実際には、ビットマップの中央にテキストを描画します。

Kotlinで記述されたコード

fun createCircleBitmapWithTextInside(context: Context, @DrawableRes drawableId: Int, text: String = "POI"): Bitmap{
            val scale = context.resources.displayMetrics.density

            var bitmap = getBitmap(context, drawableId)//BitmapFactory.decodeResource(res, drawableId)
            var bitmapConfig = bitmap.config ?: Bitmap.Config.ARGB_8888
            // resource bitmaps are imutable,
            // so we need to convert it to mutable one
            bitmap = bitmap.copy(bitmapConfig, true)

            val canvas = Canvas(bitmap)
            val paint = Paint(ANTI_ALIAS_FLAG)
            paint.color = ResourceUtils.getColor(context, R.color.white)
            paint.textSize = (18 * scale)
            paint.setShadowLayer(1f, 0f, 1f, Color.WHITE)
            paint.style = Paint.Style.FILL
            paint.textAlign = Paint.Align.CENTER

            val bound = Rect()
            paint.getTextBounds(text, 0, text.length, bound)
            val x = (canvas.width.toFloat() /2)
            // x - point to the center of width
            val y = canvas.height / 2 - (paint.descent() + paint.ascent()) / 2

/*            Timber.d("bitmap.width:${canvas.width} \tbound.width:${bound.width()} \tx:$x")
            Timber.d("bitmap.height:${canvas.height} \tbound.height:${bound.height()} \ty:$y")*/

            canvas.drawText(text, x, y, paint)
            return bitmap
        }
于 2017-07-12T11:07:36.600 に答える