Eliminate allocs - Buffer Paint.FontMetrics (#1892)

paint.getFontMetrics() has a hidden allocation.  paint.getFontMetrics(fontMetrics) is used internally inside of Paint, so caching and holding onto a metrics buffer helps reduce allocations.
This commit is contained in:
Tony Patino 2016-06-29 17:48:31 -07:00
parent 424ee02ce2
commit 7fd18d2177
3 changed files with 22 additions and 10 deletions

View file

@ -808,6 +808,8 @@ public class Legend extends ComponentBase {
return mCalculatedLineSizes;
}
protected Paint.FontMetrics fontMetricsForCalculateDimensions = new Paint.FontMetrics();
ArrayList<FSize> calculatedLineSizesForCalculateDimensions = new ArrayList<>();
/**
* Calculates the dimensions of the Legend. This includes the maximum width
* and height of a single entry, as well as the total width and height of
@ -815,7 +817,6 @@ public class Legend extends ComponentBase {
*
* @param labelpaint
*/
ArrayList<FSize> calculatedLineSizesForCalculateDimensions = new ArrayList<>();
public void calculateDimensions(Paint labelpaint, ViewPortHandler viewPortHandler) {
mTextWidthMax = getMaximumEntryWidth(labelpaint);
@ -825,7 +826,7 @@ public class Legend extends ComponentBase {
case VERTICAL: {
float maxWidth = 0f, maxHeight = 0f, width = 0f;
float labelLineHeight = Utils.getLineHeight(labelpaint);
float labelLineHeight = Utils.getLineHeight(labelpaint, fontMetricsForCalculateDimensions);
final int count = mLabels.length;
boolean wasStacked = false;
@ -878,8 +879,8 @@ public class Legend extends ComponentBase {
case HORIZONTAL: {
int labelCount = mLabels.length;
float labelLineHeight = Utils.getLineHeight(labelpaint);
float labelLineSpacing = Utils.getLineSpacing(labelpaint) + mYEntrySpace;
float labelLineHeight = Utils.getLineHeight(labelpaint, fontMetricsForCalculateDimensions);
float labelLineSpacing = Utils.getLineSpacing(labelpaint, fontMetricsForCalculateDimensions) + mYEntrySpace;
float contentWidth = viewPortHandler.contentWidth() * mMaxSizePercent;
// Prepare arrays for calculated layout

View file

@ -177,6 +177,7 @@ public class LegendRenderer extends Renderer {
mLegend.calculateDimensions(mLegendLabelPaint, mViewPortHandler);
}
protected Paint.FontMetrics fontMetricsForRenderLegent = new Paint.FontMetrics();
public void renderLegend(Canvas c) {
if (!mLegend.isEnabled())
@ -190,8 +191,8 @@ public class LegendRenderer extends Renderer {
mLegendLabelPaint.setTextSize(mLegend.getTextSize());
mLegendLabelPaint.setColor(mLegend.getTextColor());
float labelLineHeight = Utils.getLineHeight(mLegendLabelPaint);
float labelLineSpacing = Utils.getLineSpacing(mLegendLabelPaint) + mLegend.getYEntrySpace();
float labelLineHeight = Utils.getLineHeight(mLegendLabelPaint, fontMetricsForRenderLegent);
float labelLineSpacing = Utils.getLineSpacing(mLegendLabelPaint, fontMetricsForRenderLegent) + mLegend.getYEntrySpace();
float formYOffset = labelLineHeight - Utils.calcTextHeight(mLegendLabelPaint, "ABC") / 2.f;
String[] labels = mLegend.getLabels();

View file

@ -165,13 +165,23 @@ public abstract class Utils {
}
public static float getLineHeight(Paint paint) {
Paint.FontMetrics metrics = paint.getFontMetrics();
return metrics.descent - metrics.ascent;
Paint.FontMetrics metrics = new Paint.FontMetrics();
return getLineHeight(paint, metrics);
}
public static float getLineHeight(Paint paint, Paint.FontMetrics fontMetrics){
paint.getFontMetrics(fontMetrics);
return fontMetrics.descent - fontMetrics.ascent;
}
public static float getLineSpacing(Paint paint) {
Paint.FontMetrics metrics = paint.getFontMetrics();
return metrics.ascent - metrics.top + metrics.bottom;
Paint.FontMetrics metrics = new Paint.FontMetrics();
return getLineSpacing(paint, metrics);
}
public static float getLineSpacing(Paint paint, Paint.FontMetrics fontMetrics){
paint.getFontMetrics(fontMetrics);
return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom;
}
/**