react-native-linear-gradient颜色渐变

一 安装

npm i react-native-linear-gradient

react-native link react-native-linear-gradient

二 使用

2.1 colors

默认情况下,渐变色的方向是从上向下的

<LinearGradient colors={['#63B8FF', '#1C86EE', '#0000EE',]} style={{height: 150}}>
</LinearGradient>

2.2 start / end

你想渐变色从左向右,或者斜角渐变,就需要设置下了

start:{ x: number, y: number }
end:{ x: number, y: number }

start 就是渐变开始的位置,x、y 范围是 0 - 1 ,
end 同上,就是渐变结束的位置

eg1:斜角渐变

start: { x: 0.3, y: 0.4 } 渐变是从 左侧30%, 上部 40% 开始
end: { x: 0.7, y: 0.8 } 渐变是从 左侧70%, 上部 80% 结束
<LinearGradient
    start={{x: 0.25, y: 0.25}}
    end={{x: 0.75, y: 0.75}}
    colors={['red', 'green', 'black']}
    style={{height: 150, flex: 1}}>
</LinearGradient>

eg2: 从左到右

从左到右的渐变就可以设置出来了

start={{x: 0, y: 0}} 
end={{x: 1, y: 0}}

2.2 locations

假如想指定每种渐变颜色的范围,比如红色占20%, 绿色占70%,黑色占10%,也是可以设置的,就用到了另外一个属性了 locations

locations 对应的是 colors
locations={[0.2,0.7,1.0]}
colors={['red', 'green', 'black']}

red 范围就是 0.0 - 0.2
green 范围就是 0.2 - 0.7
black 范围就是 0.7 - 1.0

eg1: 0.4是渐变的起点,0.6是渐变的终点

colors={['red', 'green', 'black']}
locations={[0.4,0.5,0.6]}

posted @ 2019-01-09 21:09  qiqi715  阅读(4857)  评论(0编辑  收藏  举报