【问题标题】:React Native Use Percentage for Square Image方形图像的 React Native 使用百分比
【发布时间】:2021-06-21 20:37:42
【问题描述】:

我正在使用本机反应,并试图创建一个固定百分比的屏幕宽度的图像。但是,此图像也必须是正方形。通常,在创建方形图像时,我会以图像的样式来写:

image: {
    width: 200,
    height: 200,
  },

这将创建一个 200 x 200 的图像。我尝试使用这样的百分比:

image: {
    width: 50%,
    height: 50%,
},

但是,这显然会根据宽度和高度缩放图像。如何使图像成为 x by x 图像,其中 x 是屏幕宽度的百分比。例如,如果屏幕是 400px 宽,我想要一个 200 x 200 的图像。

【问题讨论】:

    标签: react-native


    【解决方案1】:

    你可以使用Dimensions来计算图片的大小

    const _width  = Dimensions.get('screen').width * 0.5
    

    然后使用

    image: {
        width: _width,
        height: _width,
      },
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2013-05-17
      • 2017-08-27
      • 1970-01-01
      • 2019-12-20
      相关资源
      最近更新 更多