如何在可绘制的自定义标记中显示文本 [英] How to show text in drawable custom marker
问题描述
我尝试创建一个自定义标记,显示的文本数字等于下面的示例: b $ b
但是当你运行应用程序时,文本不会显示,只有红色的椭圆出现,没有数字。
$ b
我的代码
InicializeMap()##
< pre $ private $ {code $>私人无效IntriesizeMap()
{
if(_googleMap == null)
{
_googleMap =((MapFragment)getFragmentManager() .findFragmentById(R.id.mapMapeamento))的GetMap();
LatLng pos = new LatLng(23.4555453556,11.145315551);
MapUtils mapUtils = new MapUtils(getApplicationContext());
位图位图= mapUtils.GetBitmapMarker();
标记标记= _googleMap.addMarker(新的MarkerOptions()
.POSITION(POS)
.icon(BitmapDescriptorFactory.fromBitmap(位图))); //检查
如果映射成功与否创建
如果(_googleMap == NULL)
Toast.makeText(getApplicationContext(),Mensagens.erroCriarMapa,Toast.LENGTH_SHORT)。显示();
$ / code $ / pre
$ hr
我的方法来创建位图
public Bitmap GetBitmapMarker()
{
Paint color = new Paint( );
color.setTextSize(35);
color.setColor(Color.BLACK);
int px = _mContext.getResources()。getDimensionPixelSize(R.dimen.map_dot_marker_size);
位图mDotMarkerBitmap = Bitmap.createBitmap(px,px,Bitmap.Config.ARGB_8888);
画布画布=新画布(mDotMarkerBitmap);
canvas.drawText(Hello!,30,40,color);
Drawable shape = _mContext.getResources()。getDrawable(R.drawable.shape_marker_red);
shape.setBounds(0,0,mDotMarkerBitmap.getWidth(),mDotMarkerBitmap.getHeight());
shape.draw(canvas);
返回mDotMarkerBitmap;
$ b $ h2 res / drawable / shape_marker_red
<?xml version =1.0encoding =utf-8?>
< gradient
android:angle =90
android:endColor =#f58383
android:startColor =#ee6464/>
android:width =1dp
android:color =#a13939/>
解决方案我得到了下面的实现,它工作。
实现如下:
MapActivity类
$ b $ pre $ code > MapUtils mapUtils = new MapUtils(getApplicationContext());
位图位图= mapUtils.GetBitmapMarker(getApplicationContext(),R.drawable.marker_blue,1);
标记标记= _googleMap.addMarker(新的MarkerOptions()
.POSITION(POS)
.icon(BitmapDescriptorFactory.fromBitmap(位图)));
MapUtils类
public Bitmap GetBitmapMarker(Context mContext,int resourceId,String mText)
{
try
{
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics()。density;
位图位图= BitmapFactory.decodeResource(resources,resourceId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
//设置默认位图配置如果没有
if(bitmapConfig == null)
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
bitmap = bitmap.copy(bitmapConfig,true);
画布画布=新画布(位图);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setTextSize((int)(14 * scale));
paint.setShadowLayer(1f,0f,1f,Color.DKGRAY);
//将文本绘制到画布中心
Rect bounds = new Rect();
paint.getTextBounds(mText,0,mText.length(),bounds);
int x =(bitmap.getWidth() - bounds.width())/ 2;
int y =(bitmap.getHeight()+ bounds.height())/ 2;
canvas.drawText(mText,x * scale,y * scale,paint);
返回位图;
}
catch(Exception e)
{
return null;
}
}
I'm trying to create a Custom Marker that displays text with a number equal to the example below:
Example:
But when you run the application the text is not displayed, only the red ellipse appears without a number.
My Code
InicializeMap()##
private void InicializeMap()
{
if (_googleMap == null)
{
_googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapMapeamento)).getMap();
LatLng pos = new LatLng(23.4555453556, 11.145315551);
MapUtils mapUtils = new MapUtils(getApplicationContext());
Bitmap bitmap = mapUtils.GetBitmapMarker();
Marker marker = _googleMap.addMarker(new MarkerOptions()
.position(pos)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap)));
// check if map is created successfully or not
if (_googleMap == null)
Toast.makeText(getApplicationContext(), Mensagens.erroCriarMapa, Toast.LENGTH_SHORT).show();
}
}
My method to create the Bitmap
public Bitmap GetBitmapMarker()
{
Paint color = new Paint();
color.setTextSize(35);
color.setColor(Color.BLACK);
int px = _mContext.getResources().getDimensionPixelSize(R.dimen.map_dot_marker_size);
Bitmap mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mDotMarkerBitmap);
canvas.drawText("Hello!", 30, 40, color);
Drawable shape = _mContext.getResources().getDrawable(R.drawable.shape_marker_red);
shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight());
shape.draw(canvas);
return mDotMarkerBitmap;
}
res/drawable/shape_marker_red
<?xml version="1.0" encoding="utf-8"?>
<gradient
android:angle="90"
android:endColor="#f58383"
android:startColor="#ee6464" />
<stroke
android:width="1dp"
android:color="#a13939" />
I got the following implementation and it worked. The implementation was as follows:
MapActivity class
MapUtils mapUtils = new MapUtils(getApplicationContext());
Bitmap bitmap = mapUtils.GetBitmapMarker(getApplicationContext(), R.drawable.marker_blue, "1");
Marker marker = _googleMap.addMarker(new MarkerOptions()
.position(pos)
.icon(BitmapDescriptorFactory.fromBitmap(bitmap)));
MapUtils Class
public Bitmap GetBitmapMarker(Context mContext, int resourceId, String mText)
{
try
{
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
// set default bitmap config if none
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.WHITE);
paint.setTextSize((int) (14 * scale));
paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/2;
int y = (bitmap.getHeight() + bounds.height())/2;
canvas.drawText(mText, x * scale, y * scale, paint);
return bitmap;
}
catch (Exception e)
{
return null;
}
}
这篇关于如何在可绘制的自定义标记中显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!