RN渐变色组件

1,556 阅读2分钟

自行安装react-native-linear-gradient这个组件

import React from 'react';
import { View } from 'react-native';
import LinearGradient from 'react-native-linear-gradient';

const GradientLine = () => {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <LinearGradient
        colors={['#CCCCCC', '#FFFFFF', '#CCCCCC']} // 这里为过渡的颜色,多少颜色就写多少就这样
        start={{ x: 0.5, y: 0 }}
        end={{ x: 0.5, y: 1 }}
        style={{ width: 2, height: '80%' }}>
        {/* 这里放你的内容 */}
      </LinearGradient>
    </View>
  );
};

export default GradientLine;

在 LinearGradient 组件中,start 和 end 属性用于定义渐变的起始点和结束点。

  • start: 用于指定渐变的起始点,是一个包含 x 和 y 属性的对象。x 和 y 属性的值表示起始点在渐变矩形中的位置,取值范围为 [0, 1],其中 0 表示左边(或顶部),1 表示右边(或底部)。
  • end: 用于指定渐变的结束点,同样是一个包含 x 和 y 属性的对象。x 和 y 属性的值表示结束点在渐变矩形中的位置,取值范围为 [0, 1]。

通过调整 start 和 end 属性中 x 和 y 的值,可以控制渐变的方向和位置。

例如,如果想要创建水平方向的渐变,可以将 start 的 x 设为 0,end 的 x 设为 1,start 和 end 的 y 都设为 0.5。如果想要创建垂直方向的渐变,可以将 start 的 y 设为 0,end 的 y 设为 1,start 和 end 的 x 都设为 0.5。

案例

背景初始为透明色 输入1-100之间的数字 会有白色和红色从下往上对应透明度的渐变色(从透明慢慢显示) 做这个案例初衷是为了适配我这个组件,当向上滑动时让上面的透明块背景渐变就这样

import React, { useState } from 'react';
import { View, TextInput, StyleSheet, SafeAreaView, Text } from 'react-native';
import LinearGradient from 'react-native-linear-gradient';

const App = () => {
  const [gradientPercent, setGradientPercent] = useState(0);

  const handleInputChange = (text) => {
    // Ensure the input is a valid number between 0 and 100
    const value = Math.min(100, Math.max(0, parseInt(text, 10) || 0));
    setGradientPercent(value);
  };

  // Calculate the opacity for the gradient
  const opacity = gradientPercent / 100;

  // Create gradient colors with dynamic opacity
  const gradientColors = [
    `rgba(255, 255, 255, ${opacity})`,
    `rgba(255, 0, 0, ${opacity})`
  ];

  return (
    <SafeAreaView style={styles.container}>
      <LinearGradient
        colors={gradientColors}
        style={styles.gradient}
        start={{ x: 0.5, y: 1 }}
        end={{ x: 0.5, y: 0 }}
      >
        <View style={styles.inputContainer}>
          <TextInput
            style={styles.input}
            onChangeText={handleInputChange}
            placeholder="Enter a value from 0 to 100"
            keyboardType="numeric"
          />
          <Text style={styles.text}>Gradient Percent: {gradientPercent}%</Text>
        </View>
      </LinearGradient>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  gradient: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  inputContainer: {
    alignItems: 'center',
    backgroundColor: 'rgba(255, 255, 255, 0.5)', // 半透明背景以便于查看输入内容
    padding: 20,
    borderRadius: 10,
  },
  input: {
    height: 40,
    borderColor: 'gray',
    borderWidth: 1,
    width: '80%',
    marginBottom: 20,
    paddingHorizontal: 10,
    backgroundColor: '#fff',
  },
  text: {
    fontSize: 18,
    color: '#000',
  },
});

export default App;