【问题标题】:How to draw rectangles in an image in React native如何在 React Native 中的图像中绘制矩形
【发布时间】:2019-09-20 11:56:33
【问题描述】:

一段时间以来,我一直在寻找有关如何使用 react native 在图像中绘制矩形的参考资料,但我一无所获。

我尝试做的是将函数、照片和矩形对角线顶点坐标作为参数传递,然后返回带有该绘制矩形的图像。我怎么能这样做?

Example

【问题讨论】:

    标签: javascript image react-native


    【解决方案1】:

    想得简单.. 就像用背景颜色创建 div。 这是一个例子:

    import * as React from 'react';
    import { Text, View, StyleSheet, Image } from 'react-native';
    import { Constants } from 'expo';
    
    export default class App extends React.Component {
      render() {
        return (
          <View style={styles.container}>
            <View style={styles.rectangle}></View>
            <Image source={require('assets/snack-icon.png')} />
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: 'center',
        paddingTop: Constants.statusBarHeight,
        backgroundColor: '#ecf0f1',
        padding: 8,
        position: 'relative',
      },
      rectangle: {
        height: 128,
        width: 128,
        backgroundColor: 'salmon',
        position: 'absolute', 
        zIndex: 99,
        top: '50%',
        left: '40%'
      },
    
    });
    
    

    结果:

    【讨论】:

      【解决方案2】:

      创建一个以图像和视图(矩形框)为子项的视图。通过将绝对位置设置为矩形将矩形框放置在图像上。要定位,您必须为顶部、底部、左侧和右侧设置矩形样式值。I已经创建了一个函数来传递位置值。

      检查这个例子:

      import * as React from "react";
      import { Text, View, StyleSheet, Image } from "react-native";
      
      export default class App extends React.Component {
        renderImage = (topPosition, bottomPosition, leftPosition, rightPosition) => {
          return (
            <View style={styles.imageContainer}>
              <Image
                source={{
                  uri:
                    "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRzwZW9gvrxF2McRF-wP5TxCIBU_3fA2XDl9DESsm1uqowjSvZ1"
                }}
                style={styles.image}
                resizeMode="stretch"
              />
              <View
                style={[
                  styles.rectangle,
                  {
                    top: topPosition,
                    bottom: bottomPosition,
                    left: leftPosition,
                    right: rightPosition
                  }
                ]}
              />
            </View>
          );
        };
      
        render() {
          return (
            <View style={styles.container}>{this.renderImage(80, 55, 30, 70)}</View>
          );
        }
      }
      
      const styles = StyleSheet.create({
        container: {
          flex: 1,
          justifyContent: "center",
          backgroundColor: "#ecf0f1",
          padding: 8
        },
        imageContainer: {
          width: 300,
          height: 250,
          alignSelf: "center"
        },
        image: {
          width: 300,
          height: 250
        },
        rectangle: {
          borderWidth: 3,
          borderColor: "red",
          position: "absolute"
        }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-05
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多