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