mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Remove object allocations during draw
This commit is contained in:
@@ -51,6 +51,7 @@ public class HabitHistoryView extends ScrollableDataView
|
||||
private int nDays;
|
||||
private int todayWeekday;
|
||||
private int colors[];
|
||||
private Rect baseLocation;
|
||||
|
||||
public HabitHistoryView(Context context, Habit habit, int baseSize)
|
||||
{
|
||||
@@ -64,6 +65,8 @@ public class HabitHistoryView extends ScrollableDataView
|
||||
wdays = DateHelper.getShortDayNames();
|
||||
dfMonth = new SimpleDateFormat("MMM", Locale.getDefault());
|
||||
dfYear = new SimpleDateFormat("yyyy", Locale.getDefault());
|
||||
|
||||
baseLocation = new Rect();
|
||||
}
|
||||
|
||||
private void updateDate()
|
||||
@@ -140,7 +143,7 @@ public class HabitHistoryView extends ScrollableDataView
|
||||
{
|
||||
super.onDraw(canvas);
|
||||
|
||||
Rect location = new Rect(0, 0, columnWidth - squareSpacing, columnWidth - squareSpacing);
|
||||
baseLocation.set(0, 0, columnWidth - squareSpacing, columnWidth - squareSpacing);
|
||||
|
||||
previousMonth = "";
|
||||
previousYear = "";
|
||||
@@ -151,11 +154,11 @@ public class HabitHistoryView extends ScrollableDataView
|
||||
|
||||
for (int column = 0; column < nColumns - 1; column++)
|
||||
{
|
||||
drawColumn(canvas, location, currentDate, column);
|
||||
location.offset(columnWidth, -columnHeight);
|
||||
drawColumn(canvas, baseLocation, currentDate, column);
|
||||
baseLocation.offset(columnWidth, -columnHeight);
|
||||
}
|
||||
|
||||
drawAxis(canvas, location);
|
||||
drawAxis(canvas, baseLocation);
|
||||
}
|
||||
|
||||
private void drawColumn(Canvas canvas, Rect location, GregorianCalendar date, int column)
|
||||
|
||||
@@ -40,6 +40,7 @@ public class HabitScoreView extends ScrollableDataView
|
||||
private SimpleDateFormat dfDay;
|
||||
|
||||
private Paint pText, pGraph;
|
||||
private RectF rect, prevRect;
|
||||
|
||||
private int[] colors;
|
||||
private int[] scores;
|
||||
@@ -82,6 +83,9 @@ public class HabitScoreView extends ScrollableDataView
|
||||
|
||||
dfMonth = new SimpleDateFormat("MMM", Locale.getDefault());
|
||||
dfDay = new SimpleDateFormat("d", Locale.getDefault());
|
||||
|
||||
rect = new RectF();
|
||||
prevRect = new RectF();
|
||||
}
|
||||
|
||||
protected void fetchData()
|
||||
@@ -96,14 +100,14 @@ public class HabitScoreView extends ScrollableDataView
|
||||
|
||||
float lineHeight = pText.getFontSpacing();
|
||||
|
||||
RectF rGrid = new RectF(0, 0, nColumns * columnWidth, columnHeight);
|
||||
rGrid.offset(0, headerHeight);
|
||||
drawGrid(canvas, rGrid);
|
||||
rect.set(0, 0, nColumns * columnWidth, columnHeight);
|
||||
rect.offset(0, headerHeight);
|
||||
drawGrid(canvas, rect);
|
||||
|
||||
String previousMonth = "";
|
||||
|
||||
pGraph.setColor(habit.color);
|
||||
RectF prevR = null;
|
||||
prevRect.setEmpty();
|
||||
|
||||
long currentDate = DateHelper.getStartOfToday();
|
||||
|
||||
@@ -122,26 +126,26 @@ public class HabitScoreView extends ScrollableDataView
|
||||
double sRelative = ((double) score) / Habit.MAX_SCORE;
|
||||
int height = (int) (columnHeight * sRelative);
|
||||
|
||||
RectF r = new RectF(0, 0, columnWidth, columnWidth);
|
||||
r.offset(k * columnWidth,
|
||||
rect.set(0, 0, columnWidth, columnWidth);
|
||||
rect.offset(k * columnWidth,
|
||||
headerHeight + columnHeight - height - columnWidth / 2);
|
||||
|
||||
if (prevR != null)
|
||||
if (!prevRect.isEmpty())
|
||||
{
|
||||
drawLine(canvas, prevR, r);
|
||||
drawMarker(canvas, prevR);
|
||||
drawLine(canvas, prevRect, rect);
|
||||
drawMarker(canvas, prevRect);
|
||||
}
|
||||
|
||||
if (k == nColumns - 1) drawMarker(canvas, r);
|
||||
if (k == nColumns - 1) drawMarker(canvas, rect);
|
||||
|
||||
prevR = r;
|
||||
prevRect.set(rect);
|
||||
|
||||
r = new RectF(0, 0, columnWidth, columnHeight);
|
||||
r.offset(k * columnWidth, headerHeight);
|
||||
rect.set(0, 0, columnWidth, columnHeight);
|
||||
rect.offset(k * columnWidth, headerHeight);
|
||||
if (!month.equals(previousMonth))
|
||||
canvas.drawText(month, r.centerX(), r.bottom + lineHeight * 1.2f, pText);
|
||||
canvas.drawText(month, rect.centerX(), rect.bottom + lineHeight * 1.2f, pText);
|
||||
else
|
||||
canvas.drawText(day, r.centerX(), r.bottom + lineHeight * 1.2f, pText);
|
||||
canvas.drawText(day, rect.centerX(), rect.bottom + lineHeight * 1.2f, pText);
|
||||
|
||||
previousMonth = month;
|
||||
currentDate += 7 * DateHelper.millisecondsInOneDay;
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.isoron.uhabits.models.Streak;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class HabitStreakView extends ScrollableDataView
|
||||
{
|
||||
@@ -36,6 +37,8 @@ public class HabitStreakView extends ScrollableDataView
|
||||
private List<Streak> streaks;
|
||||
private long maxStreakLength;
|
||||
private int[] colors;
|
||||
private SimpleDateFormat dfMonth;
|
||||
private Rect rect;
|
||||
|
||||
public HabitStreakView(Context context, Habit habit, int columnWidth)
|
||||
{
|
||||
@@ -45,6 +48,9 @@ public class HabitStreakView extends ScrollableDataView
|
||||
setDimensions(columnWidth);
|
||||
createPaints();
|
||||
createColors();
|
||||
|
||||
dfMonth = new SimpleDateFormat("MMM", Locale.getDefault());
|
||||
rect = new Rect();
|
||||
}
|
||||
|
||||
private void setDimensions(int baseSize)
|
||||
@@ -97,7 +103,6 @@ public class HabitStreakView extends ScrollableDataView
|
||||
|
||||
int nStreaks = streaks.size();
|
||||
int start = nStreaks - nColumns - dataOffset;
|
||||
SimpleDateFormat dfMonth = new SimpleDateFormat("MMM");
|
||||
|
||||
String previousMonth = "";
|
||||
|
||||
@@ -112,14 +117,14 @@ public class HabitStreakView extends ScrollableDataView
|
||||
pBar.setColor(colors[(int) Math.floor(lRelative * 3)]);
|
||||
|
||||
int height = (int) (columnHeight * lRelative);
|
||||
Rect r = new Rect(0, 0, columnWidth - 2, height);
|
||||
r.offset(offset * columnWidth, headerHeight + columnHeight - height);
|
||||
rect.set(0, 0, columnWidth - 2, height);
|
||||
rect.offset(offset * columnWidth, headerHeight + columnHeight - height);
|
||||
|
||||
canvas.drawRect(r, pBar);
|
||||
canvas.drawText(Long.toString(l), r.centerX(), r.top - barHeaderOffset, pBar);
|
||||
canvas.drawRect(rect, pBar);
|
||||
canvas.drawText(Long.toString(l), rect.centerX(), rect.top - barHeaderOffset, pBar);
|
||||
|
||||
if (!month.equals(previousMonth))
|
||||
canvas.drawText(month, r.centerX(), r.bottom + lineHeight * 1.2f, pText);
|
||||
canvas.drawText(month, rect.centerX(), rect.bottom + lineHeight * 1.2f, pText);
|
||||
|
||||
previousMonth = month;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ public class RingView extends View
|
||||
private Paint pRing;
|
||||
private float lineHeight;
|
||||
private String label;
|
||||
private RectF rect;
|
||||
|
||||
public RingView(Context context, int size, int color, float perc, String label)
|
||||
{
|
||||
@@ -45,6 +46,8 @@ public class RingView extends View
|
||||
pRing.setAntiAlias(true);
|
||||
pRing.setTextAlign(Paint.Align.CENTER);
|
||||
|
||||
rect = new RectF();
|
||||
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
@@ -62,21 +65,21 @@ public class RingView extends View
|
||||
float thickness = size * 0.15f;
|
||||
|
||||
pRing.setColor(color);
|
||||
RectF r = new RectF(0, 0, size, size);
|
||||
canvas.drawArc(r, -90, 360 * perc, true, pRing);
|
||||
rect.set(0, 0, size, size);
|
||||
canvas.drawArc(rect, -90, 360 * perc, true, pRing);
|
||||
|
||||
pRing.setColor(Color.rgb(230, 230, 230));
|
||||
canvas.drawArc(r, 360 * perc - 90 + 2, 360 * (1 - perc) - 4, true, pRing);
|
||||
canvas.drawArc(rect, 360 * perc - 90 + 2, 360 * (1 - perc) - 4, true, pRing);
|
||||
|
||||
pRing.setColor(Color.WHITE);
|
||||
r.inset(thickness, thickness);
|
||||
canvas.drawArc(r, -90, 360, true, pRing);
|
||||
rect.inset(thickness, thickness);
|
||||
canvas.drawArc(rect, -90, 360, true, pRing);
|
||||
|
||||
pRing.setColor(Color.GRAY);
|
||||
pRing.setTextSize(size * 0.2f);
|
||||
lineHeight = pRing.getFontSpacing();
|
||||
canvas.drawText(String.format("%.0f%%", perc * 100), r.centerX(),
|
||||
r.centerY() + lineHeight / 3, pRing);
|
||||
canvas.drawText(String.format("%.0f%%", perc * 100), rect.centerX(),
|
||||
rect.centerY() + lineHeight / 3, pRing);
|
||||
|
||||
pRing.setTextSize(size * 0.15f);
|
||||
canvas.drawText(label, size / 2, size + lineHeight * 1.2f, pRing);
|
||||
|
||||
Reference in New Issue
Block a user