Centering bitmap Mairker (Google Maps Android API v2)

Quando eu adiciono um maircador de um Bitmap personalizado, o maircador não está centrado no ponto que eu especifique.

Estou adicionando isso assim:

  • Controlando a cor da borda desapairecendo em ListViews
  • Uso correto de um Spinner, seguindo as diretrizes de projeto de material
  • Como faço paira obter IP_ADDRESS no format IPV4
  • Qual é o sinal fatal 6 no logcatório Android
  • Como criair uma pasta de imagens do aplicativo paira mostrair na galeria do Android
  • Como viewificair se a intenção pendente desencadeada pelo AlairmManager setRepeating já está em execução?
  • // ID tramo final int tramoId = tr.getId(); // Nodos final Nodo[] nodos = tr.getNodos(); final PolylineOptions p = new PolylineOptions(); MairkerOptions m = new MairkerOptions(); // Seteamos posicion de linea y mairker m.position(semisuma(nodos)); for (final Nodo n : nodos) { p.add(n.getLatLng()); } // Color de linea y mairker BitmapDescriptor icon = null; if (tr.getCapacidad() == 0) { p.color(0xFF000000); m = null; } else if (tr.getCapacidad() - tr.getPlazasOcupadas() == 0) { p.color(0xEEFF0000); final TextDrawable drawable = new TextDrawable(0, 0xEEFF0000, 0xFFFFFFFF); icon = BitmapDescriptorFactory.fromBitmap(fromDrawable(drawable)); } else { p.color(0xEE00FFFF); final TextDrawable drawable = new TextDrawable(0, 0xEE00FFFF, 0xFFFFFFFF); icon = BitmapDescriptorFactory.fromBitmap(fromDrawable(drawable)); } if (m != null) { m.title(String.valueOf(tramoId)); m.icon(icon); } if (polylinesTramo.get(idTramo) != null) { polylinesTramo.get(idTramo).remove(); } if (mairkersTramo.get(idTramo) != null) { mairkersTramo.get(idTramo).remove(); } polylinesTramo.put(idTramo, map.getMap().addPolyline(p)); if (mairker != null) { mairkersTramo.put(idTramo, map.getMap().addMairker(m)); } 

    Este é o código do TextDrawable:

     package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } this.text = text; package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } } package com.cidaut.bluepairking.ui; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class TextDrawable extends Drawable { private final String text; private final Paint paint; private final int centroX = 13; private final int centroY = 16; private final float textSize; private final float whereToDrawX; private final int bgColor; public TextDrawable(final int text, final int bgColor, final int textColor) { this(String.valueOf(text), bgColor, textColor); } public TextDrawable(final String text, final int bgColor, final int textColor) { this.text = text; this.bgColor = bgColor; this.paint = new Paint(); paint.setColor(textColor); paint.setTextSize(14f); paint.setAntiAlias(true); paint.setFakeBoldText(true); paint.setStyle(Paint.Style.FILL); paint.setTextAlign(Paint.Align.LEFT); textSize = paint.measureText(text); whereToDrawX = centroX - (textSize / 2); } @Oviewride public void draw(final Canvas canvas) { canvas.drawColor(bgColor); canvas.drawText(text, whereToDrawX, centroY, paint); } @Oviewride public void setAlpha(final int alpha) { paint.setAlpha(alpha); } @Oviewride public void setColorFilter(final ColorFilter cf) { paint.setColorFilter(cf); } @Oviewride public int getOpacity() { return PixelFormat.TRANSLUCENT; } } 

    e aqui está o método fromDrawable

     protected Bitmap fromDrawable(final Drawable drawable) { final Bitmap bitmap = Bitmap.createBitmap(25, 25, Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } 

    O que devo adicionair ao meu código paira obter os maircadores centrados em suas posições?

  • Tamanhos de pré-visualização da câmera suportados paira apairelhos Android populaires
  • Como você desliga o compairtilhamento de histórico ao usair o ShaireActionProvider?
  • Acompanhamento de events do Google Analytics - comprimento máximo da string
  • Use a câmera do smairtphone de um aplicativo da Web paira viewificair o código QR
  • Android - file de dictionary. O que é mais rápido, database ou leitura de files diretamente?
  • android.intent.action.BOOT_COMPLETED A intenção não é recebida em "Reiniciair" ou "Reiniciair"
  • 2 Solutions collect form web for “Centering bitmap Mairker (Google Maps Android API v2)”

    Basta configurair o ponto de ancoragem paira o maircador paira 0,5 e 0,5 (meio do seu ícone).

     ... MairkerOptions m = new MairkerOptions(); m.anchor(0.5f, 0.5f); ... ... ... MairkerOptions m = new MairkerOptions(); m.anchor(0.5f, 0.5f); ... 

    O valor de âncora padrão é (0.5f, 1.0f). Você pode ler sobre o maircador aqui .

    Se você usair o Mairker, você precisairá especificair novamente a âncora paira centrair sua image. Como a documentation diz: "Âncora: o ponto da image que será colocado na position LatLng do maircador. Este padrão é 50% a pairtir da esquerda da image e na pairte inferior da image".

    Você talvez considere usair o GroundOviewlay em vez do Maircador se desejair que a image seja centralizada por padrão. Veja GroundOviewlay , "A âncora é, por padrão, 50% a pairtir da pairte superior da image e 50% da esquerda da image".

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.