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);
141 drawBackground(canvas);
144 drawReferenceLap(canvas);
147 drawCurrentLap(canvas);
158 private void drawBackground(Canvas canvas)
160 int width = canvas.getWidth();
161 int height = canvas.getHeight();
163 Rect rect = new Rect(0,0, width, height);
164 Paint paint = new Paint();
165 paint.setColor(Color.BLACK);
166 paint.setStyle(Paint.Style.FILL);
167 canvas.drawRect(rect, paint);
174 private void drawReferenceLap(Canvas canvas)
176 if ((refLapTimeList == null)||(refLapTimeList.size() <= 0))
181 float width = canvas.getWidth();
182 float height = canvas.getHeight();
184 Paint paint = new Paint();
185 paint.setColor(Color.BLUE);
186 paint.setStyle(Paint.Style.STROKE);
187 paint.setStyle(Paint.Style.FILL);
188 paint.setStrokeWidth(0.0f);
189 paint.setAntiAlias(true);
191 float boxWidthUnit = width / (totalLaptimeCount);
192 float boxHeightUnit = height / (maxLaptime * 1.2f);
195 for (Long time : refLapTimeList)
197 RectF barRect = new RectF(startX, (height - boxHeightUnit * time), (startX + boxWidthUnit), height);
198 canvas.drawRect(barRect, paint);
199 startX = startX + boxWidthUnit;
208 private void drawCurrentLap(Canvas canvas)
210 if (curLapTimeList == null)
215 float width = canvas.getWidth();
216 float height = canvas.getHeight();
218 Paint paint = new Paint();
219 paint.setColor(Color.WHITE);
220 paint.setStyle(Paint.Style.STROKE);
221 paint.setStyle(Paint.Style.FILL);
222 paint.setStrokeWidth(0.0f);
223 paint.setAntiAlias(true);
225 int minimumLapTime = 15;
226 float boxWidthUnit = width / (totalLaptimeCount);
227 float boxHeightUnit = height / (maxLaptime * 1.2f);
228 float circleRadius = (totalLaptimeCount > minimumLapTime) ? (boxWidthUnit / 4.0f) : (width / minimumLapTime / 4.0f);
231 if ((curLapTimeList.size() <= 0)&&(isStarted))
233 long currentLapTime = System.currentTimeMillis() - lastSystemLaptime;
234 canvas.drawCircle((startX + (boxWidthUnit / 2.0f)), (height - boxHeightUnit * currentLapTime), circleRadius, paint);
238 for (Long time : curLapTimeList)
240 canvas.drawCircle((startX + (boxWidthUnit / 2.0f)), (height - boxHeightUnit * time), circleRadius, paint);
241 startX = startX + boxWidthUnit;
246 long currentLapTime = System.currentTimeMillis() - lastSystemLaptime;
247 canvas.drawCircle((startX + (boxWidthUnit / 2.0f)), (height - boxHeightUnit * currentLapTime), circleRadius, paint);
255 private void drawMessage(Canvas canvas)
257 int width = canvas.getWidth();
258 int height = canvas.getHeight();
259 Paint paint = new Paint();
261 paint.setColor(Color.WHITE);
262 paint.setStyle(Paint.Style.STROKE);
263 paint.setStrokeWidth(0.0f);
264 paint.setAntiAlias(true);
265 //canvas.drawRect(rect, paint);
273 if (curTimeList != null)
275 lapCount = curTimeList.size();
278 if (refTimeList != null)
280 refCount = refTimeList.size();
285 long totalTime = curTimeList.get(lapCount - 1) - curTimeList.get(0);
286 long currTime = curTimeList.get(lapCount - 1) - curTimeList.get(lapCount - 2);
287 //long currTime = (lapCount > 2) ? curTimeList.get(lapCount - 1) - curTimeList.get(lapCount - 2) : 0;
288 if ((lapCount <= refCount)&&(refTimeList != null))
290 diffTime = totalTime - (refTimeList.get(lapCount - 1) - refTimeList.get(0));
291 lapTime = currTime - (refTimeList.get(lapCount - 1) - refTimeList.get(lapCount - 2));
292 //lapTime = currTime - ((lapCount > 2) ? refTimeList.get(lapCount - 1) - refTimeList.get(lapCount - 2) : 0);
305 ovearll = "T:" + TimeStringConvert.getDiffTimeString(diffTime).toString();
310 lap = TimeStringConvert.getDiffTimeString(lapTime).toString();
316 float density = context.getResources().getDisplayMetrics().density;
317 paint.setTextSize(density * 20.0f + 0.5f); //
322 paint.setTextSize(32.0f);
324 paint.setAntiAlias(true);
325 Paint.FontMetrics fm = paint.getFontMetrics();
326 float textHeight = fm.descent - fm.ascent;
327 float textWidth = paint.measureText(ovearll);
330 float x = (width < textWidth) ? 0.0f : (width - textWidth - 8.0f);
331 float y = (height - textHeight - 2) - fm.ascent;
333 canvas.drawText(ovearll, x , y, paint); // 全体時間の遅れ・進み
334 canvas.drawText(lap, 0.0f , y, paint); // 前回ラップ時間の遅れ・進み
341 private void parseReferenceTimeList()
343 if (timerCounter == null)
347 refLapTimeList = null;
349 refTimeList = timerCounter.getReferenceLapTimeList();
350 if (refTimeList == null)
354 totalLaptimeCount = refTimeList.size();
356 if (totalLaptimeCount <= 1)
360 refLapTimeList = new ArrayList<>();
361 long prevTime = refTimeList.get(0);
362 for (Long time : refTimeList)
364 long currTime = time - prevTime;
367 refLapTimeList.add(currTime);
369 if (currTime > maxLaptime)
371 maxLaptime = currTime;
381 private void parseLapTime()
383 if (timerCounter == null)
387 curTimeList = timerCounter.getLapTimeList();
388 int lapTimeCount = curTimeList.size();
389 if (lapTimeCount > totalLaptimeCount)
391 totalLaptimeCount = lapTimeCount;
393 if (lapTimeCount < 1)
398 if (lapTimeCount == 1)
400 lastSystemLaptime = curTimeList.get(0);
404 curLapTimeList.clear();
405 long prevTime = curTimeList.get(0);
406 for (Long time : curTimeList)
408 long currTime = time - prevTime;
411 curLapTimeList.add(currTime);
413 if (currTime > maxLaptime)
415 maxLaptime = currTime;
419 lastSystemLaptime = prevTime;
423 public boolean performClick()
425 return (super.performClick());