-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NativeAnimated][Android] Add support for animated events
- Loading branch information
1 parent
b05c7f7
commit 77953a3
Showing
11 changed files
with
402 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* The examples provided by Facebook are for non-commercial testing and | ||
* evaluation purposes only. | ||
* | ||
* Facebook reserves all rights not expressly granted. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL | ||
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* @flow | ||
*/ | ||
'use strict'; | ||
|
||
var React = require('react'); | ||
var ReactNative = require('react-native'); | ||
var { | ||
View, | ||
Text, | ||
Animated, | ||
StyleSheet, | ||
TouchableWithoutFeedback, | ||
} = ReactNative; | ||
|
||
const styles = StyleSheet.create({ | ||
row: { | ||
padding: 10, | ||
zIndex: 1, | ||
}, | ||
block: { | ||
width: 50, | ||
height: 50, | ||
backgroundColor: 'blue', | ||
}, | ||
}); | ||
|
||
class EventExample extends React.Component { | ||
static title = '<ScrollView>'; | ||
static description = 'Component that enables scrolling through child components.'; | ||
state = { | ||
scrollY: new Animated.Value(0), | ||
}; | ||
|
||
componentDidMount() { | ||
setInterval(() => { | ||
const start = Date.now(); | ||
console.warn('lagStart'); | ||
setTimeout(() => { | ||
while(Date.now() - start < 2500) {} | ||
console.warn('lagStop'); | ||
}, 1); | ||
}, 5000); | ||
} | ||
|
||
render() { | ||
const testOpacity = this.state.scrollY.interpolate({ | ||
inputRange: [0, 300], | ||
outputRange: [1, 0.5], | ||
}); | ||
return ( | ||
<Animated.ScrollView | ||
style={{ flex: 1, backgroundColor: 'blue' }} | ||
onScroll={Animated.event( | ||
[{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }], | ||
{ useNativeDriver: true } | ||
)} | ||
> | ||
<View style={{ height: 5000 }}> | ||
<Animated.View | ||
style={{ | ||
opacity: testOpacity, | ||
width: 400, | ||
height: 400, | ||
backgroundColor: 'red', | ||
}} | ||
/> | ||
</View> | ||
</Animated.ScrollView> | ||
); | ||
} | ||
} | ||
|
||
module.exports = EventExample; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
ReactAndroid/src/main/java/com/facebook/react/animated/EventAnimationDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.animated; | ||
|
||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.WritableArray; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.uimanager.events.RCTEventEmitter; | ||
|
||
import java.util.List; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Handles updating a {@link ValueAnimatedNode} when an event gets dispatched. | ||
*/ | ||
/* package */ class EventAnimationDriver implements RCTEventEmitter { | ||
private List<String> mEventPath; | ||
/* package */ ValueAnimatedNode mValueNode; | ||
|
||
public EventAnimationDriver(List<String> eventPath, ValueAnimatedNode valueNode) { | ||
mEventPath = eventPath; | ||
mValueNode = valueNode; | ||
} | ||
|
||
@Override | ||
public void receiveEvent(int targetTag, String eventName, @Nullable WritableMap event) { | ||
if (event == null) { | ||
throw new IllegalArgumentException("Native animated events must have event data."); | ||
} | ||
|
||
// Get the new value for the node by looking into the event map using the provided event path. | ||
ReadableMap curMap = event; | ||
for (int i = 0; i < mEventPath.size() - 1; i++) { | ||
curMap = curMap.getMap(mEventPath.get(i)); | ||
} | ||
|
||
mValueNode.mValue = curMap.getDouble(mEventPath.get(mEventPath.size() - 1)); | ||
} | ||
|
||
@Override | ||
public void receiveTouches(String eventName, WritableArray touches, WritableArray changedIndices) { | ||
throw new RuntimeException("receiveTouches is not support by native animated events"); | ||
} | ||
} |
Oops, something went wrong.