@@ -228,10 +228,17 @@ public static Point autoScaleDown(Point point) {
228228
229229public static Point scaleDown (Point point , int zoom ) {
230230 if (zoom == 100 || point == null ) return point ;
231+ FloatAwarePoint fPoint = FloatAwareGeometryFactory .createFloatAwarePoint (point );
231232 float scaleFactor = getScalingFactor (zoom );
232- Point scaledPoint = new Point (0 ,0 );
233- scaledPoint .x = Math .round (point .x / scaleFactor );
234- scaledPoint .y = Math .round (point .y / scaleFactor );
233+ FloatAwarePoint scaledPoint = new FloatAwarePoint (0 ,0 );
234+
235+ float absX = (fPoint .x + fPoint .residualX ) / scaleFactor ;
236+ float absY = (fPoint .y + fPoint .residualY ) / scaleFactor ;
237+
238+ scaledPoint .x = Math .round (absX );
239+ scaledPoint .y = Math .round (absY );
240+ scaledPoint .residualX = absX - scaledPoint .x ;
241+ scaledPoint .residualY = absY - scaledPoint .y ;
235242 return scaledPoint ;
236243}
237244
@@ -255,16 +262,7 @@ public static Rectangle autoScaleDown(Rectangle rect) {
255262}
256263
257264public static Rectangle scaleDown (Rectangle rect , int zoom ) {
258- if (zoom == 100 || rect == null ) return rect ;
259- Rectangle scaledRect = new Rectangle (0 ,0 ,0 ,0 );
260- Point scaledTopLeft = scaleDown (new Point (rect .x , rect .y ), zoom );
261- Point scaledBottomRight = scaleDown (new Point (rect .x + rect .width , rect .y + rect .height ), zoom );
262-
263- scaledRect .x = scaledTopLeft .x ;
264- scaledRect .y = scaledTopLeft .y ;
265- scaledRect .width = scaledBottomRight .x - scaledTopLeft .x ;
266- scaledRect .height = scaledBottomRight .y - scaledTopLeft .y ;
267- return scaledRect ;
265+ return scaleBounds (rect , 100 , zoom );
268266}
269267/**
270268 * Returns a new scaled down Rectangle if enabled for Drawable class.
@@ -333,12 +331,24 @@ public static boolean isSmoothScalingEnabled() {
333331 */
334332public static Rectangle scaleBounds (Rectangle rect , int targetZoom , int currentZoom ) {
335333 if (rect == null || targetZoom == currentZoom ) return rect ;
334+ FloatAwareRectangle fRect = FloatAwareGeometryFactory .createFloatAwareRectangle (rect );
336335 float scaleFactor = ((float )targetZoom ) / (float )currentZoom ;
337- Rectangle returnRect = new Rectangle (0 ,0 ,0 ,0 );
338- returnRect .x = Math .round (rect .x * scaleFactor );
339- returnRect .y = Math .round (rect .y * scaleFactor );
340- returnRect .width = Math .round (rect .width * scaleFactor );
341- returnRect .height = Math .round (rect .height * scaleFactor );
336+ FloatAwareRectangle returnRect = new FloatAwareRectangle (0 ,0 ,0 ,0 );
337+
338+ float absX = (fRect .x + fRect .residualX ) * scaleFactor ;
339+ float absY = (fRect .y + fRect .residualY ) * scaleFactor ;
340+ float absWidth = (fRect .width + fRect .residualWidth ) * scaleFactor ;
341+ float absHeight = (fRect .height + fRect .residualHeight ) * scaleFactor ;
342+
343+ returnRect .x = Math .round (absX );
344+ returnRect .y = Math .round (absY );
345+ returnRect .width = Math .round (absWidth );
346+ returnRect .height = Math .round (absHeight );
347+
348+ returnRect .residualX = absX - returnRect .x ;
349+ returnRect .residualY = absY - returnRect .y ;
350+ returnRect .residualWidth = absWidth - returnRect .width ;
351+ returnRect .residualHeight = absHeight - returnRect .height ;
342352 return returnRect ;
343353}
344354
@@ -436,10 +446,17 @@ public static Point autoScaleUp(Point point) {
436446
437447public static Point scaleUp (Point point , int zoom ) {
438448 if (zoom == 100 || point == null ) return point ;
449+ FloatAwarePoint fPoint = FloatAwareGeometryFactory .createFloatAwarePoint (point );
439450 float scaleFactor = getScalingFactor (zoom );
440- Point scaledPoint = new Point (0 ,0 );
441- scaledPoint .x = Math .round (point .x * scaleFactor );
442- scaledPoint .y = Math .round (point .y * scaleFactor );
451+ FloatAwarePoint scaledPoint = new FloatAwarePoint (0 ,0 );
452+
453+ float absX = (fPoint .x + fPoint .residualX ) * scaleFactor ;
454+ float absY = (fPoint .y + fPoint .residualY ) * scaleFactor ;
455+
456+ scaledPoint .x = Math .round (absX );
457+ scaledPoint .y = Math .round (absY );
458+ scaledPoint .residualX = absX - scaledPoint .x ;
459+ scaledPoint .residualY = absY - scaledPoint .y ;
443460 return scaledPoint ;
444461}
445462
@@ -463,16 +480,7 @@ public static Rectangle autoScaleUp(Rectangle rect) {
463480}
464481
465482public static Rectangle scaleUp (Rectangle rect , int zoom ) {
466- if (zoom == 100 || rect == null ) return rect ;
467- Rectangle scaledRect = new Rectangle (0 ,0 ,0 ,0 );
468- Point scaledTopLeft = scaleUp (new Point (rect .x , rect .y ), zoom );
469- Point scaledBottomRight = scaleUp (new Point (rect .x + rect .width , rect .y + rect .height ), zoom );
470-
471- scaledRect .x = scaledTopLeft .x ;
472- scaledRect .y = scaledTopLeft .y ;
473- scaledRect .width = scaledBottomRight .x - scaledTopLeft .x ;
474- scaledRect .height = scaledBottomRight .y - scaledTopLeft .y ;
475- return scaledRect ;
483+ return scaleBounds (rect , zoom , 100 );
476484}
477485
478486/**
@@ -751,4 +759,20 @@ public ImageData getImageData(int zoom) {
751759 return DPIUtil .scaleImageData (device , imageData , zoom , currentZoom );
752760 }
753761}
762+
763+ private class FloatAwareGeometryFactory {
764+ static FloatAwareRectangle createFloatAwareRectangle (Rectangle rectangle ) {
765+ if (rectangle instanceof FloatAwareRectangle ) {
766+ return (FloatAwareRectangle ) rectangle ;
767+ }
768+ return new FloatAwareRectangle (rectangle .x , rectangle .y , rectangle .width , rectangle .height );
769+ }
770+
771+ static FloatAwarePoint createFloatAwarePoint (Point point ) {
772+ if (point instanceof FloatAwarePoint ) {
773+ return (FloatAwarePoint ) point ;
774+ }
775+ return new FloatAwarePoint (point .x , point .y );
776+ }
777+ }
754778}
0 commit comments