Skip to content

Commit a97acd9

Browse files
committed
fix(android): fixed pixel rounding in transitions
1 parent dcc3f79 commit a97acd9

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementTransition.java

+25-24
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.util.Log;
88
import android.graphics.Canvas;
99
import android.graphics.Rect;
10+
import android.graphics.RectF;
1011
import android.graphics.Color;
1112
import android.view.View;
1213
import android.view.ViewGroup;
@@ -175,7 +176,7 @@ private void updateLayout() {
175176
RNSharedElementTransitionItem startItem = mItems.get(Item.START.getValue());
176177
RNSharedElementTransitionItem endItem = mItems.get(Item.END.getValue());
177178

178-
// Get styles & content
179+
// Get styles
179180
RNSharedElementStyle startStyle = startItem.getStyle();
180181
RNSharedElementStyle endStyle = endItem.getStyle();
181182
if ((startStyle == null) && (endStyle == null)) return;
@@ -192,8 +193,8 @@ private void updateLayout() {
192193
Rect startFrame = (startStyle != null) ? startStyle.frame : EMPTY_RECT;
193194
Rect endLayout = (endStyle != null) ? endStyle.layout : EMPTY_RECT;
194195
Rect endFrame = (endStyle != null) ? endStyle.frame : EMPTY_RECT;
195-
Rect parentLayout = new Rect(startLayout);
196-
parentLayout.union(endLayout);
196+
RectF parentLayout = new RectF(startLayout);
197+
parentLayout.union(new RectF(endLayout));
197198

198199
// Get clipped areas
199200
Rect startClippedLayout = (startStyle != null) ? startItem.getClippedLayout() : EMPTY_RECT;
@@ -202,25 +203,25 @@ private void updateLayout() {
202203
Rect endClipInsets = getClipInsets(endLayout, endClippedLayout);
203204

204205
// Get interpolated layout
205-
Rect interpolatedLayout;
206-
Rect interpolatedClipInsets;
206+
RectF interpolatedLayout;
207+
RectF interpolatedClipInsets;
207208
RNSharedElementStyle interpolatedStyle;
208209
if ((startStyle != null) && (endStyle != null)) {
209210
interpolatedLayout = getInterpolatedLayout(startLayout, endLayout, mNodePosition);
210211
interpolatedClipInsets = getInterpolatedClipInsets(parentLayout, startClipInsets, startClippedLayout, endClipInsets, endClippedLayout, mNodePosition);
211212
interpolatedStyle = getInterpolatedStyle(startStyle, startContent, endStyle, endContent, mNodePosition);
212213
} else if (startStyle != null) {
213-
interpolatedLayout = startLayout;
214+
interpolatedLayout = new RectF(startLayout);
214215
interpolatedStyle = startStyle;
215-
interpolatedClipInsets = startClipInsets;
216+
interpolatedClipInsets = new RectF(startClipInsets);
216217
} else {
217218
if (!mInitialNodePositionSet) {
218219
mNodePosition = 1.0f;
219220
mInitialNodePositionSet = true;
220221
}
221-
interpolatedLayout = endLayout;
222+
interpolatedLayout = new RectF(endLayout);
222223
interpolatedStyle = endStyle;
223-
interpolatedClipInsets = endClipInsets;
224+
interpolatedClipInsets = new RectF(endClipInsets);
224225
}
225226

226227
// Apply clipping insets
@@ -241,8 +242,8 @@ private void updateLayout() {
241242
super.layout(
242243
-mParentOffset[0],
243244
-mParentOffset[1],
244-
parentLayout.width() - mParentOffset[0],
245-
parentLayout.height() - mParentOffset[1]
245+
(int) Math.ceil(parentLayout.width() - mParentOffset[0]),
246+
(int) Math.ceil(parentLayout.height() - mParentOffset[1])
246247
);
247248
setTranslationX(parentLayout.left);
248249
setTranslationY(parentLayout.top);
@@ -352,22 +353,22 @@ private Rect getClipInsets(Rect layout, Rect clippedLayout) {
352353
);
353354
}
354355

355-
private Rect getInterpolatedClipInsets(
356-
Rect interpolatedLayout,
356+
private RectF getInterpolatedClipInsets(
357+
RectF interpolatedLayout,
357358
Rect startClipInsets,
358359
Rect startClippedLayout,
359360
Rect endClipInsets,
360361
Rect endClippedLayout,
361362
float position) {
362-
Rect clipInsets = new Rect();
363+
RectF clipInsets = new RectF();
363364

364365
// Top
365366
if ((endClipInsets.top == 0) && (startClipInsets.top != 0) && (startClippedLayout.top <= endClippedLayout.top)) {
366367
clipInsets.top = Math.max(0, startClippedLayout.top - interpolatedLayout.top);
367368
} else if ((startClipInsets.top == 0) && (endClipInsets.top != 0) && (endClippedLayout.top <= startClippedLayout.top)) {
368369
clipInsets.top = Math.max(0, endClippedLayout.top - interpolatedLayout.top);
369370
} else {
370-
clipInsets.top = (int) (startClipInsets.top + ((endClipInsets.top - startClipInsets.top) * position));
371+
clipInsets.top = (startClipInsets.top + ((endClipInsets.top - startClipInsets.top) * position));
371372
}
372373

373374
// Bottom
@@ -376,7 +377,7 @@ private Rect getInterpolatedClipInsets(
376377
} else if ((startClipInsets.bottom == 0) && (endClipInsets.bottom != 0) && (endClippedLayout.bottom >= startClippedLayout.bottom)) {
377378
clipInsets.bottom = Math.max(0, interpolatedLayout.bottom - endClippedLayout.bottom);
378379
} else {
379-
clipInsets.bottom = (int) (startClipInsets.bottom + ((endClipInsets.bottom - startClipInsets.bottom) * position));
380+
clipInsets.bottom = (startClipInsets.bottom + ((endClipInsets.bottom - startClipInsets.bottom) * position));
380381
}
381382

382383
// Left
@@ -385,7 +386,7 @@ private Rect getInterpolatedClipInsets(
385386
} else if ((startClipInsets.left == 0) && (endClipInsets.left != 0) && (endClippedLayout.left <= startClippedLayout.left)) {
386387
clipInsets.left = Math.max(0, endClippedLayout.left - interpolatedLayout.left);
387388
} else {
388-
clipInsets.left = (int) (startClipInsets.left + ((endClipInsets.left - startClipInsets.left) * position));
389+
clipInsets.left = (startClipInsets.left + ((endClipInsets.left - startClipInsets.left) * position));
389390
}
390391

391392
// Right
@@ -394,18 +395,18 @@ private Rect getInterpolatedClipInsets(
394395
} else if ((startClipInsets.right == 0) && (endClipInsets.right != 0) && (endClippedLayout.right >= startClippedLayout.right)) {
395396
clipInsets.right = Math.max(0, interpolatedLayout.right - endClippedLayout.right);
396397
} else {
397-
clipInsets.right = (int) (startClipInsets.right + ((endClipInsets.right - startClipInsets.right) * position));
398+
clipInsets.right = (startClipInsets.right + ((endClipInsets.right - startClipInsets.right) * position));
398399
}
399400

400401
return clipInsets;
401402
}
402403

403-
private Rect getInterpolatedLayout(Rect layout1, Rect layout2, float position) {
404-
return new Rect(
405-
(int) (layout1.left + ((layout2.left - layout1.left) * position)),
406-
(int) (layout1.top + ((layout2.top - layout1.top) * position)),
407-
(int) (layout1.right + ((layout2.right - layout1.right) * position)),
408-
(int) (layout1.bottom + ((layout2.bottom - layout1.bottom) * position))
404+
private RectF getInterpolatedLayout(Rect layout1, Rect layout2, float position) {
405+
return new RectF(
406+
(layout1.left + ((layout2.left - layout1.left) * position)),
407+
(layout1.top + ((layout2.top - layout1.top) * position)),
408+
(layout1.right + ((layout2.right - layout1.right) * position)),
409+
(layout1.bottom + ((layout2.bottom - layout1.bottom) * position))
409410
);
410411
}
411412

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementView.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.util.Log;
44
import android.view.View;
55
import android.graphics.Rect;
6+
import android.graphics.RectF;
67

78
import com.facebook.react.uimanager.ThemedReactContext;
89

@@ -29,8 +30,8 @@ void reset() {
2930
}
3031

3132
void updateViewAndDrawable(
32-
Rect layout,
33-
Rect parentLayout,
33+
RectF layout,
34+
RectF parentLayout,
3435
Rect originalLayout,
3536
Rect originalFrame,
3637
RNSharedElementContent content,
@@ -53,8 +54,8 @@ void updateViewAndDrawable(
5354
}
5455

5556
// Update view size/position/scale
56-
int width = layout.width();
57-
int height = layout.height();
57+
float width = layout.width();
58+
float height = layout.height();
5859
if (useGPUScaling) {
5960
int originalWidth = originalFrame.width();
6061
int originalHeight = originalFrame.height();
@@ -65,8 +66,8 @@ void updateViewAndDrawable(
6566
setTranslationY(layout.top - parentLayout.top);
6667

6768
// Update scale
68-
float scaleX = (float) width / (float) originalWidth;
69-
float scaleY = (float) height / (float) originalHeight;
69+
float scaleX = width / (float) originalWidth;
70+
float scaleY = height / (float) originalHeight;
7071
if (!Float.isInfinite(scaleX) && !Float.isNaN(scaleX) && !Float.isInfinite(scaleY) && !Float.isNaN(scaleY)) {
7172

7273
// Determine si
@@ -111,7 +112,7 @@ void updateViewAndDrawable(
111112
} else {
112113

113114
// Update view
114-
layout(0, 0, width, height);
115+
layout(0, 0, (int)Math.ceil(width), (int)Math.ceil(height));
115116
setTranslationX(layout.left - parentLayout.left);
116117
setTranslationY(layout.top - parentLayout.top);
117118
}

0 commit comments

Comments
 (0)