2015-05-22 52 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;