1 package net.osdn.gokigen.joggingtimer.stopwatch.graphview;
3 import android.content.Context;
4 import android.graphics.Canvas;
5 import android.graphics.Color;
6 import android.graphics.Paint;
7 import android.graphics.Rect;
8 import android.graphics.RectF;
9 import android.util.AttributeSet;
10 import android.view.View;
12 import net.osdn.gokigen.joggingtimer.stopwatch.ITimerCounter;
13 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
15 import java.util.ArrayList;
16 import java.util.List;
22 public class LapTimeGraphView extends View
24 private Context context = null;
25 private ITimerCounter timerCounter = null;
26 private boolean isStarted = false;
27 private long maxLaptime = 0;
28 private long lastSystemLaptime = 0;
29 private int totalLaptimeCount = 0;
31 private List<Long> refTimeList = null;
32 private List<Long> refLapTimeList = null;
34 private List<Long> curTimeList = null;
35 private List<Long> curLapTimeList = null;
41 public LapTimeGraphView(Context context)
44 initComponent(context);
51 public LapTimeGraphView(Context context, AttributeSet attrs)
53 super(context, attrs);
54 initComponent(context);
61 public LapTimeGraphView(Context context, AttributeSet attrs, int defStyleAttr)
63 super(context, attrs, defStyleAttr);
64 initComponent(context);
70 private void initComponent(Context context)
72 this.context = context;
73 setWillNotDraw(false);
74 curLapTimeList = new ArrayList<>();
81 public void setITimerCounter(ITimerCounter counter)
83 timerCounter = counter;
84 parseReferenceTimeList();
92 public void notifyStarted(long startTime)
94 curLapTimeList.clear();
95 lastSystemLaptime = startTime;
103 public void notifyLapTime()
114 public void notifyStopped()
125 public void notifyReset()
127 curLapTimeList.clear();
136 protected void onDraw(Canvas canvas)
138 super.onDraw(canvas);
143 drawBackground(canvas);
146 drawReferenceLap(canvas);
149 drawCurrentLap(canvas);
165 private void drawBackground(Canvas canvas)
167 int width = canvas.getWidth();
168 int height = canvas.getHeight();
170 Rect rect = new Rect(0,0, width, height);
171 Paint paint = new Paint();
172 paint.setColor(Color.BLACK);
173 paint.setStyle(Paint.Style.FILL);
174 canvas.drawRect(rect, paint);
176 // Log.v("Canvas", "("+ width + "," + height + ")");
183 private void drawReferenceLap(Canvas canvas)
185 if ((refLapTimeList == null)||(refLapTimeList.size() <= 0))
190 float width = canvas.getWidth();
191 float height = canvas.getHeight();
193 Paint paint = new Paint();
194 paint.setColor(Color.BLUE);
195 paint.setStyle(Paint.Style.STROKE);
196 paint.setStyle(Paint.Style.FILL);
197 paint.setStrokeWidth(0.0f);
198 paint.setAntiAlias(true);
200 float boxWidthUnit = width / (totalLaptimeCount);
201 float boxHeightUnit = height / (maxLaptime * 1.2f);
204 for (Long time : refLapTimeList)
206 RectF barRect = new RectF(startX, (height - boxHeightUnit * time), (startX + boxWidthUnit), height);
207 canvas.drawRect(barRect, paint);
208 startX = startX + boxWidthUnit;
217 private void drawCurrentLap(Canvas canvas)
219 if (curLapTimeList == null)
224 float width = canvas.getWidth();
225 float height = canvas.getHeight();
227 Paint paint = new Paint();
228 paint.setColor(Color.WHITE);
229 paint.setStyle(Paint.Style.STROKE);
230 paint.setStyle(Paint.Style.FILL);
231 paint.setStrokeWidth(0.0f);
232 paint.setAntiAlias(true);
234 int minimumLapTime = 15;
235 float boxWidthUnit = width / (totalLaptimeCount);
236 float boxHeightUnit = height / (maxLaptime * 1.2f);
237 float circleRadius = (totalLaptimeCount > minimumLapTime) ? (boxWidthUnit / 4.0f) : (width / minimumLapTime / 4.0f);
240 if ((curLapTimeList.size() <= 0)&&(isStarted))
242 long currentLapTime = System.currentTimeMillis() - lastSystemLaptime;
243 canvas.drawCircle((startX + (boxWidthUnit / 2.0f)), (height - boxHeightUnit * currentLapTime), circleRadius, paint);
247 for (Long time : curLapTimeList)
249 canvas.drawCircle((startX + (boxWidthUnit / 2.0f)), (height - boxHeightUnit * time), circleRadius, paint);
250 startX = startX + boxWidthUnit;
255 long currentLapTime = System.currentTimeMillis() - lastSystemLaptime;
256 canvas.drawCircle((startX + (boxWidthUnit / 2.0f)), (height - boxHeightUnit * currentLapTime), circleRadius, paint);
264 private void drawMessage(Canvas canvas)
266 boolean drawTextLeft = false;
267 int width = canvas.getWidth();
268 int height = canvas.getHeight();
269 Paint paint = new Paint();
271 paint.setColor(Color.WHITE);
272 paint.setStyle(Paint.Style.STROKE);
273 paint.setStrokeWidth(0.0f);
274 paint.setAntiAlias(true);
275 //canvas.drawRect(rect, paint);
284 if (curTimeList != null)
286 lapCount = curTimeList.size();
289 if (refTimeList != null)
291 refCount = refTimeList.size();
296 long totalTime = curTimeList.get(lapCount - 1) - curTimeList.get(0);
297 currTime = curTimeList.get(lapCount - 1) - curTimeList.get(lapCount - 2);
298 //long currTime = (lapCount > 2) ? curTimeList.get(lapCount - 1) - curTimeList.get(lapCount - 2) : 0;
299 if ((lapCount <= refCount)&&(refTimeList != null))
301 diffTime = totalTime - (refTimeList.get(lapCount - 1) - refTimeList.get(0));
302 lapTime = currTime - (refTimeList.get(lapCount - 1) - refTimeList.get(lapCount - 2));
303 //lapTime = currTime - ((lapCount > 2) ? refTimeList.get(lapCount - 1) - refTimeList.get(lapCount - 2) : 0);
312 String textToDraw = "";
313 if ((lapTime == 0)&&(diffTime == 0)&&(lapCount > 1))
315 textToDraw = "[" + (lapCount - 1) + "] " + TimeStringConvert.getTimeString(currTime).toString();
322 textToDraw = TimeStringConvert.getDiffTimeString(lapTime).toString();
326 // 「前回ラップ時間の遅れ・進み / 全体時間の遅れ・進み」
327 textToDraw = textToDraw + " / " + TimeStringConvert.getDiffTimeString(diffTime).toString();
334 float density = context.getResources().getDisplayMetrics().density;
335 paint.setTextSize(density * 20.0f + 0.5f);
340 paint.setTextSize(32.0f);
342 paint.setAntiAlias(true);
343 Paint.FontMetrics fm = paint.getFontMetrics();
344 float textHeight = fm.descent - fm.ascent;
345 float textWidth = paint.measureText(textToDraw);
348 float x = (width < textWidth) ? 0.0f : (width - textWidth - 8.0f);
349 float y = (height - textHeight - 2) - fm.ascent;
354 canvas.drawText(textToDraw, x , y, paint);
361 private void parseReferenceTimeList()
363 if (timerCounter == null)
367 refLapTimeList = null;
369 refTimeList = timerCounter.getReferenceLapTimeList();
370 if (refTimeList == null)
374 totalLaptimeCount = refTimeList.size();
376 if (totalLaptimeCount <= 1)
380 refLapTimeList = new ArrayList<>();
381 long prevTime = refTimeList.get(0);
382 for (Long time : refTimeList)
384 long currTime = time - prevTime;
387 refLapTimeList.add(currTime);
389 if (currTime > maxLaptime)
391 maxLaptime = currTime;
401 private void parseLapTime()
403 if (timerCounter == null)
407 curTimeList = timerCounter.getLapTimeList();
408 int lapTimeCount = curTimeList.size();
409 if (lapTimeCount > totalLaptimeCount)
411 totalLaptimeCount = lapTimeCount;
413 if (lapTimeCount < 1)
418 if (lapTimeCount == 1)
420 lastSystemLaptime = curTimeList.get(0);
424 curLapTimeList.clear();
425 long prevTime = curTimeList.get(0);
426 for (Long time : curTimeList)
428 long currTime = time - prevTime;
431 curLapTimeList.add(currTime);
433 if (currTime > maxLaptime)
435 maxLaptime = currTime;
439 lastSystemLaptime = prevTime;
443 public boolean performClick()
445 return (super.performClick());