I created a ruler that looks like a typical ruler in school. What i want to know is, what kind of formula is this "int size = (i%2.5==0) ? 2000 : 13;". I have successfully solved in creating this view but i don't really know how this formula works. Can anyone explain?

public class ColorRulerView extends View {
Paint paint = new Paint();
static final private float pxinch = 500 / 67.f * 25.4f / 16;
float width, height;
public ColorRulerView(Context context, AttributeSet foo) {
super(context, foo);
setBackgroundColor(Color.TRANSPARENT);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(0);
paint.setAntiAlias(false);
paint.setColor(Color.WHITE);
}
public void onSizeChanged(int w, int h, int oldW, int oldH) {
width = w;
height = h;
}
public void onDraw(Canvas c) {
for (int i = 0; ; ++i) {
float x = i * pxinch;
if (x > 5000) {
break;
}
int size = (i%2.5==0) ? 2000 : 13;
c.drawLine(x, 2, x, size, paint);
}
super.onDraw(c);
}
}
The statement
int size = (i%2.5==0) ? 2000 : 13
is a ternary operation.
lets break this down:
(i % 2.5) : this will produce the reminder when the value of i is divided by 2.5. So, if
i = 3, 3 % 2.5 = 0.5
i = 4, 4 % 2.5 = 1.5
assume i = 3, the result is 0.5 which is not equal to 0. So, size will be 13.
whenever the condition (i % 2.5 == 0) is true, size is assigned a value 2000 else, it is assign 13.
value that can make this condition true are 5, 10, 15 etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With