[android] Reuse Bitmap, Paint & Canvas instances in LocalGlyphRasterizer

- Make LocalGlyphRasterizer.drawGlyphBitmap() non-static
- Store and reuse Bitmap, Paint & Canvas instances
This commit is contained in:
Kiyong Jung
2018-07-19 12:08:40 +09:00
committed by Tobrun
parent 94ad8e2ff8
commit 37bbed5b9c
3 changed files with 46 additions and 24 deletions

View File

@@ -1,8 +1,10 @@
package com.mapbox.mapboxsdk.text;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.support.annotation.Keep;
import android.support.annotation.WorkerThread;
@@ -12,6 +14,25 @@ import android.support.annotation.WorkerThread;
* by the portable local_glyph_rasterizer.hpp
*/
public class LocalGlyphRasterizer {
private final Bitmap bitmap;
private final Paint paint;
private final Canvas canvas;
LocalGlyphRasterizer() {
/*
35x35px dimensions are hardwired to match local_glyph_rasterizer.cpp
These dimensions are large enough to draw a 24 point character in the middle
of the bitmap (y: 20) with some buffer around the edge
*/
bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888);
paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(24);
canvas = new Canvas();
canvas.setBitmap(bitmap);
}
/***
* Uses Android-native drawing code to rasterize a single glyph
@@ -26,23 +47,10 @@ public class LocalGlyphRasterizer {
*/
@WorkerThread
@Keep
protected static Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) {
/*
35x35px dimensions are hardwired to match local_glyph_rasterizer.cpp
These dimensions are large enough to draw a 24 point character in the middle
of the bitmap (y: 20) with some buffer around the edge
*/
Bitmap bitmap = Bitmap.createBitmap(35, 35, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(24);
protected Bitmap drawGlyphBitmap(String fontFamily, boolean bold, char glyphID) {
paint.setTypeface(Typeface.create(fontFamily, bold ? Typeface.BOLD : Typeface.NORMAL));
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawText(String.valueOf(glyphID), 0, 20, paint);
return bitmap;
}
}