gpt4 book ai didi

javascript - 动画渐变颜色 React Native

转载 作者:数据小太阳 更新时间:2023-10-29 06:05:13 24 4
gpt4 key购买 nike

我正在尝试在 React Native 中创建一个渐变,该渐变将在应用程序打开时以一种颜色开始,然后每 30 秒逐渐变为另一种颜色。常规线性渐变在不尝试添加动画的情况下工作。我尝试使用插值和动画计时,如 React Native 文档中所示,但似乎没有任何效果。

我的代码:

import React, {Component} from 'react';
import {processColor, AppRegistry, StyleSheet, Dimensions, Animated, Image, Easing, View} from 'react-native';

import TimerMixin from 'react-timer-mixin';
import LinearGradient from 'react-native-linear-gradient';

var screenWidth = Dimensions.get('window').width;
var screenHeight = Dimensions.get('window').height;

//HEX version of colors
var gradientColors = [['#EF2A2A', '#EF6A2A'], //Red
['#EF6A2A', '#EFD82A'], //Orange
['#1BD170', '#61E822'], //Green
['#22D2E6', '#26F084'], //Aqua
['#2A3BEF', '#2ADCEF'], //Blue
['#EF2AD2', '#2A3BEF'], //Purple
['#EF2AD2', '#EF2A2A'] //Pink
]
var gradientColorsNoHash = [['EF2A2A', 'EF6A2A'], //Red
['EF6A2A', 'EFD82A'], //Orange
['1BD170', '61E822'], //Green
['22D2E6', '26F084'], //Aqua
['2A3BEF', '2ADCEF'], //Blue
['EF2AD2', '2A3BEF'], //Purple
['EF2AD2', 'EF2A2A'] //Pink
]
/*var gradientColors = [['ef2a2a', 'ef6a2a'], //Red
['ef6a2a', 'efd82a'], //Orange
['1bd170', '61e822'], //Green
['22d2e6', '26f084'], //Aqua
['2a3bef', '2adcef'], //Blue
['ef2ad2', '2a3bef'], //Purple
['ef2ad2', 'ef2a2a'] //Pink
]*/
//RGBA Version of Colors
/*var gradientColors = [['rgba(239, 42, 42, 1)', 'rgba(239, 106, 42, 1)'], //Red
['rgba(239, 106, 42, 1)', 'rgba(239, 216, 42, 1)'], //Orange
['rgba(0, 221, 103, 1)', 'rgba(97, 232, 35, 1)'], //Green
['rgba(34, 210, 230, 1)', 'rgba(38, 240, 132, 1)'], //Aqua
['rgba(42, 59, 239, 1)', 'rgba(42, 220, 239, 1)'], //Blue
['rgba(239, 42, 210, 1)', 'rgba(42, 59, 239, 1)'], //Purple
['rgba(239, 42, 210, 1)', 'rgba(239, 42, 42, 1)'] //Pink
]*/

function hex(c) {
var s = "0123456789abcdef";
var i = parseInt(c);
if (i == 0 || isNaN(c))
return "00";
i = Math.round(Math.min (Math.max (0, i), 255));
//console.log('hex(c) complete!');
return s.charAt((i - i % 16) / 16) + s.charAt(i % 16);
}

// Convert an RGB triplet to a hex string
function convertToHex (rgb) {
return hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}

// Convert a hex string to an RGB triplet
function convertToRGB(hex) {
var color = [];
color[0] = parseInt(hex.substring(0, 2), 16);
color[1] = parseInt(hex.substring(2, 4), 16);
color[2] = parseInt(hex.substring(4, 6), 16);
return color;
}

function generateColor(colorStart,colorEnd,colorCount) {

// The beginning of your gradient
var start = convertToRGB(colorStart);

// The end of your gradient
var end = convertToRGB(colorEnd);

// The number of colors to compute
var len = colorCount;

//Alpha blending amount
var alpha = 0.0;

var saida = [];

for (i = 0; i < len; i++) {
var c = [];
alpha += (1.0/len);

c[0] = start[0] * alpha + (1 - alpha) * end[0];
c[1] = start[1] * alpha + (1 - alpha) * end[1];
c[2] = start[2] * alpha + (1 - alpha) * end[2];

saida.push(convertToHex(c));

}

return saida;

}

var number = randomIntFromInterval(0,6)
function randomIntFromInterval(min,max) { return Math.floor(Math.random()*(max-min+1)+min); }

const GradientView = React.createClass({
mixins: [TimerMixin],

getInitialState() {
return {
gradIndex: number,
colorTop: gradientColors[number][0],
colorBottom: gradientColors[number][1],
}
},
componentDidMount() {
this.setInterval(() => {

var count = 0
var topGradArray = generateColor(gradientColorsNoHash[this.state.gradIndex][0],(this.state.gradIndex === 6 ? 0 : gradientColorsNoHash[this.state.gradIndex+1][0] ),770);
var bottomGradArray = generateColor(gradientColorsNoHash[this.state.gradIndex][1],(this.state.gradIndex === 6 ? 0 : gradientColorsNoHash[this.state.gradIndex+1][1] ),770);
console.log('Gradients Made');
var clearId = this.setInterval(() => {
if (count == 0) {
this.setState({ clearId: clearId, gradIndex: ( this.state.gradIndex === 6 ? 0 : this.state.gradIndex+1 ) });
console.log('clearId SET!');
}

this.setState({
colorTop: processColor(topGradArray[count]),
colorBottom: processColor(bottomGradArray[count]),
});
count = count+1

if (count == 769) {
console.log('colorTop and Bottom Saved');
this.clearInterval(this.state.clearId)
}
}, 13);

}, 30000);
},

render(){
return(
<LinearGradient colors={[this.state.colorTop, this.state.colorBottom]}>
<View style={styles.translucentContainer}/>
</LinearGradient>
);
}
});

const styles = StyleSheet.create({
translucentContainer: {
width: screenWidth,
height: screenHeight,
backgroundColor: 'white',
opacity: 0.3,
},
});

export default GradientView;
AppRegistry.registerComponent('GradientView', () => GradientView);

更新:在浏览了许多不同的资源之后,我得出的结论是,为 LinearGradient 类设置动画的唯一方法是像他们的文档中那样以增量方式快速更改颜色。但是,他们的示例是连续的,不允许您设置所需的最终颜色。对于我的应用程序,我希望渐变保持一种颜色 30 秒,然后经过 10 秒过渡到下一个颜色渐变,然后重复。例如,它看起来像:红色渐变(30 秒)、红色到橙色的过渡(10 秒)、橙色渐变(30 秒)、橙色到绿色的过渡(10 秒)等。

我在这段代码中遇到了两种似乎交替发生的错误。通常,第一个错误是第一个计时器(30 秒)响起时出现的错误:

The first error

在关闭该错误消息以查看会发生什么后,当同一计时器再次关闭时会弹出此错误:

The second error

在这一点上,我认为错误的根源在于在 componentDidMount()

中包含的函数中正确生成颜色

最佳答案

我找到了可行的解决方案!

使用线性插值生成渐变。这是我发现的正确控制渐变的最简单方法。

chroma.js :

我找到了一个名为 chroma.js 的库那可以做到这一点!他们有一个方法叫做 scale.colors可以为您完成这项工作!

安装包:

npm install chroma-js

您可以调整 INTERVALGRADIENT_COLOR_LENGTH 常量来改变效果。

然后在代码中使用生成的频谱变量:

import React from 'react'
import { AppRegistry, StyleSheet, Dimensions, View } from 'react-native'

import TimerMixin from 'react-timer-mixin'
import LinearGradient from 'react-native-linear-gradient'
import Chroma from 'chroma-js'

var screenWidth = Dimensions.get('window').width
var screenHeight = Dimensions.get('window').height

const TOP_COLORS = ['#EF2A2A', '#EF6A2A', '#1BD170', '#22D2E6', '#2A3BEF', '#EF2AD2', '#EF2AD2']
const BOTTOM_COLORS = ['#EF6A2A', '#EFD82A', '#61E822', '#26F084', '#2ADCEF', '#2A3BEF', '#EF2A2A']
const GRADIENT_COLOR_LENGTH = 700
const TOP_COLORS_SPECTRUM = Chroma.scale(TOP_COLORS).colors(GRADIENT_COLOR_LENGTH)
const BOTTOM_COLORS_SPECTRUM = Chroma.scale(BOTTOM_COLORS).colors(GRADIENT_COLOR_LENGTH)
const INTERVAL = 50

const GradientView = React.createClass({
mixins: [TimerMixin],

getInitialState () {
return {
topIndex: 0,
bottomIndex: 0,
colorTop: TOP_COLORS_SPECTRUM[0],
colorBottom: BOTTOM_COLORS_SPECTRUM[0]
}
},

componentDidMount () {
this.setInterval(() => {
let { topIndex, bottomIndex } = this.state

topIndex++
if (topIndex === TOP_COLORS_SPECTRUM.length) {
topIndex = 0
}

bottomIndex++
if (bottomIndex === BOTTOM_COLORS_SPECTRUM.length) {
bottomIndex = 0
}

this.setState({
topIndex: topIndex,
bottomIndex: bottomIndex,
colorTop: TOP_COLORS_SPECTRUM[topIndex],
colorBottom: BOTTOM_COLORS_SPECTRUM[bottomIndex]
})
}, INTERVAL)
},

render () {
return (
<LinearGradient colors={[this.state.colorTop, this.state.colorBottom]}>
<View style={styles.translucentContainer} />
</LinearGradient>
)
}
})

const styles = StyleSheet.create({
translucentContainer: {
width: screenWidth,
height: screenHeight,
backgroundColor: 'white',
opacity: 0.3
}
})

export default GradientView
AppRegistry.registerComponent('GradientView', () => GradientView)

关于javascript - 动画渐变颜色 React Native,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41750932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com