From afbdb35a3af3f59640a0807ca1cc8e16579dc2e7 Mon Sep 17 00:00:00 2001 From: Yash-Garg Date: Fri, 16 Sep 2022 18:18:30 +0530 Subject: [PATCH] fix: use float instead of int while setting bounds Tested on all predefined DPI's and custom DPI's --- .../core/customviews/CustomAnalogClock.java | 35 +++++++++++++++---- .../core/customviews/DialOverlay.java | 2 +- .../core/customviews/HandsOverlay.java | 23 ++++++++---- .../core/customviews/SquareLinearLayout.java | 2 +- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/foundation/e/blisslauncher/core/customviews/CustomAnalogClock.java b/app/src/main/java/foundation/e/blisslauncher/core/customviews/CustomAnalogClock.java index 1cb819d9d5..e801ffbf63 100755 --- a/app/src/main/java/foundation/e/blisslauncher/core/customviews/CustomAnalogClock.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/customviews/CustomAnalogClock.java @@ -108,9 +108,9 @@ public class CustomAnalogClock extends View { public void setFace(Drawable face) { mFace = face; - mSizeChanged = true; mDialHeight = mFace.getIntrinsicHeight(); mDialWidth = mFace.getIntrinsicWidth(); + mSizeChanged = true; invalidate(); } @@ -155,7 +155,9 @@ public class CustomAnalogClock extends View { super.onDraw(canvas); final boolean sizeChanged = mSizeChanged; - mSizeChanged = false; + if (sizeChanged) { + mSizeChanged = false; + } final int availW = mRight - mLeft; final int availH = mBottom - mTop; @@ -177,8 +179,11 @@ public class CustomAnalogClock extends View { } if (sizeChanged) { - mFace.setBounds(cX - (w / 2), cY - (h / 2), cX + (w / 2), cY - + (h / 2)); + // Extend bottom by 1 and top by -1 for proper bounds + mFace.setBounds(cX - (w / 2), + cY - (h / 2) - 1, + cX + (w / 2), + cY + (h / 2) + 1); } @@ -193,8 +198,26 @@ public class CustomAnalogClock extends View { // from AnalogClock.java @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - int size = BlissLauncher.getApplication(mContext).getDeviceProfile().iconSizePx; - setMeasuredDimension(size, size); + int widthMode = MeasureSpec.getMode(widthMeasureSpec); + int widthSize = MeasureSpec.getSize(widthMeasureSpec); + int heightMode = MeasureSpec.getMode(heightMeasureSpec); + int heightSize = MeasureSpec.getSize(heightMeasureSpec); + + float hScale = 1.0f; + float vScale = 1.0f; + + if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mDialWidth) { + hScale = (float) widthSize / (float) mDialWidth; + } + + if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mDialHeight) { + vScale = (float) heightSize / (float) mDialHeight; + } + + float scale = Math.min(hScale, vScale); + + setMeasuredDimension(resolveSizeAndState((int) (mDialWidth * scale), widthMeasureSpec, 0), + resolveSizeAndState((int) (mDialHeight * scale), heightMeasureSpec, 0)); } @Override diff --git a/app/src/main/java/foundation/e/blisslauncher/core/customviews/DialOverlay.java b/app/src/main/java/foundation/e/blisslauncher/core/customviews/DialOverlay.java index d3522dda25..f3582fc5ad 100755 --- a/app/src/main/java/foundation/e/blisslauncher/core/customviews/DialOverlay.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/customviews/DialOverlay.java @@ -20,7 +20,7 @@ public interface DialOverlay { * @param h the height of the canvas * @param calendar the desired date/time */ - void onDraw(Canvas canvas, int cX, int cY, int w, int h, Calendar calendar, + void onDraw(Canvas canvas, float cX, float cY, int w, int h, Calendar calendar, boolean sizeChanged); } diff --git a/app/src/main/java/foundation/e/blisslauncher/core/customviews/HandsOverlay.java b/app/src/main/java/foundation/e/blisslauncher/core/customviews/HandsOverlay.java index 66a1872c03..077f22175e 100755 --- a/app/src/main/java/foundation/e/blisslauncher/core/customviews/HandsOverlay.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/customviews/HandsOverlay.java @@ -61,7 +61,7 @@ public class HandsOverlay implements DialOverlay { } @Override - public void onDraw(Canvas canvas, int cX, int cY, int w, int h, Calendar calendar, + public void onDraw(Canvas canvas, float cX, float cY, int w, int h, Calendar calendar, boolean sizeChanged) { updateHands(calendar); @@ -89,38 +89,47 @@ public class HandsOverlay implements DialOverlay { canvas.restore(); } - private void drawMinutes(Canvas canvas, int cX, int cY, int w, int h, Calendar calendar, + private void drawMinutes(Canvas canvas, float cX, float cY, int w, int h, Calendar calendar, boolean sizeChanged) { canvas.rotate(mMinRot, cX, cY); if (sizeChanged) { w = (int) (mMinute.getIntrinsicWidth() * scale); h = (int) (mMinute.getIntrinsicHeight() * scale); - mMinute.setBounds(cX - (w / 2), cY - (h / 2), cX + (w / 2), cY + (h / 2)); + mMinute.setBounds(Math.round(cX - (w / 2f)), + Math.round(cY - (h / 2f)), + Math.round(cX + (w / 2f)), + Math.round(cY + (h / 2f))); } mMinute.draw(canvas); } - private void drawHours(Canvas canvas, int cX, int cY, int w, int h, Calendar calendar, + private void drawHours(Canvas canvas, float cX, float cY, int w, int h, Calendar calendar, boolean sizeChanged) { canvas.rotate(mHourRot, cX, cY); if (sizeChanged) { w = (int) (mHour.getIntrinsicWidth()* scale); h = (int) (mHour.getIntrinsicHeight()* scale); - mHour.setBounds(cX - (w / 2), cY - (h / 2), cX + (w / 2), cY + (h / 2)); + mHour.setBounds(Math.round(cX - (w / 2f)), + Math.round(cY - (h / 2f)), + Math.round(cX + (w / 2f)), + Math.round(cY + (h / 2f))); } mHour.draw(canvas); } - private void drawSec(Canvas canvas, int cX, int cY, int w, int h, Calendar calendar, + private void drawSec(Canvas canvas, float cX, float cY, int w, int h, Calendar calendar, boolean sizeChanged) { canvas.rotate(mSecRot, cX, cY); if (sizeChanged) { w = (int) (mSecond.getIntrinsicWidth() * scale); h = (int) (mSecond.getIntrinsicHeight() * scale); - mSecond.setBounds(cX - (w / 2), cY - (h / 2), cX + (w / 2), cY + (h / 2)); + mSecond.setBounds(Math.round(cX - (w / 2f)), + Math.round(cY - (h / 2f)), + Math.round(cX + (w / 2f)), + Math.round(cY + (h / 2f))); } mSecond.draw(canvas); } diff --git a/app/src/main/java/foundation/e/blisslauncher/core/customviews/SquareLinearLayout.java b/app/src/main/java/foundation/e/blisslauncher/core/customviews/SquareLinearLayout.java index 5b4f13f4da..6bbbb781e2 100755 --- a/app/src/main/java/foundation/e/blisslauncher/core/customviews/SquareLinearLayout.java +++ b/app/src/main/java/foundation/e/blisslauncher/core/customviews/SquareLinearLayout.java @@ -29,7 +29,7 @@ public class SquareLinearLayout extends LinearLayout { int width = getMeasuredWidth(); int height = getMeasuredHeight(); - int size = width < height ? width : height; + int size = Math.min(width, height); setMeasuredDimension(size, size); } } -- GitLab