1

嗨,天才!
有什么问题?
我试图在标记前绘制文本。我将获得清晰的位图并绘制文本,但位图仍然清晰 - 文本在哪里?
我试图将视图转换为位图,但这也不是一个好主意。
PS: scale >= 1

mMap.addMarker(new MarkerOptions()
.position(new LatLng(56.83789, 60.5986))                              .icon(BitmapDescriptorFactory.fromBitmap(drawTextToBitmap(getApplicationContext(), R.drawable.ic_maps_marker,"19"))));

    //here I'm trying to draw text bitmap
             public static Bitmap drawTextToBitmap(Context gContext,int gResId,String gText) {
                    Resources resources = gContext.getResources();
                    float scale = resources.getDisplayMetrics().density;
                    Bitmap bitmap =
                            BitmapFactory.decodeResource(resources, gResId);

                    android.graphics.Bitmap.Config bitmapConfig =
                            bitmap.getConfig();
                    if(bitmapConfig == null) {
                        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
                    }
                    bitmap = bitmap.copy(bitmapConfig, true);

                    Canvas canvas = new Canvas(bitmap);
                    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                    paint.setColor(Color.BLACK);
                    paint.setTextSize((int) (14 * scale));
                    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

                    Rect bounds = new Rect();
                    paint.getTextBounds(gText, 0, gText.length(), bounds);
                    int x = (bitmap.getWidth() - bounds.width())/2;
                    int y = (bitmap.getHeight() + bounds.height())/2;

                    canvas.drawText(gText, x * scale, y * scale, paint);

                    return bitmap;
                }
4

1 回答 1

0

密度只需要确定文本的大小。在画布上绘制文本不应使用比例,因为位移 x 和 y 是针对特定位图计算的。
尝试这个:

canvas.drawText(gText, x, y, paint);

这适用于 4 台设备:

于 2013-03-14T07:46:47.197 回答