Som*_*ame 3 reactjs react-native react-animated react-animations
我有2个AnimatedAPI 问题.
1st:我可以使用以下代码从左到右显示图像.我想将图像从位置缩放X=40 (leftPadding), Y=100(topPadding), height:20, width:20到X=20, Y=10, height:250, width:300.我该如何实现这一目标?
我的代码:
import React, { Component } from 'react';
import { StyleSheet, Text, Image, Animated, Easing, View, Button } from 'react-native';
class MyTestComp extends Component {
componentWillMount() {
this.animatedValue = new Animated.Value(0);
}
buttonPress(){
this.animatedValue.setValue(0);
Animated.timing(this.animatedValue,{
toValue:1,
duration:1000,
Easing: Easing
}).start()
}
render() {
const translateX = this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [-500, 1]
})
const transform = [{translateX}];
return (
<View>
<Text>MyTestComp</Text>
<Animated.View style={transform}>
<Image
source={require('./assets/17.jpg')}
style={{width:300, height:250}}
/>
</Animated.View>
<View style={{marginTop:10}}>
<Button title="Click Me" onPress={()=> this.buttonPress()} />
</View>
</View>
);
}
}
export default MyTestComp;
Run Code Online (Sandbox Code Playgroud)
第二:每次我运行动画时,我都会遇到异常:
我无法找到任何关于此的文档.我如何使用transform道具.
非常感谢.
K.W*_*.Wu 21
我想这就是你想要的:
动画实际上非常流畅,在GIF中看起来并不是这样,因为GIF是每秒4帧.这是代码(因为你的数字都是常量,我只是在下面的代码中硬编码了所有这些代码):
import React, { Component } from 'react'
import { Animated, View, TouchableOpacity, Easing } from 'react-native'
const backgroundImage = require('....')
class App extends Component {
constructor(props) {
super(props)
this.animatedValue = new Animated.Value(0)
}
handleAnimation = () => {
Animated.timing(this.animatedValue, {
toValue: 1,
duration: 1000,
easing: Easing.ease
}).start()
}
render() {
return (
<View style={{ flex: 1 }}>
<TouchableOpacity onPress={this.handleAnimation}>
<Text>Transform</Text>
</TouchableOpacity>
<Animated.Image
source={backgroundImage}
resizeMode='cover'
style={{
position: 'absolute',
left: 40,
top: 100,
height: 20,
width: 20,
transform: [
{
translateX: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [0, 120]
})
},
{
translateY: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [0, 25]
})
},
{
scaleX: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [1, 15]
})
},
{
scaleY: this.animatedValue.interpolate({
inputRange: [0, 1],
outputRange: [1, 12.5]
})
}
]
}}
/>
</View>
)
}
}
export default App
Run Code Online (Sandbox Code Playgroud)
一些解释:
在动画之后,图像的宽度变为300,280像素变大,因为图像从中心向上缩放,因此,图像的x坐标离开了移位的140px或-140px,并且我们希望x坐标向左移位仅20px,因此,我们应该右移它120px,这就是x的输出范围[0, 120]
y输出范围的原因相同 [0, 25]
宽度现在300与之前相比20,这是15更大的时间
现在250比较之前的高度20,这是12.5更大的时间
| 归档时间: |
|
| 查看次数: |
13562 次 |
| 最近记录: |