react-native-linear-gradient在react-native实现线性渐变

一、安装依赖与关联

// 安装依赖
yarn add react-native-linear-gradient
// 自动进行关联
yarn react-native link react-native-linear-gradient

二、线性渐变的使用

  1. 默认的线性渐变-从上到下

栗子

在这里插入图片描述

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

export default function TestScreen() {
    return (
    <ScrollView>
        <View>
            <LinearGradient
                colors={['red', '#375BB1']}
                style={{ height: 50, marginTop: 50 }}>
                <View style={{ justifyContent: 'center', alignItems: 'center', height: 50 }}>
                    <Text style={{ color: '#ffffff', fontSize: 28 }}>Test Screen</Text>
                </View>
            </LinearGradient>
        </View>
    </ScrollView>
    );
}

  1. 设置线性渐变的方向-起始位置start,end

栗子

在这里插入图片描述

<LinearGradient
    colors={['red', '#375BB1']}
    start={{ x: 0, y: 0 }}
    end={{x: 1, y: 0}}
    style={{ height: 50, marginTop: 50 }}>
    <View style={{ justifyContent: 'center', alignItems: 'center', height: 50 }}>
        <Text style={{ color: '#ffffff', fontSize: 28 }}>Test Screen</Text>
    </View>
</LinearGradient>
  1. 设置渐变色的位置-locations

栗子

在这里插入图片描述

<LinearGradient
    colors={['#4C80CE', 'red', '#375BB1']}
    start={{ x: 0, y: 0 }}
    end={{x: 1, y: 0}}
    locations={[0.2, 0.5, 0.8]}// 为对应颜色的位置
    style={{ height: 50, marginTop: 50 }}>
    <View style={{ justifyContent: 'center', alignItems: 'center', height: 50 }}>
        <Text style={{ color: '#ffffff', fontSize: 28 }}>Test Screen</Text>
    </View>
</LinearGradient>
  1. 设置旋转角度

栗子

在这里插入图片描述

<LinearGradient
    colors={['red', '#375BB1']}
    useAngle={true}// 开启旋转
    angle={90}// 旋转角度,0的时候为从下到上渐变,按照角度顺时针旋转
    angleCenter={{ x: 0.5, y: 0.5}}// 旋转中心
    style={{ height: 50, marginTop: 50 }}>
    <View style={{ justifyContent: 'center', alignItems: 'center', height: 50 }}>
        <Text style={{ color: '#ffffff', fontSize: 28 }}>Test Screen</Text>
    </View>
</LinearGradient>

三、补充

ios中可以实现文字渐变,具体请参考npm包中的官方文档

参考文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值