Skip to content

Commit

Permalink
change tick label
Browse files Browse the repository at this point in the history
  • Loading branch information
anastr committed Aug 8, 2017
1 parent 324889e commit dd4343f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.github.anastr.speedviewlib.components.Indicators.Indicator;
import com.github.anastr.speedviewlib.components.Indicators.NoIndicator;
import com.github.anastr.speedviewlib.components.note.Note;
import com.github.anastr.speedviewlib.util.OnPrintTickLabel;

import java.util.ArrayList;

Expand Down Expand Up @@ -49,6 +50,7 @@ public abstract class Speedometer extends Gauge {
/** to rotate tick label */
private boolean tickRotation = true;
private int tickPadding = (int) (getSpeedometerWidth() + dpTOpx(3f));
private OnPrintTickLabel onPrintTickLabel;

public Speedometer(Context context) {
this(context, null);
Expand Down Expand Up @@ -504,15 +506,21 @@ protected void drawTicks(Canvas c) {
c.rotate(getStartDegree()+90f, getSize() *.5f, getSize() *.5f);
// tick each degree
float tickEach = tickNumber != 1 ? (float)(endDegree - startDegree) / (float)(tickNumber-1) : endDegree +1f;
for (int i=1; i <= tickNumber; i++) {
for (int i=0; i < tickNumber; i++) {
if (!tickRotation) {
c.save();
c.rotate(-(getStartDegree()+90f + tickEach * drawnTick)
, getSize() *.5f, textPaint.getTextSize() + getPadding() + tickPadding);
}

c.drawText(String.format(getLocale(), "%d", (int)getSpeedAtDegree(tickEach * drawnTick + getStartDegree()))
, getSize() *.5f, textPaint.getTextSize() + getPadding() + tickPadding, textPaint);
int tick = (int)getSpeedAtDegree(tickEach * drawnTick + getStartDegree());
String tickLabel;
if (onPrintTickLabel != null)
tickLabel = onPrintTickLabel.getTickLabel(i, tick);
else
tickLabel = String.format(getLocale(), "%d", tick);
c.drawText( tickLabel, getSize() *.5f, textPaint.getTextSize() + getPadding() + tickPadding
, textPaint);
if (!tickRotation)
c.restore();
drawnTick++;
Expand Down Expand Up @@ -633,6 +641,18 @@ public void setTickPadding(int tickPadding) {
invalidate();
}

/**
* create custom Tick label.
* @param onPrintTickLabel maybe null, The callback that will run.
*/
public void setOnPrintTickLabel(OnPrintTickLabel onPrintTickLabel) {
this.onPrintTickLabel = onPrintTickLabel;
if (!isAttachedToWindow())
return;
updateBackgroundBitmap();
invalidate();
}

/**
* @return correct position of center X to use in drawing.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.anastr.speedviewlib.util;

/**
* Created by Anas Altair.
*/

public interface OnPrintTickLabel {
/**
* @param tickPosition position of ticks, start from 0.
* @param tick speed value at the tick.
* @return label to draw.
*/
String getTickLabel(int tickPosition, int tick);
}

0 comments on commit dd4343f

Please sign in to comment.