React Native:动画在 android 上无法正常工作

React Native: Animation not working properly on android

过去 2 天我一直在尝试解决问题,它在 iOS

上运行良好
constructor(){
  super();

  this.animation = new Animated.Value(0)

  this.state = {
    expanded: false,
  };
}
toggle(){
  let initialValue = this.state.expanded? 1 : 0 ,
     finalValue = this.state.expanded? 0 : 1

  this.setState({
     expanded: !this.state.expanded,
  });

  this.animation.setValue(initialValue)
  Animated.timing(
    this.animation,
    {
      toValue: finalValue,
    }
  ).start();
}

render(){
  return(
    <Animated.View style={{{flex: 1, marginTop: 28, paddingLeft: 25, transform: [{ scale: this.animation }, {perspective: 1000 }]}}}>
     ....
    </Animated.View>

  );
}

这个组件是子组件,在父组件中这样使用:<FeedBack ref={ref => this.feedback = ref}/>没有任何条件来检查是否显示(因为动画比例在构造函数中设置为零,所以不需要)

当按下按钮时,正在从父级调用 toggle() 方法。

现在 this works fine on iOS ,当组件加载时,视图不存在,直到按下按钮(然后缩放)。但是在 android 组件加载时,视图已经存在。当我按下按钮时,动画视图消失并重新出现(带有动画缩放)并且随后的切换工作正常。 android 中的问题是,即使 initialValue 的比例为零,视图在首次加载时仍然存在。

android 方面 react-native 的问题已经存在一段时间了(叹息)。似乎将值设置为 0 剥夺了它的特性,基本上将其视为 null ,然后在动画设置为 > 0

后恢复使用其实际值

解决方法是像这样设置动画 this.animation = new Animated.Value(0.01);

可以关注issuehere