GeezerVison is showing github.com — Eliminate allocs - Rect and RectF buffers (#1892) · FlyingAndroid/MPAndroidChart@d372fd3
File treeExpand file treeCollapse file tree Open diff view settings MPChartExample/src/com/xxmassdeveloper/mpchartexample MPChartLib/src/main/java/com/github/mikephil/charting
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivity.java +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | import android.annotation.SuppressLint; | 5 | 5 | import android.graphics.PointF; | | 6 | +import android.graphics.Rect; | 6 | 7 | import android.graphics.RectF; | 7 | 8 | import android.os.Bundle; | 8 | 9 | import android.util.Log; | @@ -257,14 +258,16 @@ private void setData(int count, float range) { | 257 | 258 | } | 258 | 259 | } | 259 | 260 |
| | 261 | + protected RectF mOnValueSelectedRectF = new RectF(); | 260 | 262 | @SuppressLint("NewApi") | 261 | 263 | @Override | 262 | 264 | public void onValueSelected(Entry e, Highlight h) { | 263 | 265 |
| 264 | 266 | if (e == null) | 265 | 267 | return; | 266 | 268 |
| 267 | | - RectF bounds = mChart.getBarBounds((BarEntry) e); | | 269 | + RectF bounds = mOnValueSelectedRectF; | | 270 | + mChart.getBarBounds((BarEntry) e, bounds); | 268 | 271 | MPPointF position = mChart.getPosition(e, AxisDependency.LEFT); | 269 | 272 |
| 270 | 273 | Log.i("bounds", bounds.toString()); | |
Collapse file Copy file name to clipboardExpand all lines: MPChartExample/src/com/xxmassdeveloper/mpchartexample/HorizontalBarChartActivity.java +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | import android.annotation.SuppressLint; | 5 | 5 | import android.graphics.PointF; | | 6 | +import android.graphics.Rect; | 6 | 7 | import android.graphics.RectF; | 7 | 8 | import android.os.Bundle; | 8 | 9 | import android.util.Log; | @@ -246,14 +247,17 @@ private void setData(int count, float range) { | 246 | 247 | } | 247 | 248 | } | 248 | 249 |
| | 250 | + protected RectF mOnValueSelectedRectF = new RectF(); | 249 | 251 | @SuppressLint("NewApi") | 250 | 252 | @Override | 251 | 253 | public void onValueSelected(Entry e, Highlight h) { | 252 | 254 |
| 253 | 255 | if (e == null) | 254 | 256 | return; | 255 | 257 |
| 256 | | - RectF bounds = mChart.getBarBounds((BarEntry) e); | | 258 | + RectF bounds = mOnValueSelectedRectF; | | 259 | + mChart.getBarBounds((BarEntry) e, bounds); | | 260 | + | 257 | 261 | MPPointF position = mChart.getPosition(e, mChart.getData().getDataSetByIndex(h.getDataSetIndex()) | 258 | 262 | .getAxisDependency()); | 259 | 263 |
| |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarChart.java +25-6Lines changed: 25 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -98,17 +98,37 @@ public Highlight getHighlightByTouchPoint(float x, float y) { | 98 | 98 |
| 99 | 99 | /** | 100 | 100 | * Returns the bounding box of the specified Entry in the specified DataSet. Returns null if the Entry could not be | 101 | | - * found in the charts data. | | 101 | + * found in the charts data. Performance-intensive code should use void getBarBounds(BarEntry, RectF) instead. | 102 | 102 | * | 103 | 103 | * @param e | 104 | 104 | * @return | 105 | 105 | */ | 106 | 106 | public RectF getBarBounds(BarEntry e) { | 107 | 107 |
| | 108 | + | | 109 | + RectF bounds = new RectF(); | | 110 | + getBarBounds(e, bounds); | | 111 | + | | 112 | + return bounds; | | 113 | + } | | 114 | + | | 115 | + /** | | 116 | + * The passed outputRect will be assigned the values of the bounding box of the specified Entry in the specified DataSet. | | 117 | + * The rect will be assigned Float.MIN_VALUE in all locations if the Entry could not be found in the charts data. | | 118 | + * | | 119 | + * @param e | | 120 | + * @return | | 121 | + */ | | 122 | + public void getBarBounds(BarEntry e, RectF outputRect){ | | 123 | + | | 124 | + RectF bounds = outputRect; | | 125 | + | 108 | 126 | IBarDataSet set = mData.getDataSetForEntry(e); | 109 | 127 |
| 110 | | - if (set == null) | 111 | | - return null; | | 128 | + if (set == null) { | | 129 | + bounds.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE); | | 130 | + return; | | 131 | + } | 112 | 132 |
| 113 | 133 | float y = e.getY(); | 114 | 134 | float x = e.getX(); | @@ -120,11 +140,10 @@ public RectF getBarBounds(BarEntry e) { | 120 | 140 | float top = y >= 0 ? y : 0; | 121 | 141 | float bottom = y <= 0 ? y : 0; | 122 | 142 |
| 123 | | - RectF bounds = new RectF(left, top, right, bottom); | | 143 | + bounds.set(left, top, right, bottom); | 124 | 144 |
| 125 | | - getTransformer(set.getAxisDependency()).rectValueToPixel(bounds); | | 145 | + getTransformer(set.getAxisDependency()).rectValueToPixel(outputRect); | 126 | 146 |
| 127 | | - return bounds; | 128 | 147 | } | 129 | 148 |
| 130 | 149 | /** | |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java +7-5Lines changed: 7 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -141,12 +141,15 @@ protected float[] getMarkerPosition(Highlight high) { | 141 | 141 | } | 142 | 142 |
| 143 | 143 | @Override | 144 | | - public RectF getBarBounds(BarEntry e) { | | 144 | + public void getBarBounds(BarEntry e, RectF outputRect) { | 145 | 145 |
| | 146 | + RectF bounds = outputRect; | 146 | 147 | IBarDataSet set = mData.getDataSetForEntry(e); | 147 | 148 |
| 148 | | - if (set == null) | 149 | | - return null; | | 149 | + if (set == null){ | | 150 | + outputRect.set(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE); | | 151 | + return; | | 152 | + } | 150 | 153 |
| 151 | 154 | float y = e.getY(); | 152 | 155 | float x = e.getX(); | @@ -158,11 +161,10 @@ public RectF getBarBounds(BarEntry e) { | 158 | 161 | float left = y >= 0 ? y : 0; | 159 | 162 | float right = y <= 0 ? y : 0; | 160 | 163 |
| 161 | | - RectF bounds = new RectF(left, top, right, bottom); | | 164 | + bounds.set(left, top, right, bottom); | 162 | 165 |
| 163 | 166 | getTransformer(set.getAxisDependency()).rectValueToPixel(bounds); | 164 | 167 |
| 165 | | - return bounds; | 166 | 168 | } | 167 | 169 |
| 168 | 170 | protected float[] mGetPositionBuffer = new float[2]; | |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.java +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -725,6 +725,7 @@ protected void drawCenterText(Canvas c) { | 725 | 725 | } | 726 | 726 | } | 727 | 727 |
| | 728 | + protected RectF mDrawHighlightedRectF = new RectF(); | 728 | 729 | @Override | 729 | 730 | public void drawHighlighted(Canvas c, Highlight[] indices) { | 730 | 731 |
| @@ -743,7 +744,8 @@ public void drawHighlighted(Canvas c, Highlight[] indices) { | 743 | 744 | ? radius * (mChart.getHoleRadius() / 100.f) | 744 | 745 | : 0.f; | 745 | 746 |
| 746 | | - final RectF highlightedCircleBox = new RectF(); | | 747 | + final RectF highlightedCircleBox = mDrawHighlightedRectF; | | 748 | + highlightedCircleBox.set(0,0,0,0); | 747 | 749 |
| 748 | 750 | for (int i = 0; i < indices.length; i++) { | 749 | 751 |
| |
Collapse file Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java +6-2Lines changed: 6 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -147,6 +147,7 @@ public static int calcTextWidth(Paint paint, String demoText) { | 147 | 147 | return (int) paint.measureText(demoText); | 148 | 148 | } | 149 | 149 |
| | 150 | + private static Rect mCalcTextHeightRect = new Rect(); | 150 | 151 | /** | 151 | 152 | * calculates the approximate height of a text, depending on a demo text | 152 | 153 | * avoid repeated calls (e.g. inside drawing methods) | @@ -157,7 +158,8 @@ public static int calcTextWidth(Paint paint, String demoText) { | 157 | 158 | */ | 158 | 159 | public static int calcTextHeight(Paint paint, String demoText) { | 159 | 160 |
| 160 | | - Rect r = new Rect(); | | 161 | + Rect r = mCalcTextHeightRect; | | 162 | + r.set(0,0,0,0); | 161 | 163 | paint.getTextBounds(demoText, 0, demoText.length(), r); | 162 | 164 | return r.height(); | 163 | 165 | } | @@ -188,6 +190,7 @@ public static FSize calcTextSize(Paint paint, String demoText) { | 188 | 190 | return result; | 189 | 191 | } | 190 | 192 |
| | 193 | + private static Rect mCalcTextSizeRect = new Rect(); | 191 | 194 | /** | 192 | 195 | * calculates the approximate size of a text, depending on a demo text | 193 | 196 | * avoid repeated calls (e.g. inside drawing methods) | @@ -198,7 +201,8 @@ public static FSize calcTextSize(Paint paint, String demoText) { | 198 | 201 | */ | 199 | 202 | public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) { | 200 | 203 |
| 201 | | - Rect r = new Rect(); | | 204 | + Rect r = mCalcTextSizeRect; | | 205 | + r.set(0,0,0,0); | 202 | 206 | paint.getTextBounds(demoText, 0, demoText.length(), r); | 203 | 207 | outputFSize.width = r.width(); | 204 | 208 | outputFSize.height = r.height(); | |
|