javascript - 在 React Native 中设置背景颜色动画

标签 javascript react-native

我将如何在 React Native 中将动画从一种颜色转换为另一种颜色。我发现通过插入 Animated.Value 可以通过以下方式为颜色设置动画:

var BLACK = 0;
var RED = 1;
var BLUE = 2;

backgroundColor: this.state.color.interpolate({
  inputRange: [BLACK, RED, BLUE],
  outputRange: ['rgb(0, 0, 0)', 'rgb(255, 0, 0)', 'rgb(0, 0, 255)']
})

Animated.timing(this.state.color, {toValue: RED}).start();

但是使用这种方法,从黑色到蓝色,你必须经过红色。在混音中添加更多颜色,您最终会进入 1980 年代的迪斯科舞厅。

是否有另一种方法可以让您直接从一种颜色转到另一种颜色?

最佳答案

假设你有 Animated.Value 让我们说 x,你可以像这样插入颜色:

render() {
    var color = this.state.x.interpolate({
        inputRange: [0, 300],
        outputRange: ['rgba(255, 0, 0, 1)', 'rgba(0, 255, 0, 1)']
    });

    return (
        <Animated.View style={{backgroundColor:color}}></Animated.View>
    );
}

您可以在我发布在 github 上的问题中找到完整的工作示例.

关于javascript - 在 React Native 中设置背景颜色动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622466/

相关文章:

javascript - 在 Heroku 中使用 webpack 部署 Rails 应用程序时出错

android - react-native编译错误: The SDK Build Tools revision is too low for project

javascript - 外部JS : Can't validate form

javascript - Node.JS fs.readFileSync() 错误参数

reactjs - 找不到模块 'react-faq-component' 的声明文件

javascript - 如何在不提供任何填充的情况下将背景颜色应用于 View ?

javascript - 渲染警告 : Received `true` for a non-boolean attribute `className`

sqlite - react-native array.map 与 asyncstorage.getItem

javascript - 在追加之前检查 HTML 元素是否存在

javascript - 单击链接后 Bootstrap 导航栏崩溃