2015-05-22 51 views
1

我想在我的Android应用程序的谷歌地图上使用自定义标记。 为此我有一个方法,它创建了我的位图。每个位图都是一个包含特殊文本的标记。 我已经做到了这一点,它在我的2.3.3 Android上运行正常。但在其他设备上,它崩溃,因为我不使用mutable Bitmaps。 我将我的代码更改为可变位图,但现在文本不可见,只是没有文本的标记位图。在画布上绘制文字不要使用可变位图

我的方法:

Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.marker); 

Typeface tf = Typeface.create("Helvetica", Typeface.BOLD); 

Paint paint = new Paint(); 
paint.setStyle(Paint.Style.FILL); 
paint.setColor(Color.WHITE); 
paint.setTypeface(tf); 
paint.setTextAlign(Paint.Align.CENTER); 
paint.setTextSize(convertToPixels(context, 8)); 

Rect textRect = new Rect(); 
paint.getTextBounds(markerText, 0, markerText.length(), textRect); 
// THIS LINE IS NEW FOR MUTABLE 
Bitmap mutableBitmap = bm.copy(Bitmap.Config.ARGB_8888, true); 
Canvas canvas = new Canvas(mutableBitmap); 

if(textRect.width() >= (canvas.getWidth() - 4)){ 
    paint.setTextSize(convertToPixels(context, 7)); 
} 
int xPos = (canvas.getWidth()/2) - 2; 
int yPos = (int) ((canvas.getHeight()/2) - ((paint.descent() + paint.ascent())/2)) ; 

canvas.drawText(markerText, xPos, yPos, paint); 

return bm; 

我做错了吗?我只是添加可变的行。

问候

+0

你试图返回mutableBitmap代替BM? –

+0

是的,这是问题所在。答案被标记为已解决。 thx家伙。 – BHuelse

回答

1

你的方法返回您获得使用BitmapFactory.decodeResource()不可改变的位图。

你必须返回你drawed的可变位:

return mutableBitmap;