React Native组件ImageBackground

1.基础知识

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

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

 <ImageBackground
      style={{height:100,width:300}} 
      resizeMode='cover'
     >
         <Text style={{color:'red',fontSize:24}}> image 嵌入 text</Text>
</ImageBackground>

下面是具体的实现代码:

 /**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @providesModule ImageBackground
 * @flow
 * @format
 */
'use strict';

const Image = require('Image');
const React = require('React');
const StyleSheet = require('StyleSheet');
const View = require('View');

const ensureComponentIsNative = require('ensureComponentIsNative');

import type {NativeMethodsMixinType} from 'ReactNativeTypes';

/**
 * Very simple drop-in replacement for <Image> which supports nesting views.
 *
 * ```ReactNativeWebPlayer
 * import React, { Component } from 'react';
 * import { AppRegistry, View, ImageBackground, Text } from 'react-native';
 *
 * class DisplayAnImageBackground extends Component {
 *   render() {
 *     return (
 *       <ImageBackground
 *         style={{width: 50, height: 50}}
 *         source={{uri: 'https://facebook.github.io/react-native/img/opengraph.png'}}
 *       >
 *         <Text>React</Text>
 *       </ImageBackground>
 *     );
 *   }
 * }
 *
 * // App registration and rendering
 * AppRegistry.registerComponent('DisplayAnImageBackground', () => DisplayAnImageBackground);
 * ```
 */
class ImageBackground extends React.Component<$FlowFixMeProps> {
  setNativeProps(props: Object) {
    // Work-around flow
    const viewRef = this._viewRef;
    if (viewRef) {
      ensureComponentIsNative(viewRef);
      viewRef.setNativeProps(props);
    }
  }

  _viewRef: ?NativeMethodsMixinType = null;

  _captureRef = ref => {
    this._viewRef = ref;
  };

  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>
    );
  }
}

module.exports = ImageBackground;

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

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

2.实例操作

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

        <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属性,最终实现效果如下图所示:

![圆形图片效果图](https://img-blog.csdn.net/20180310145728443?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3V3dTE1MA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

suwu150

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值