Skip to content

Commit 323d42e

Browse files
committed
fix(android): most stretch values are now working correctly with imageRotation
1 parent 3aa9b71 commit 323d42e

File tree

1 file changed

+71
-49
lines changed

1 file changed

+71
-49
lines changed

packages/image/platforms/android/java/com/nativescript/image/ScalingUtils.java

Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -136,33 +136,33 @@ public Object getState() {
136136
}
137137
@Override
138138
public Matrix getTransform(
139-
Matrix outTransform,
140-
Rect parentRect,
141-
int childWidth,
142-
int childHeight,
143-
float focusX,
144-
float focusY) {
145-
float sX = (float) parentRect.width() / (float) childWidth;
146-
float sY = (float) parentRect.height() / (float) childHeight;
147-
float rotationDelta = (90 - (_imageRotation % 180))/90.0f;
148-
if (rotationDelta != 1) {
149-
float destSX = (float) parentRect.width() / (float) childHeight;
150-
float destSY = (float) parentRect.height() / (float) childWidth;
151-
if (rotationDelta < 0) {
152-
sX = destSX + rotationDelta * (destSX - sX);
153-
sY = destSY + rotationDelta * (destSY - sY);
154-
} else {
155-
sX = sX + (1 - rotationDelta) * (destSX - sX);
156-
sY = sY + (1 - rotationDelta) * (destSY - sY);
157-
}
158-
159-
}
160-
getTransformImpl(outTransform, parentRect, childWidth, childHeight, focusX, focusY, sX, sY);
161-
if (_imageMatrix != null) {
162-
outTransform.preConcat(_imageMatrix);
163-
} else if (_imageRotation != 0) {
164-
outTransform.preRotate(_imageRotation, childWidth / 2.0f, childHeight / 2.0f);
139+
Matrix outTransform,
140+
Rect parentRect,
141+
int childWidth,
142+
int childHeight,
143+
float focusX,
144+
float focusY) {
145+
float sX = (float) parentRect.width() / (float) childWidth;
146+
float sY = (float) parentRect.height() / (float) childHeight;
147+
float rotationDelta = (90 - (_imageRotation % 180))/90.0f;
148+
if (rotationDelta != 1) {
149+
float destSX = (float) parentRect.width() / (float) childHeight;
150+
float destSY = (float) parentRect.height() / (float) childWidth;
151+
if (rotationDelta < 0) {
152+
sX = destSX + rotationDelta * (destSX - sX);
153+
sY = destSY + rotationDelta * (destSY - sY);
154+
} else {
155+
sX = sX + (1 - rotationDelta) * (destSX - sX);
156+
sY = sY + (1 - rotationDelta) * (destSY - sY);
165157
}
158+
159+
}
160+
getTransformImpl(outTransform, parentRect, childWidth, childHeight, focusX, focusY, sX, sY, rotationDelta);
161+
if (_imageMatrix != null) {
162+
outTransform.preConcat(_imageMatrix);
163+
} else if (_imageRotation != 0) {
164+
outTransform.preRotate(_imageRotation, childWidth / 2.0f, childHeight / 2.0f);
165+
}
166166

167167
return outTransform;
168168
}
@@ -175,7 +175,8 @@ public abstract void getTransformImpl(
175175
float focusX,
176176
float focusY,
177177
float scaleX,
178-
float scaleY);
178+
float scaleY,
179+
float rotationDelta);
179180
}
180181

181182
public static class ScaleTypeFitXY extends AbstractScaleType {
@@ -191,10 +192,14 @@ public void getTransformImpl(
191192
float focusX,
192193
float focusY,
193194
float scaleX,
194-
float scaleY) {
195-
float dx = parentRect.left;
196-
float dy = parentRect.top;
195+
float scaleY,
196+
float rotationDelta) {
197+
float deltaX = ((rotationDelta != 1) ? (childWidth - childHeight) * scaleX/ 2.0f : 0.0f);
198+
float deltaY = ((rotationDelta != 1) ? (childWidth - childHeight) * scaleY/ 2.0f : 0.0f);
199+
float dx = parentRect.left - deltaX;
200+
float dy = parentRect.top + deltaY;
197201
outTransform.setScale(scaleX, scaleY);
202+
198203
outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
199204
}
200205

@@ -217,10 +222,13 @@ public void getTransformImpl(
217222
float focusX,
218223
float focusY,
219224
float scaleX,
220-
float scaleY) {
225+
float scaleY,
226+
float rotationDelta) {
221227
float scale = Math.min(scaleX, scaleY);
222-
float dx = parentRect.left;
223-
float dy = parentRect.top;
228+
float delta = ((rotationDelta != 1) ? (childWidth - childHeight) * scale/ 2.0f : 0.0f);
229+
230+
float dx = parentRect.left - delta;
231+
float dy = parentRect.top + delta;
224232
outTransform.setScale(scale, scale);
225233
outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
226234
}
@@ -244,7 +252,8 @@ public void getTransformImpl(
244252
float focusX,
245253
float focusY,
246254
float scaleX,
247-
float scaleY) {
255+
float scaleY,
256+
float rotationDelta) {
248257
float scale = Math.min(scaleX, scaleY);
249258
float dx = parentRect.left;
250259
float dy = parentRect.top + (parentRect.height() - childHeight * scale);
@@ -271,7 +280,8 @@ public void getTransformImpl(
271280
float focusX,
272281
float focusY,
273282
float scaleX,
274-
float scaleY) {
283+
float scaleY,
284+
float rotationDelta) {
275285
float scale = Math.min(scaleX, scaleY);
276286
float dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
277287
float dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
@@ -298,10 +308,12 @@ public void getTransformImpl(
298308
float focusX,
299309
float focusY,
300310
float scaleX,
301-
float scaleY) {
311+
float scaleY,
312+
float rotationDelta) {
302313
float scale = Math.min(scaleX, scaleY);
303-
float dx = parentRect.left + (parentRect.width() - childWidth * scale);
304-
float dy = parentRect.top + (parentRect.height() - childHeight * scale);
314+
float delta = ((rotationDelta != 1) ? (childWidth - childHeight) * scale/ 2.0f : 0.0f);
315+
float dx = parentRect.left + (parentRect.width() - childWidth * scale) + delta;
316+
float dy = parentRect.top + (parentRect.height() - childHeight * scale) - delta;
305317
outTransform.setScale(scale, scale);
306318
outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
307319
}
@@ -325,7 +337,8 @@ public void getTransformImpl(
325337
float focusX,
326338
float focusY,
327339
float scaleX,
328-
float scaleY) {
340+
float scaleY,
341+
float rotationDelta) {
329342
float dx = parentRect.left + (parentRect.width() - childWidth) * 0.5f;
330343
float dy = parentRect.top + (parentRect.height() - childHeight) * 0.5f;
331344
outTransform.setTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
@@ -350,7 +363,8 @@ public void getTransformImpl(
350363
float focusX,
351364
float focusY,
352365
float scaleX,
353-
float scaleY) {
366+
float scaleY,
367+
float rotationDelta) {
354368
float scale = Math.min(Math.min(scaleX, scaleY), 1.0f);
355369
float dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
356370
float dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
@@ -377,15 +391,16 @@ public void getTransformImpl(
377391
float focusX,
378392
float focusY,
379393
float scaleX,
380-
float scaleY) {
394+
float scaleY,
395+
float rotationDelta) {
381396
float scale, dx, dy;
382397
if (scaleY > scaleX) {
383398
scale = scaleY;
384399
dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
385-
dy = parentRect.top;
400+
dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
386401
} else {
387402
scale = scaleX;
388-
dx = parentRect.left;
403+
dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
389404
dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
390405
}
391406
outTransform.setScale(scale, scale);
@@ -411,16 +426,21 @@ public void getTransformImpl(
411426
float focusX,
412427
float focusY,
413428
float scaleX,
414-
float scaleY) {
415-
float scale, dx, dy;
429+
float scaleY,
430+
float rotationDelta) {
431+
float scale, dx, dy, delta;
416432
if (scaleY > scaleX) {
417433
scale = scaleY;
434+
delta = ((rotationDelta != 1) ? (childWidth - childHeight) * scale/ 2.0f : 0.0f);
418435
dx = parentRect.width() * 0.5f - childWidth * scale * focusX;
419436
dx = parentRect.left + Math.max(Math.min(dx, 0), parentRect.width() - childWidth * scale);
420-
dy = parentRect.top;
437+
dy = parentRect.height() * 0.5f - childHeight * scale * focusY;
438+
dy = parentRect.top + Math.max(Math.min(dy, 0), parentRect.height() - childHeight * scale) - delta;
421439
} else {
422440
scale = scaleX;
423-
dx = parentRect.left;
441+
delta = ((rotationDelta != 1) ? (childWidth - childHeight) * scale: 0.0f);
442+
dx = parentRect.width() * 0.5f - childWidth * scale * focusX;
443+
dx = parentRect.left + Math.max(Math.min(dx, 0), parentRect.width() - childWidth * scale) + Math.min(delta / 2.0f, 0.0f);
424444
dy = parentRect.height() * 0.5f - childHeight * scale * focusY;
425445
dy = parentRect.top + Math.max(Math.min(dy, 0), parentRect.height() - childHeight * scale);
426446
}
@@ -447,7 +467,8 @@ public void getTransformImpl(
447467
float focusX,
448468
float focusY,
449469
float scaleX,
450-
float scaleY) {
470+
float scaleY,
471+
float rotationDelta) {
451472
float scale, dx, dy;
452473
scale = scaleX;
453474
dx = parentRect.left;
@@ -475,7 +496,8 @@ public void getTransformImpl(
475496
float focusX,
476497
float focusY,
477498
float scaleX,
478-
float scaleY) {
499+
float scaleY,
500+
float rotationDelta) {
479501
float scale, dx, dy;
480502
scale = scaleY;
481503
dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;

0 commit comments

Comments
 (0)