美文网首页
ImageBackground源码解读与背景样式属性imageS

ImageBackground源码解读与背景样式属性imageS

作者: vincent_z | 来源:发表于2018-01-19 13:58 被阅读0次

背景

React Native 组件本身并不支持web开发中background-image样式属性,而是采用背景图片组件ImageBackground来实现背景图片的效果。因为项目开发需要,在使用ImageBackground过程要要实现背景圆角border-radius,发现官方没有提供ImageBackground组件style的文档,并且发现直接传递给style属性的样式并不作用于背景组件本身。好奇之下,阅读了下该组件的源码,实现方式特别简单,但是有一个属性尤为重要:imageStyle

核心源码

class ImageBackground extends React.Component<$FlowFixMeProps> {
  setNativeProps(props: Object) { ... }
  ...
  render() {
    const {children, style, imageStyle, imageRef, ...props} = this.props;
    return (
      <View style={style} ref={this._captureRef}>
        <Image
          {...props}
          style={[
            StyleSheet.absoluteFill,
            {
              // Temporary Workaround:
              // Current (imperfect yet) implementation of <Image> overwrites width and height styles
              // (which is not quite correct), and these styles conflict with explicitly set styles
              // of <ImageBackground> and with our internal layout model here.
              // So, we have to proxy/reapply these styles explicitly for actual <Image> component.
              // This workaround should be removed after implementing proper support of
              // intrinsic content size of the <Image>.
              width: style.width,
              height: style.height,
            },
            imageStyle,
          ]}
          ref={imageRef}
        />
        {children}
      </View>
    );
  }
}

先看结构,

<View>
  <Image></Image>
  {children}  // 这里的children可以是任意组件
</View>

ImageBackground的实现方式:<View>组件中包含一个<Image>和任意其它组件,稍微提炼下:

      <View style={style} ref={this._captureRef}>
        <Image
          {...props}
          style={[
            StyleSheet.absoluteFill,
            {
              width: style.width,
              height: style.height,
            },
            imageStyle,
          ]}
          ref={imageRef}
        />
        {children}
      </View>

传入的数据有children, style, imageStyle, imageRef 还有其它代表剩余属性值的...props,可以看到,传入的style直接作用在<View>组件上并不作用在<Image>children是与<Image>平级的子组件,imageStyle和其它属性值比如source直接传入Image组件,这里的imageStyle就是我们需要注意的,因为它接收的样式会作用于<Image>,这里的StyleSheet.absoluteFill来自于react-native/Libraries/StyleSheet/StyleSheet.js

const absoluteFillObject = {
  position: 'absolute',
  left: 0,
  right: 0,
  top: 0,
  bottom: 0,
};
const absoluteFill = ReactNativePropRegistry.register(absoluteFillObject); // This also freezes it

这里的<Image>是绝对定位,并且会填充整个<View>扮演背景图片,也就是说imageStyle就是背景图片的样式,然而,官方文档并未指明这样一个隐藏的重要属性

例子

<ImageBackground
  source={{url:".../profile" }}
  style={{
    width: 150,
    height: 150,
    justifyContent: "center",
    alignItems: "center"
  }}
>
  <Text style={{ color: "white" }}>hello vincent</Text>
</ImageBackground>
预览
imageStyle={{
  borderColor: "grey",
  borderWidth: 2,
  borderRadius: 75
}}
预览

谢谢~

相关文章

  • ImageBackground源码解读与背景样式属性imageS

    背景 React Native 组件本身并不支持web开发中background-image样式属性,而是采用背景...

  • CSS背景

    CSS 属性定义背景效果: background-colorbackground-imagebackground-...

  • 背景

    CSS3中包含几个新的背景属性,提供更大背景元素控制。background-imagebackground-siz...

  • RN学习笔记2019.10.21

    常用组件属性备份记录,方便后期进一步学习总结,包括FlatList的属性、ImageBackground背景图片填...

  • React Native 控件样式使用

    显示图片 在RN中没有单独设置图片背景的属性,需要用ImageBackground Listview显示列表 Te...

  • css样式入门书目录

    css样式-字体属性 css样式-背景属性 css样式-边框属性 css样式-列表属性 css样式-定位属性 cs...

  • 2018-07-10 CSS样式表

    常用的样式属性(文本属性): 常用的样式属性(背景属性): 常用的样式属性(方框属性): 常用的样式属性: 内嵌样...

  • CSS常用样式

    文字属性控制字体设置文本格式 边框 边界 列表符号属性 背景样式 连接属性 a 鼠标光标样式 边框基本样式属性 边...

  • CSS-1

    全局样式 类样式 背景样式 外部样式 文本样式 属性属性值作用color表示颜色的内容设置文本颜色directio...

  • web前端入门到实战:11种常用css样式之文本字体学习

    常见css样式:1.字体与颜色2.背景属性3.文本属性4.边框属性5.鼠标光标属性6.列表样式7.定位属性8.内外...

网友评论

      本文标题:ImageBackground源码解读与背景样式属性imageS

      本文链接:https://www.haomeiwen.com/subject/xnkooxtx.html