我如何在React Native中将一个图像放在另一个图像上

Ben*_*Btg 1 react-native

我正在尝试在React Native中制作一个带有面部图像和以时钟为中心的指针的时钟组件。不允许嵌入其他图片。我发现了几篇文章,以使文字在图片上居中,但找不到以任何方式使图片在图片上居中的方法。

编辑:我试图找到一种避免给出绝对位置的方法,以便可以动态调整组件的大小。

Ayu*_*ani 6

如果我正确理解,您想显示一个图像而不是其他图像。一个作为父图像,另一个作为子图像,而不使用绝对位置。为此,您可以使用react-native提供的ImageBackground组件,并通过将其高度和宽度设置为百分比值来使用。

下面是示例:

父图像:Clock.png是一个ImageBackground组件

子图像:Needle.png是图像组件

import React, { Component } from 'react'
import { StyleSheet, View, Text, ImageBackground, Image } from 'react-native'

export default class ImageView extends Component {
  render() {
    return (
      <View
        style={{
          flex: 1
        }}
      >
        <View
          style={{
            flex: 0.25,
            alignItems: 'center',
            justifyContent: 'center',
            backgroundColor: 'red'
          }}
        >
          <Text style={{ color: 'white', fontSize: 26 }}>I am Header</Text>
        </View>
        <View
          style={{
            flex: 1,
            alignItems: 'center',
            justifyContent: 'center',
            // backgroundColor: 'blue',
            borderWidth: 1,
            borderColor: 'red'
          }}
        >
          <ImageBackground
            source={require('@assets/Clock.png')}
            style={{
              height: '60%',
              width: '100%'
            }}
            resizeMode="contain"
          >
            <Image
              style={{
                marginTop: '4.5%',
                alignSelf: 'center',
                height: '30%',
                width: '100%'
              }}
              resizeMode="contain"
              source={require('@assets/Needle.png')}
            />
          </ImageBackground>
        </View>
      </View>
    )
  }
}
Run Code Online (Sandbox Code Playgroud)

时钟