nit and style fixes

Signed-off-by: Harry Bond <me@hbond.xyz>
This commit is contained in:
Harry Bond
2025-08-30 19:56:24 +01:00
parent ea1d0eefa2
commit 3c1b8be5ce
2 changed files with 48 additions and 47 deletions

View File

@@ -72,16 +72,16 @@ public abstract class BaseSignView extends View
} }
@Override @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
super.onSizeChanged(w, h, oldw, oldh); super.onSizeChanged(width, height, oldWidth, oldHeight);
float px = getPaddingLeft() + getPaddingRight(); final float paddingX = getPaddingLeft() + getPaddingRight();
float py = getPaddingTop() + getPaddingBottom(); final float paddingY = getPaddingTop() + getPaddingBottom();
mWidth = w - px; mWidth = width - paddingX;
mHeight = h - py; mHeight = height - paddingY;
mRadius = Math.min(mWidth, mHeight) / 2f; mRadius = Math.min(mWidth, mHeight) / 2f;
mBorderWidth = mRadius * mBorderWidthRatio; mBorderWidth = mRadius * mBorderWidthRatio;
// subtract half the stroke PLUS the extra inset // subtract half the stroke PLUS the extra inset
float gap = mRadius * mBorderInsetRatio; final float gap = mRadius * mBorderInsetRatio;
mBorderRadius = mRadius - (mBorderWidth / 2f) - gap; mBorderRadius = mRadius - (mBorderWidth / 2f) - gap;
configureTextSize(); configureTextSize();
} }
@@ -90,11 +90,11 @@ public abstract class BaseSignView extends View
protected void onDraw(@NonNull Canvas canvas) protected void onDraw(@NonNull Canvas canvas)
{ {
super.onDraw(canvas); super.onDraw(canvas);
String str = getValueString(); final String str = getValueString();
if (str == null) return; if (str == null) return;
float cx = mWidth / 2f; final float cx = mWidth / 2f;
float cy = mHeight / 2f; final float cy = mHeight / 2f;
// background & border // background & border
boolean alert = isAlert(); boolean alert = isAlert();
@@ -115,9 +115,9 @@ public abstract class BaseSignView extends View
@Override @Override
public boolean onTouchEvent(@NonNull MotionEvent e) public boolean onTouchEvent(@NonNull MotionEvent e)
{ {
float cx = mWidth / 2f, cy = mHeight / 2f; final float cx = mWidth / 2f, cy = mHeight / 2f;
float dx = e.getX() - cx, dy = e.getY() - cy; final float dx = e.getX() - cx, dy = e.getY() - cy;
if (dx*dx + dy*dy <= mRadius*mRadius) if ((dx * dx) + (dy * dy) <= (mRadius * mRadius))
{ {
performClick(); performClick();
return true; return true;
@@ -136,7 +136,7 @@ public abstract class BaseSignView extends View
{ {
Rect b = new Rect(); Rect b = new Rect();
mTextPaint.getTextBounds(str, 0, str.length(), b); mTextPaint.getTextBounds(str, 0, str.length(), b);
float y = cy - b.exactCenterY(); final float y = cy - b.exactCenterY();
c.drawText(str, cx, y, mTextPaint); c.drawText(str, cx, y, mTextPaint);
} }
@@ -144,10 +144,10 @@ public abstract class BaseSignView extends View
{ {
String text = getValueString(); String text = getValueString();
if (text == null) return; if (text == null) return;
float textRadius = mBorderRadius - mBorderWidth; final float textRadius = mBorderRadius - mBorderWidth;
float maxSz = 2f * textRadius; final float maxTextSize = 2f * textRadius;
float maxSz2 = maxSz * maxSz; final float maxTextSize2 = maxTextSize * maxTextSize;
float lo = 0f, hi = maxSz, sz = maxSz; float lo = 0f, hi = maxTextSize, sz = maxTextSize;
Rect b = new Rect(); Rect b = new Rect();
while (lo <= hi) while (lo <= hi)
{ {
@@ -155,8 +155,10 @@ public abstract class BaseSignView extends View
mTextPaint.setTextSize(sz); mTextPaint.setTextSize(sz);
mTextPaint.getTextBounds(text, 0, text.length(), b); mTextPaint.getTextBounds(text, 0, text.length(), b);
float area = b.width()*b.width() + b.height()*b.height(); float area = b.width()*b.width() + b.height()*b.height();
if (area <= maxSz2) lo = sz + 1f; if (area <= maxTextSize2)
else hi = sz - 1f; lo = sz + 1f;
else
hi = sz - 1f;
} }
mTextPaint.setTextSize(Math.max(1f, sz)); mTextPaint.setTextSize(Math.max(1f, sz));
} }

View File

@@ -25,24 +25,23 @@ public class SpeedLimitView extends BaseSignView
setBorderWidthRatio(0.2f); setBorderWidthRatio(0.2f);
setBorderInsetRatio(0.05f); setBorderInsetRatio(0.05f);
try (TypedArray a = ctx.getTheme() try (TypedArray styleAttrs = ctx.getTheme().obtainStyledAttributes(attrs, R.styleable.SpeedLimitView, 0, 0))
.obtainStyledAttributes(attrs, R.styleable.SpeedLimitView, 0, 0))
{ {
int bg = a.getColor(R.styleable.SpeedLimitView_speedLimitBackgroundColor, DefaultValues.BACKGROUND_COLOR); final int bgColor = styleAttrs.getColor(R.styleable.SpeedLimitView_speedLimitBackgroundColor, DefaultValues.BACKGROUND_COLOR);
int bd = a.getColor(R.styleable.SpeedLimitView_speedLimitBorderColor, DefaultValues.BORDER_COLOR); final int borderColor = styleAttrs.getColor(R.styleable.SpeedLimitView_speedLimitBorderColor, DefaultValues.BORDER_COLOR);
int al = a.getColor(R.styleable.SpeedLimitView_speedLimitAlertColor, DefaultValues.ALERT_COLOR); final int alertColor = styleAttrs.getColor(R.styleable.SpeedLimitView_speedLimitAlertColor, DefaultValues.ALERT_COLOR);
int tc = a.getColor(R.styleable.SpeedLimitView_speedLimitTextColor, DefaultValues.TEXT_COLOR); final int textColor = styleAttrs.getColor(R.styleable.SpeedLimitView_speedLimitTextColor, DefaultValues.TEXT_COLOR);
int tac = a.getColor(R.styleable.SpeedLimitView_speedLimitTextAlertColor, DefaultValues.TEXT_ALERT_COLOR); final int txtAlertColor = styleAttrs.getColor(R.styleable.SpeedLimitView_speedLimitTextAlertColor, DefaultValues.TEXT_ALERT_COLOR);
setColors(bg, bd, al, tc, tac); setColors(bgColor, borderColor, alertColor, textColor, txtAlertColor);
unlimitedBorderColor = a.getColor(R.styleable.SpeedLimitView_speedLimitUnlimitedBorderColor, DefaultValues.UNLIMITED_BORDER_COLOR); unlimitedBorderColor = styleAttrs.getColor(R.styleable.SpeedLimitView_speedLimitUnlimitedBorderColor, DefaultValues.UNLIMITED_BORDER_COLOR);
unlimitedStripeColor = a.getColor(R.styleable.SpeedLimitView_speedLimitUnlimitedStripeColor, DefaultValues.UNLIMITED_STRIPE_COLOR); unlimitedStripeColor = styleAttrs.getColor(R.styleable.SpeedLimitView_speedLimitUnlimitedStripeColor, DefaultValues.UNLIMITED_STRIPE_COLOR);
if (isInEditMode()) if (isInEditMode())
{ {
mSpeedLimit = a.getInt(R.styleable.SpeedLimitView_speedLimitEditModeSpeedLimit, 60); mSpeedLimit = styleAttrs.getInt(R.styleable.SpeedLimitView_speedLimitEditModeSpeedLimit, 60);
mAlert = a.getBoolean(R.styleable.SpeedLimitView_speedLimitEditModeAlert, false); mAlert = styleAttrs.getBoolean(R.styleable.SpeedLimitView_speedLimitEditModeAlert, false);
mSpeedStr = Integer.toString(mSpeedLimit); mSpeedStr = Integer.toString(mSpeedLimit);
} }
} }
} }
@@ -76,9 +75,9 @@ public class SpeedLimitView extends BaseSignView
@Override @Override
protected void onDraw(Canvas canvas) protected void onDraw(Canvas canvas)
{ {
float cx = mWidth/2f, cy = mHeight/2f; final float cx = mWidth/2f, cy = mHeight/2f;
if (mSpeedLimit == 0) if (mSpeedLimit == 0) // 0 means unlimited speed (maxspeed=none)
{ {
// background // background
mBackgroundPaint.setColor(mBackgroundColor); mBackgroundPaint.setColor(mBackgroundColor);
@@ -101,24 +100,24 @@ public class SpeedLimitView extends BaseSignView
private void drawUnlimitedStripes(Canvas c, float cx, float cy) private void drawUnlimitedStripes(Canvas c, float cx, float cy)
{ {
Paint stripe = new Paint(Paint.ANTI_ALIAS_FLAG); final Paint stripe = new Paint(Paint.ANTI_ALIAS_FLAG);
stripe.setColor(unlimitedStripeColor); stripe.setColor(unlimitedStripeColor);
stripe.setStrokeWidth(mBorderWidth * 0.4f); stripe.setStrokeWidth(mBorderWidth * 0.4f);
float r = mRadius * 0.8f; // shorten to 80% of full radius final float radius = mRadius * 0.8f; // Shorten to 80% of full radius
float diag = (float)(1/Math.sqrt(2)); final float diag = (float) (1/Math.sqrt(2)); // 45 degrees
float dx = -diag, dy = +diag; final float dx = -diag, dy = +diag;
float px = -dy, py = +dx; // perpendicular final float px = -dy, py = +dx; // Perpendicular
float step = r * 0.15f; // spacing final float step = radius * 0.15f; // Spacing
for (int i = -2; i <= 2; i++) for (int i = -2; i <= 2; i++)
{ {
float ox = px * step * i; final float ox = px * step * i;
float oy = py * step * i; final float oy = py * step * i;
float sx = cx + dx * r + ox; final float sx = cx + dx * radius + ox;
float sy = cy + dy * r + oy; final float sy = cy + dy * radius + oy;
float ex = cx - dx * r + ox; final float ex = cx - dx * radius + ox;
float ey = cy - dy * r + oy; final float ey = cy - dy * radius + oy;
c.drawLine(sx, sy, ex, ey, stripe); c.drawLine(sx, sy, ex, ey, stripe);
} }
} }