-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.android.js
177 lines (171 loc) · 4.71 KB
/
index.android.js
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
Animated,
TouchableHighlight,
Easing,
} from 'react-native';
class CustomButton extends Component {
render() {
return (
<TouchableHighlight
style={styles.button}
underlayColor="#a5a5a5"
onPress={this.props.onPress}>
<Text style={styles.buttonText}>{this.props.text}</Text>
</TouchableHighlight>
);
}
}
//视图淡入效果
class FadeInView extends React.Component {
state: any;
constructor(props) {
super(props);
this.state = {
fadeAnim: new Animated.Value(0), // 透明度为0
};
}
componentDidMount() {
Animated.timing( // 使用timing过渡动画
this.state.fadeAnim, // 开启动画的值
{
toValue: 1, // 目标值
duration: 3500, // 配置延续时间
},
).start(); // 开启动画
}
render() {
return (
<Animated.View // 特殊带有动画的View视图
style={{
opacity: this.state.fadeAnim, // Binds
}}>
{this.props.children}
</Animated.View>
);
}
}
class AnimatedDemo extends Component {
constructor(props) {
super(props);
this.state = {
show: true,
anim: new Animated.Value(0),
compositeAnim: new Animated.Value(0),
};
}
render() {
return (
<View style={{margin:20}}>
<Text style={styles.welcome}>
Animated使用实例
</Text>
<CustomButton text="动画:视图淡入效果"
onPress={()=>{
this.setState((state) => (
{show: !state.show}
));
}}
/>
{this.state.show && <FadeInView>
<View style={styles.content}>
<Image source={require('./imgs/logo.jpg')} style={{width:50,height:50}}/>
</View>
</FadeInView>}
<CustomButton text="动画:加入插值效果移动"
onPress={()=>{
Animated.spring(this.state.anim, {
toValue: 0,
velocity: 7,
tension: -20,
friction: 3,
}).start();
}}
/>
<Animated.View
style={[styles.content, {
transform: [
{scale: this.state.anim.interpolate({
inputRange: [0, 1],
outputRange: [1, 3],
})},
{translateX: this.state.anim.interpolate({
inputRange: [0, 1],
outputRange: [0, 300],
})},
{rotate: this.state.anim.interpolate({
inputRange: [0, 1],
outputRange: [
'0deg', '720deg'
],
})},
]}
]}>
<Image source={require('./imgs/logo.jpg')} style={{width:50,height:50}}/>
</Animated.View>
<CustomButton text="动画:组合动画效果"
onPress={()=>{
Animated.sequence([
Animated.timing(this.state.compositeAnim, {
toValue: 100,
easing: Easing.linear,
}),
Animated.delay(200),
Animated.timing(this.state.compositeAnim, {
toValue: 0,
easing: Easing.elastic(2),
}),
Animated.delay(100),
Animated.timing(this.state.compositeAnim, {
toValue: 50,
easing: Easing.linear,
}),
Animated.timing(this.state.compositeAnim, {
toValue: 0,
easing: Easing.elastic(1),
})
]).start();
}}
/>
<Animated.View
style={[styles.content, {
bottom:this.state.compositeAnim
}]}>
<Image source={require('./imgs/logo.jpg')} style={{width:50,height:50}}/>
</Animated.View>
</View>
);
}
}
const styles = StyleSheet.create({
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
button: {
margin:5,
backgroundColor: 'white',
padding: 15,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#cdcdcd',
},
content: {
backgroundColor: 'green',
borderWidth: 1,
padding: 5,
margin: 20,
alignItems: 'center',
},
});
AppRegistry.registerComponent('AnimatedDemo', () => AnimatedDemo);