From a06ced4b164c003992daec50c7c8f909f07b0d39 Mon Sep 17 00:00:00 2001 From: Satyajit Sahoo Date: Sat, 4 Feb 2017 11:05:37 -0800 Subject: [PATCH] Add perspective transform support on Android Summary: Rebased #6926 against master. Fixes #2915 Closes https://github.com/facebook/react-native/pull/11713 Differential Revision: D4513111 fbshipit-source-id: 9fb653c4bfd64eda12a605f6cabb0159b2af8f73 --- Examples/UIExplorer/js/TransformExample.js | 2 +- .../react/uimanager/BaseViewManager.java | 22 +++++++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Examples/UIExplorer/js/TransformExample.js b/Examples/UIExplorer/js/TransformExample.js index ea7ad3d15..f6d028566 100644 --- a/Examples/UIExplorer/js/TransformExample.js +++ b/Examples/UIExplorer/js/TransformExample.js @@ -211,7 +211,7 @@ exports.title = 'Transforms'; exports.description = 'View transforms'; exports.examples = [ { - title: 'Perspective', + title: 'Perspective, Rotate, Animation', description: 'perspective: 850, rotateX: Animated.timing(0 -> 360)', render(): React.Element { return ; } }, diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java index 815bad0fa..24192175d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java @@ -5,8 +5,6 @@ package com.facebook.react.uimanager; import android.graphics.Color; import android.os.Build; import android.view.View; -import android.view.ViewGroup; - import com.facebook.react.bridge.ReadableArray; import com.facebook.react.uimanager.annotations.ReactProp; @@ -35,6 +33,9 @@ public abstract class BaseViewManager PERSPECTIVE_ARRAY_INVERTED_CAMERA_DISTANCE_INDEX) { + float invertedCameraDistance = (float) perspectiveArray[PERSPECTIVE_ARRAY_INVERTED_CAMERA_DISTANCE_INDEX]; + if (invertedCameraDistance < 0) { + float cameraDistance = -1 / invertedCameraDistance; + float scale = DisplayMetricsHolder.getScreenDisplayMetrics().density; + + // The following converts the matrix's perspective to a camera distance + // such that the camera perspective looks the same on Android and iOS + float normalizedCameraDistance = scale * cameraDistance * CAMERA_DISTANCE_NORMALIZATION_MULTIPLIER; + + view.setCameraDistance(normalizedCameraDistance); + } + } } private static void resetTransformProperty(View view) { @@ -175,5 +192,6 @@ public abstract class BaseViewManager