/
RNSharedElementDrawable.java
364 lines (315 loc) · 10.9 KB
/
RNSharedElementDrawable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package versioned.host.exp.exponent.modules.api.components.sharedelement;
import android.util.Log;
import android.view.View;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Outline;
import android.graphics.PixelFormat;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.widget.ImageView;
import com.facebook.react.views.image.ReactImageView;
import com.facebook.react.views.view.ReactViewGroup;
import com.facebook.react.views.view.ReactViewBackgroundDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.drawable.ScalingUtils.ScaleType;
import com.facebook.drawee.generic.RoundingParams;
class RNSharedElementDrawable extends Drawable {
enum ViewType {
NONE("none"),
REACTIMAGEVIEW("image"),
IMAGEVIEW("image"),
PLAIN("view"),
GENERIC("generic");
private final String value;
ViewType(final String newValue) {
value = newValue;
}
public String getValue() {
return value;
}
}
static private String LOG_TAG = "RNSharedElementDrawable";
private RNSharedElementContent mContent = null;
private RNSharedElementStyle mStyle = null;
private ViewType mViewType = ViewType.NONE;
private float mPosition = 0;
private int mAlpha = 255;
private Path mPathForBorderRadiusOutline = null;
private ReactViewBackgroundDrawable mReactViewBackgroundDrawableCache = null;
RNSharedElementStyle getStyle() {
return mStyle;
}
RNSharedElementContent getContent() {
return mContent;
}
float getPosition() {
return mPosition;
}
ViewType update(
RNSharedElementContent content,
RNSharedElementStyle style,
float position
) {
boolean invalidated = false;
// Update content
if (mContent != content) {
mContent = content;
invalidated = true;
}
// Update view-type
ViewType viewType = (mContent != null) ? RNSharedElementDrawable.getViewType(mContent.view, style) : ViewType.NONE;
if (mViewType != viewType) {
mViewType = viewType;
invalidated = true;
}
// Update & check style changes
if ((mStyle != null) && (style != null) && !invalidated) {
switch (viewType) {
case REACTIMAGEVIEW:
case IMAGEVIEW:
if ((mStyle.compare(style) &
(RNSharedElementStyle.PROP_BORDER
| RNSharedElementStyle.PROP_BACKGROUND_COLOR
| RNSharedElementStyle.PROP_SCALETYPE)) != 0) {
//Log.d(LOG_TAG, "drawableChanged, viewType: " + viewType + ", changes: " + mStyle.compare(style));
invalidated = true;
} else {
invalidated = false;
}
break;
case PLAIN:
if ((mStyle.compare(style) &
(RNSharedElementStyle.PROP_BORDER
| RNSharedElementStyle.PROP_BACKGROUND_COLOR)) != 0) {
//Log.d(LOG_TAG, "drawableChanged, viewType: " + viewType + ", changes: " + mStyle.compare(style));
invalidated = true;
} else {
invalidated = false;
}
break;
case GENERIC:
// nop
break;
}
}
mStyle = style;
// Update position
mPosition = position;
// Invalidate if neccessary
if (invalidated) {
invalidateSelf();
}
return viewType;
}
@Override
public int getOpacity() {
// This method was deprecated in API level 29.
// This method is no longer used in graphics optimizations
return PixelFormat.TRANSLUCENT;
}
@Override
public void setColorFilter(ColorFilter cf) {
// do nothing
}
@Override
public void setAlpha(int alpha) {
if (alpha != mAlpha) {
mAlpha = alpha;
}
}
@Override
public int getAlpha() {
return mAlpha;
}
/* Android's elevation implementation requires this to be implemented to know where to draw the shadow. */
@Override
public void getOutline(Outline outline) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
super.getOutline(outline);
return;
}
if (mStyle == null) {
outline.setRect(getBounds());
return;
}
if ((mStyle.borderTopLeftRadius == 0) &&
(mStyle.borderTopRightRadius == 0) &&
(mStyle.borderBottomLeftRadius == 0) &&
(mStyle.borderBottomRightRadius == 0)) {
outline.setRect(getBounds());
return;
}
if (mPathForBorderRadiusOutline == null) {
mPathForBorderRadiusOutline = new Path();
} else {
mPathForBorderRadiusOutline.reset();
}
float extraRadiusForOutline = mStyle.borderWidth / 2f;
mPathForBorderRadiusOutline.addRoundRect(
new RectF(getBounds()),
new float[]{
mStyle.borderTopLeftRadius + extraRadiusForOutline,
mStyle.borderTopLeftRadius + extraRadiusForOutline,
mStyle.borderTopRightRadius + extraRadiusForOutline,
mStyle.borderTopRightRadius + extraRadiusForOutline,
mStyle.borderBottomRightRadius + extraRadiusForOutline,
mStyle.borderBottomRightRadius + extraRadiusForOutline,
mStyle.borderBottomLeftRadius + extraRadiusForOutline,
mStyle.borderBottomLeftRadius + extraRadiusForOutline
},
Path.Direction.CW
);
outline.setConvexPath(mPathForBorderRadiusOutline);
}
static ViewType getViewType(View view, RNSharedElementStyle style) {
if (view == null) return ViewType.NONE;
if (view instanceof ReactImageView) {
return ViewType.REACTIMAGEVIEW;
} else if (view instanceof ImageView) {
return ViewType.IMAGEVIEW;
} else if (view instanceof ReactViewGroup) {
ReactViewGroup viewGroup = (ReactViewGroup) view;
if (viewGroup.getChildCount() == 0) {
if (style.isVisible()) {
return ViewType.PLAIN;
} else {
return ViewType.NONE;
}
}
}
return ViewType.GENERIC;
}
@Override
public void draw(Canvas canvas) {
//Log.d(LOG_TAG, "draw, viewType: " + mViewType + ", position: " + mPosition);
switch (mViewType) {
case REACTIMAGEVIEW:
drawReactImageView(canvas);
break;
case IMAGEVIEW:
drawImageView(canvas);
break;
case PLAIN:
drawPlainView(canvas);
break;
case GENERIC:
drawGenericView(canvas);
break;
}
}
private void drawReactImageView(Canvas canvas) {
// TODO FIX IMAGE STRETCH ISSUE WHEN IMAGE DOESN'T FILL
// ENTIRE CANVAS
ReactImageView imageView = (ReactImageView) mContent.view;
RNSharedElementStyle style = mStyle;
DraweeController controller = imageView.getController();
GenericDraweeHierarchy hierarchy = imageView.getHierarchy();
Drawable drawable = hierarchy.getTopLevelDrawable();
// Backup current props
Rect oldBounds = new Rect(drawable.getBounds());
ScaleType oldScaleType = hierarchy.getActualImageScaleType();
RoundingParams oldRoundingParams = hierarchy.getRoundingParams();
Drawable oldBackgroundImage = null; //hierarchy.getBackgroundImage();
int oldFadeDuration = hierarchy.getFadeDuration();
// Configure drawable
drawable.setBounds(getBounds());
hierarchy.setActualImageScaleType(style.scaleType);
RoundingParams roundingParams = new RoundingParams();
roundingParams.setBorderColor(style.borderColor);
roundingParams.setBorderWidth(style.borderWidth);
roundingParams.setRoundingMethod(RoundingParams.RoundingMethod.BITMAP_ONLY);
roundingParams.setCornersRadii(
style.borderTopLeftRadius,
style.borderTopRightRadius,
style.borderBottomRightRadius,
style.borderBottomLeftRadius
);
hierarchy.setRoundingParams(roundingParams);
hierarchy.setBackgroundImage(null);
hierarchy.setFadeDuration(0);
// Draw!
drawable.draw(canvas);
// Restore props
hierarchy.setFadeDuration(oldFadeDuration);
hierarchy.setBackgroundImage(oldBackgroundImage);
hierarchy.setRoundingParams(oldRoundingParams);
hierarchy.setActualImageScaleType(oldScaleType);
drawable.setBounds(oldBounds);
}
private void drawImageView(Canvas canvas) {
ImageView imageView = (ImageView) mContent.view;
RNSharedElementStyle style = mStyle;
Drawable drawable = imageView.getDrawable();
// Backup current props
Rect oldBounds = new Rect(drawable.getBounds());
// Configure drawable
int width = (int) mContent.size.right;
int height = (int) mContent.size.bottom;
drawable.setBounds(0, 0, width, height);
Matrix matrix = new Matrix();
style.scaleType.getTransform(matrix, getBounds(), width, height, 0.5f, 0.5f);
// Draw!
int saveCount = canvas.save();
canvas.concat(matrix);
drawable.draw(canvas);
canvas.restoreToCount(saveCount);
// Restore props
drawable.setBounds(oldBounds);
}
private void drawPlainView(Canvas canvas) {
RNSharedElementStyle style = mStyle;
// Create drawable
ReactViewBackgroundDrawable drawable = mReactViewBackgroundDrawableCache;
if (drawable == null) {
drawable = new ReactViewBackgroundDrawable(mContent.view.getContext());
mReactViewBackgroundDrawableCache = drawable;
}
drawable.setBounds(getBounds());
// Set background color
drawable.setColor(style.backgroundColor);
// Set border
float borderColorRGB = (float) ((int) style.borderColor & 0x00FFFFFF);
float borderColorAlpha = (float) ((int) style.borderColor >>> 24);
drawable.setBorderStyle(style.borderStyle);
for (int i = 0; i < 4; i++) {
drawable.setBorderColor(i, borderColorRGB, borderColorAlpha);
drawable.setBorderWidth(i, style.borderWidth);
}
drawable.setRadius(style.borderTopLeftRadius, 0);
drawable.setRadius(style.borderTopRightRadius, 1);
drawable.setRadius(style.borderBottomRightRadius, 2);
drawable.setRadius(style.borderBottomLeftRadius, 3);
// Draw!
drawable.draw(canvas);
}
private void drawGenericView(Canvas canvas) {
mContent.view.draw(canvas);
}
/*private void drawGenericScaledCanvas(Canvas canvas) {
View view = mContent.view;
// Save canvas
int saveCount = canvas.save();
// Adjust scale
Rect bounds = getBounds();
float scaleX = (float) bounds.width() / (float) view.getWidth();
float scaleY = (float) bounds.height() / (float) view.getHeight();
float scale = 1;
if ((scaleX >= 1) && (scaleY >= 1)) {
scaleX = Math.min(scaleX, scaleY);
scaleY = scaleX;
} else if ((scaleX <= 1) && (scaleY <= 1)) {
scaleX = Math.max(scaleX, scaleY);
scaleY = scaleX;
}
canvas.scale(scaleX, scaleY);
// Draw!
view.draw(canvas);
// Restore canvas
canvas.restoreToCount(saveCount);
}*/
}