Add rotatePointF to RotationUtils

Bug: 218374828
Test: atest RotationUtilsTest
Change-Id: I877e762b1f2b96ae8da3e6b08a78ba4d066c2880
This commit is contained in:
Ilya Matyukhin
2022-11-28 05:09:56 +00:00
parent 2a5adc3dfe
commit 3318c87c48
2 changed files with 48 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ import android.annotation.Dimension;
import android.graphics.Insets;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.view.Surface.Rotation;
import android.view.SurfaceControl;
@@ -192,6 +193,29 @@ public class RotationUtils {
}
}
/**
* Same as {@link #rotatePoint}, but for float coordinates.
*/
public static void rotatePointF(PointF inOutPoint, @Rotation int rotation,
float parentW, float parentH) {
float origX = inOutPoint.x;
switch (rotation) {
case ROTATION_0:
return;
case ROTATION_90:
inOutPoint.x = inOutPoint.y;
inOutPoint.y = parentW - origX;
return;
case ROTATION_180:
inOutPoint.x = parentW - inOutPoint.x;
inOutPoint.y = parentH - inOutPoint.y;
return;
case ROTATION_270:
inOutPoint.x = parentH - inOutPoint.y;
inOutPoint.y = origX;
}
}
/**
* Sets a matrix such that given a rotation, it transforms physical display
* coordinates to that rotation's logical coordinates.

View File

@@ -18,6 +18,7 @@ package android.util;
import static android.util.RotationUtils.rotateBounds;
import static android.util.RotationUtils.rotatePoint;
import static android.util.RotationUtils.rotatePointF;
import static android.view.Surface.ROTATION_180;
import static android.view.Surface.ROTATION_270;
import static android.view.Surface.ROTATION_90;
@@ -25,6 +26,7 @@ import static android.view.Surface.ROTATION_90;
import static org.junit.Assert.assertEquals;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -79,4 +81,26 @@ public class RotationUtilsTest {
rotatePoint(testResult, ROTATION_270, parentW, parentH);
assertEquals(new Point(560, 60), testResult);
}
@Test
public void testRotatePointF() {
float parentW = 1000f;
float parentH = 600f;
PointF testPt = new PointF(60f, 40f);
PointF testResult = new PointF(testPt);
rotatePointF(testResult, ROTATION_90, parentW, parentH);
assertEquals(40f, testResult.x, .1f);
assertEquals(940f, testResult.y, .1f);
testResult.set(testPt.x, testPt.y);
rotatePointF(testResult, ROTATION_180, parentW, parentH);
assertEquals(940f, testResult.x, .1f);
assertEquals(560f, testResult.y, .1f);
testResult.set(testPt.x, testPt.y);
rotatePointF(testResult, ROTATION_270, parentW, parentH);
assertEquals(560f, testResult.x, .1f);
assertEquals(60f, testResult.y, .1f);
}
}