Skip to content

Commit

Permalink
Implement native Animated value listeners on Android
Browse files Browse the repository at this point in the history
Summary:
Adds support for `Animated.Value#addListener` for native driven nodes on Android. This is based on work by skevy in the exponent RN fork. Also adds a UIExplorer example.

** Test plan **
Run unit tests

Tested that by adding a listener to a native driven animated node and checked that the listener callback is called properly.

Also tested that it doesn't crash on iOS that doesn't support this yet.
Closes facebook/react-native#8844

Differential Revision: D3670906

fbshipit-source-id: 15700ed7b93db140d907ce80af4dae6be3102135
  • Loading branch information
janicduplessis authored and Facebook Github Bot 7 committed Aug 4, 2016
1 parent 0ecca5a commit 3292d8f
Showing 1 changed file with 60 additions and 10 deletions.
70 changes: 60 additions & 10 deletions UIExplorer/js/NativeAnimationsExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
*/
'use strict';

var React = require('react');
var ReactNative = require('react-native');
var {
const React = require('react');
const ReactNative = require('react-native');
const {
View,
Text,
Animated,
StyleSheet,
TouchableWithoutFeedback,
} = ReactNative;
var UIExplorerButton = require('./UIExplorerButton');

class Tester extends React.Component {
state = {
Expand All @@ -47,12 +46,8 @@ class Tester extends React.Component {
...this.props.config,
toValue: this.current,
};
try {
Animated[this.props.type](this.state.native, { ...config, useNativeDriver: true }).start();
} catch (e) {
// uncomment this if you want to get the redbox errors!
throw e;
}

Animated[this.props.type](this.state.native, { ...config, useNativeDriver: true }).start();
Animated[this.props.type](this.state.js, { ...config, useNativeDriver: false }).start();
};

Expand All @@ -78,6 +73,52 @@ class Tester extends React.Component {
}
}

class ValueListenerExample extends React.Component {
state = {
anim: new Animated.Value(0),
progress: 0,
};
_current = 0;

componentDidMount() {
this.state.anim.addListener((e) => this.setState({ progress: e.value }));
}

componentWillUnmount() {
this.state.anim.removeAllListeners();
}

_onPress = () => {
this._current = this._current ? 0 : 1;
const config = {
duration: 1000,
toValue: this._current,
};

Animated.timing(this.state.anim, { ...config, useNativeDriver: true }).start();
};

render() {
return (
<TouchableWithoutFeedback onPress={this._onPress}>
<View>
<View style={styles.row}>
<Animated.View
style={[
styles.block,
{
opacity: this.state.anim,
}
]}
/>
</View>
<Text>Value: {this.state.progress}</Text>
</View>
</TouchableWithoutFeedback>
);
}
}

const styles = StyleSheet.create({
row: {
padding: 10,
Expand Down Expand Up @@ -304,4 +345,13 @@ exports.examples = [
);
},
},
{
title: 'Animated value listener',
platform: 'android',
render: function() {
return (
<ValueListenerExample />
);
},
},
];

0 comments on commit 3292d8f

Please sign in to comment.