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.Path;
8 import android.graphics.Rect;
9 import android.graphics.RectF;
10 import android.util.AttributeSet;
11 import android.view.View;
13 import net.osdn.gokigen.joggingtimer.stopwatch.ITimerCounter;
14 import net.osdn.gokigen.joggingtimer.utilities.TimeStringConvert;
16 import java.util.ArrayList;
17 import java.util.List;
23 public class LapTimeGraphView extends View
25 private Context context = null;
26 private ITimerCounter timerCounter = null;
27 private boolean isStarted = false;
28 private long maxLaptime = 0;
29 private long lastSystemLaptime = 0;
30 private int totalLaptimeCount = 0;
32 private List<Long> refTimeList = null;
33 private List<Long> refLapTimeList = null;
35 private List<Long> curTimeList = null;
36 private List<Long> curLapTimeList = null;
42 public LapTimeGraphView(Context context)
45 initComponent(context);
52 public LapTimeGraphView(Context context, AttributeSet attrs)
54 super(context, attrs);
55 initComponent(context);
62 public LapTimeGraphView(Context context, AttributeSet attrs, int defStyleAttr)
64 super(context, attrs, defStyleAttr);
65 initComponent(context);
71 private void initComponent(Context context)
73 this.context = context;
74 setWillNotDraw(false);
75 curLapTimeList = new ArrayList<>();
82 public void setITimerCounter(ITimerCounter counter)
84 timerCounter = counter;
85 parseReferenceTimeList();
93 public void notifyStarted(long startTime)
95 curLapTimeList.clear();
96 lastSystemLaptime = startTime;
104 public void notifyLapTime()
115 public void notifyStopped()
126 public void notifyReset()
128 curLapTimeList.clear();
137 protected void onDraw(Canvas canvas)
139 super.onDraw(canvas);
144 drawBackground(canvas);
147 drawReferenceLap(canvas);
150 drawCurrentLap(canvas);
166 private void drawBackground(Canvas canvas)
168 int width = canvas.getWidth();
169 int height = canvas.getHeight();
171 Rect rect = new Rect(0,0, width, height);
172 Paint paint = new Paint();
173 paint.setColor(Color.BLACK);
174 paint.setStyle(Paint.Style.FILL);
175 canvas.drawRect(rect, paint);
177 // Log.v("Canvas", "("+ width + "," + height + ")");
184 private void drawReferenceLap(Canvas canvas)
186 if ((refLapTimeList == null)||(refLapTimeList.size() <= 0))
191 float width = canvas.getWidth();
192 float height = canvas.getHeight();
194 Paint paint = new Paint();
195 paint.setColor(Color.BLUE);
196 paint.setStyle(Paint.Style.STROKE);
197 paint.setStyle(Paint.Style.FILL);
198 paint.setStrokeWidth(0.0f);
199 paint.setAntiAlias(true);
201 float boxWidthUnit = width / (totalLaptimeCount);
202 float boxHeightUnit = height / (maxLaptime * 1.2f);
205 for (Long time : refLapTimeList)
207 RectF barRect = new RectF(startX, (height - boxHeightUnit * time), (startX + boxWidthUnit), height);
208 canvas.drawRect(barRect, paint);
209 startX = startX + boxWidthUnit;
218 private void drawCurrentLap(Canvas canvas)
220 if (curLapTimeList == null)
225 float width = canvas.getWidth();
226 float height = canvas.getHeight();
228 Paint paint = new Paint();
229 paint.setColor(Color.WHITE);
230 paint.setColor(Color.GRAY);
231 paint.setStyle(Paint.Style.FILL_AND_STROKE);
232 paint.setStrokeWidth(0.0f);
233 paint.setAntiAlias(true);
235 int minimumLapTime = 15;
236 float boxWidthUnit = width / (totalLaptimeCount);
237 float boxHeightUnit = height / (maxLaptime * 1.2f);
238 float circleRadius = (totalLaptimeCount > minimumLapTime) ? (boxWidthUnit / 4.0f) : (width / minimumLapTime / 4.0f);
241 if ((curLapTimeList.size() <= 0)&&(isStarted))
243 long currentLapTime = System.currentTimeMillis() - lastSystemLaptime;
244 canvas.drawCircle((startX + (boxWidthUnit / 2.0f)), (height - boxHeightUnit * currentLapTime), circleRadius, paint);
248 Path linePath = new Path();
249 linePath.moveTo(0.0f, height); // 0原点から線を引く
250 for (Long time : curLapTimeList)
252 float cX = (startX + (boxWidthUnit / 2.0f));
253 float cY = (height - boxHeightUnit * time);
254 linePath.lineTo(cX, cY);
255 canvas.drawCircle(cX, cY, circleRadius, paint);
256 startX = startX + boxWidthUnit;
260 long currentLapTime = System.currentTimeMillis() - lastSystemLaptime;
261 float cX = (startX + (boxWidthUnit / 2.0f));
262 float cY = (height - boxHeightUnit * currentLapTime);
263 linePath.lineTo(cX, cY);
264 canvas.drawCircle(cX, cY, circleRadius, paint);
266 paint.setStyle(Paint.Style.STROKE);
267 paint.setColor(Color.GRAY);
268 canvas.drawPath(linePath, paint);
275 private void drawMessage(Canvas canvas)
277 boolean drawTextLeft = false;
278 int width = canvas.getWidth();
279 int height = canvas.getHeight();
280 Paint paint = new Paint();
282 paint.setColor(Color.WHITE);
283 paint.setStyle(Paint.Style.STROKE);
284 paint.setStrokeWidth(0.0f);
285 paint.setAntiAlias(true);
286 //canvas.drawRect(rect, paint);
295 if (curTimeList != null)
297 lapCount = curTimeList.size();
300 if (refTimeList != null)
302 refCount = refTimeList.size();
307 long totalTime = curTimeList.get(lapCount - 1) - curTimeList.get(0);
308 currTime = curTimeList.get(lapCount - 1) - curTimeList.get(lapCount - 2);
309 //long currTime = (lapCount > 2) ? curTimeList.get(lapCount - 1) - curTimeList.get(lapCount - 2) : 0;
310 if ((lapCount <= refCount)&&(refTimeList != null))
312 diffTime = totalTime - (refTimeList.get(lapCount - 1) - refTimeList.get(0));
313 lapTime = currTime - (refTimeList.get(lapCount - 1) - refTimeList.get(lapCount - 2));
314 //lapTime = currTime - ((lapCount > 2) ? refTimeList.get(lapCount - 1) - refTimeList.get(lapCount - 2) : 0);
323 String textToDraw = "";
324 if ((lapTime == 0)&&(diffTime == 0)&&(lapCount > 1))
326 textToDraw = "[" + (lapCount - 1) + "] " + TimeStringConvert.getTimeString(currTime).toString();
333 textToDraw = TimeStringConvert.getDiffTimeString(lapTime).toString();
337 // 「前回ラップ時間の遅れ・進み / 全体時間の遅れ・進み」
338 textToDraw = textToDraw + " / " + TimeStringConvert.getDiffTimeString(diffTime).toString();
345 float density = context.getResources().getDisplayMetrics().density;
346 paint.setTextSize(density * 20.0f + 0.5f);
351 paint.setTextSize(32.0f);
353 paint.setAntiAlias(true);
354 Paint.FontMetrics fm = paint.getFontMetrics();
355 float textHeight = fm.descent - fm.ascent;
356 float textWidth = paint.measureText(textToDraw);
359 float x = (width < textWidth) ? 0.0f : (width - textWidth - 8.0f);
360 float y = (height - textHeight - 2) - fm.ascent;
365 canvas.drawText(textToDraw, x , y, paint);
372 private void parseReferenceTimeList()
374 if (timerCounter == null)
378 refLapTimeList = null;
380 refTimeList = timerCounter.getReferenceLapTimeList();
381 if (refTimeList == null)
385 totalLaptimeCount = refTimeList.size();
387 if (totalLaptimeCount <= 1)
391 refLapTimeList = new ArrayList<>();
392 long prevTime = refTimeList.get(0);
393 for (Long time : refTimeList)
395 long currTime = time - prevTime;
398 refLapTimeList.add(currTime);
400 if (currTime > maxLaptime)
402 maxLaptime = currTime;
412 private void parseLapTime()
414 if (timerCounter == null)
418 curTimeList = timerCounter.getLapTimeList();
419 int lapTimeCount = curTimeList.size();
420 if (lapTimeCount > totalLaptimeCount)
422 totalLaptimeCount = lapTimeCount;
424 if (lapTimeCount < 1)
429 if (lapTimeCount == 1)
431 lastSystemLaptime = curTimeList.get(0);
435 curLapTimeList.clear();
436 long prevTime = curTimeList.get(0);
437 for (Long time : curTimeList)
439 long currTime = time - prevTime;
442 curLapTimeList.add(currTime);
444 if (currTime > maxLaptime)
446 maxLaptime = currTime;
450 lastSystemLaptime = prevTime;
454 public boolean performClick()
456 return (super.performClick());