React-Native组件ImageBackground

Dear 丶 2022-05-30 04:53 339阅读 0赞

1.基础知识

在RN版本0.46版本的时候添加了ImageBackground控件,在0.46版本以后使用Image的时候不能在嵌套使用,ImageBackground就是解决这个问题的,现在如果在 标签中嵌套其他组件现在会报黄盒警告。ImageBackground的使用和Image一样,只不过可以嵌套其他组件了。

在 “react-native”: “0.54.0”,中,直接会出现红屏,使用的方式如下所示:

  1. <ImageBackground
  2. style={
  3. {height:100,width:300}}
  4. resizeMode='cover'
  5. >
  6. <Text style={
  7. {color:'red',fontSize:24}}> image 嵌入 text</Text>
  8. </ImageBackground>

下面是具体的实现代码:

  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @providesModule ImageBackground
  10. * @flow
  11. * @format
  12. */
  13. 'use strict';
  14. const Image = require('Image');
  15. const React = require('React');
  16. const StyleSheet = require('StyleSheet');
  17. const View = require('View');
  18. const ensureComponentIsNative = require('ensureComponentIsNative');
  19. import type {NativeMethodsMixinType} from 'ReactNativeTypes';
  20. /**
  21. * Very simple drop-in replacement for <Image> which supports nesting views.
  22. *
  23. * ```ReactNativeWebPlayer
  24. * import React, { Component } from 'react';
  25. * import { AppRegistry, View, ImageBackground, Text } from 'react-native';
  26. *
  27. * class DisplayAnImageBackground extends Component {
  28. * render() {
  29. * return (
  30. * <ImageBackground
  31. * style={
  32. {width: 50, height: 50}}
  33. * source={
  34. {uri: 'https://facebook.github.io/react-native/img/opengraph.png'}}
  35. * >
  36. * <Text>React</Text>
  37. * </ImageBackground>
  38. * );
  39. * }
  40. * }
  41. *
  42. * // App registration and rendering
  43. * AppRegistry.registerComponent('DisplayAnImageBackground', () => DisplayAnImageBackground);
  44. * ```
  45. */
  46. class ImageBackground extends React.Component<$FlowFixMeProps> {
  47. setNativeProps(props: Object) {
  48. // Work-around flow
  49. const viewRef = this._viewRef;
  50. if (viewRef) {
  51. ensureComponentIsNative(viewRef);
  52. viewRef.setNativeProps(props);
  53. }
  54. }
  55. _viewRef: ?NativeMethodsMixinType = null;
  56. _captureRef = ref => {
  57. this._viewRef = ref;
  58. };
  59. render() {
  60. const {children, style, imageStyle, imageRef, ...props} = this.props;
  61. return (
  62. <View style={style} ref={this._captureRef}>
  63. <Image
  64. {...props}
  65. style={[
  66. StyleSheet.absoluteFill,
  67. {
  68. // Temporary Workaround:
  69. // Current (imperfect yet) implementation of <Image> overwrites width and height styles
  70. // (which is not quite correct), and these styles conflict with explicitly set styles
  71. // of <ImageBackground> and with our internal layout model here.
  72. // So, we have to proxy/reapply these styles explicitly for actual <Image> component.
  73. // This workaround should be removed after implementing proper support of
  74. // intrinsic content size of the <Image>.
  75. width: style.width,
  76. height: style.height,
  77. },
  78. imageStyle,
  79. ]}
  80. ref={imageRef}
  81. />
  82. {children}
  83. </View>
  84. );
  85. }
  86. }
  87. module.exports = ImageBackground;

从代码可以看出,只是仅仅对 Image 进行了View的包装而已,并且能够看到将包含在ImageBackground 中的 children 使用Image进行了包装,给外层抛出一下三个接口: style, imageStyle, imageRef,从官网能够得知,唯一的不同就是名字的不同,属性与用法完全和Image一致。

官网说明如下链接:
1.react-native中背景图片属性ImageBackground
2.react-native中背景图片属性ImageBackground-中文网

2.实例操作

如何将背景图片修饰为圆形,并且在里边输入文字

  1. <ImageBackground source={imageSrc} style={ { justifyContent: 'center', alignItems: 'center', width: 60, height: 60, resizeMode: 'cover', borderRadius: 60 / 2, cursor: 'pointer', overflow: 'hidden', borderWidth: 0, borderColor: 'rgba(200,200,200,0.5)' }} > <Text>{'客'}</Text> </ImageBackground>

如上所示代码,通过宽高进行控制一个正方形,然后再通不过属性borderRadius进行控制圆角,由于是直径的一半,所以最终构造成圆形,在这个时候会出现在ios系统中仍然是一个正方形,需要使用属性overflow: 'hidden', 来控制最终圆角的形成,ios中默认给控件加圆角时圆角之外的图案是还在的,在react-native中需要设置css的一个样式 overflow:hidden属性,最终实现效果如下图所示:

圆形图片效果图

发表评论

表情:
评论列表 (有 0 条评论,339人围观)

还没有评论,来说两句吧...

相关阅读