【问题标题】:React Native ImageBackground Orientation ChangeReact Native ImageBackground 方向更改
【发布时间】:2021-01-31 11:06:08
【问题描述】:

即使我可以更改屏幕方向以更新状态并重新渲染,ImageBackground 组件仍然不会更新其宽度。

我有以下代码:

<View
  onLayout={event => this.mylayoutchange(event)}
  style={{ flex: 1, backgroundColor: 'green' }}
>
  <ImageBackground
    style={{ flex: 1, width: this.state.width, height: this.state.height }}
    imageStyle={{ resizeMode: 'repeat' }}
    source={require('../assets/images/my_background.jpg')}
  >
    <View>
      <Text>.... Other code here....</Text>
    </View>
  </ImageBackground>
</View>;

当用户改变设备的方向时,会调用 mylayoutchange() 函数。它正确更新状态。渲染功能将更新。如console.log() 所示,宽度和高度已正确更改。但是,无论出于何种原因,&lt;ImageBackground&gt; 都不会更新其正确的宽度和高度。

当屏幕旋转时,背景图像不再填满屏幕的整个大小,而是看到绿色的背景颜色。

我做错了什么?

我的环境:

"react": "16.13.1",
"react-native": "0.63.2",

【问题讨论】:

  • 如果您可以,请在 Snack 上分享您的问题的重新制作。乖乖,我们可以提供更好的帮助。

标签: react-native screen-orientation imagebackground


【解决方案1】:

您需要将resize 用于resizeMethod 以使图像得到实际调整大小:

resizeMethod

当图像的大小调整时应该使用的机制 尺寸与图像视图的尺寸不同。默认为 auto.

  • auto:使用启发式方法在resizescale 之间进行选择。

  • resize:一种软件操作,可在解码之前更改内存中的编码图像。这应该用来代替scale 当图像比视图大得多时。

  • scale: 图像被缩小或放大。与resize 相比,scale 更快(通常是硬件加速)并且 产生更高质量的图像。如果图像是,这应该使用 比视图小。如果图像是,也应该使用它 比视图略大。

很明显,RN 在您的情况下选择了scale,因此您必须将其显式设置为resize,以便它在方向更改和 flex 样式更改开始时起作用:

return (
  <View
    style={{ flex: 1, backgroundColor: 'blue' }}
  >
    <ImageBackground
      // All props other than children, style, imageStyle, imageRef,
      // will be passed to the inner Image component,
      // so, we can use resizeMethod that will be passed to the Image component

      resizeMethod="resize"

      style={{
        flex: 1,
        borderWidth: 1,
        borderColor: 'red'
      }}
      imageStyle={{
        resizeMode: 'repeat',
      }}
      source={require('../assets/images/my_background.jpg')}
    >
      <View>
        <Text style={{
          backgroundColor: 'white'
        }}>.... Other code here....</Text>
      </View>
      <View style={{
        position: 'absolute',
        right: 0,
        bottom: 0
      }}>
        <Text style={{
          backgroundColor: 'white'
        }}>Absolute bottom-right text</Text>
      </View>
    </ImageBackground>
  </View>
);

结果截屏:

环境测试:

"react": "16.13.1",
"react-native": "0.63.2",

用于重复背景的示例平铺图像(400 X 400 像素):

世博小吃:

RN ImageBackground Orientation Change

【讨论】:

  • 嗯,它确实有效。奇怪的是,如果图像小于视图,则应该使用scale。我的图像比视图小,所以我应该使用 resize 来代替直觉。
  • @kojow7 您正在尝试在现有代码中使用此属性,该代码使用widthheightonLayout 内部计算,并且很可能您正在使用扩展语法来应用道具@ ImageBackground 样式中的 987654348@ 哪一个不起作用,因为 Dimentions 辅助类对象还返回 fontScalescale 以及 widthheight (屏幕截图:prnt.sc/v8ivzq) @98765435 @ 和 scale 不是有效的样式属性。请注意我的代码,因为我根本不使用onLayout
  • @kojow7 你说它是 "counterintuitive" 但 RN 作为任何软件都不是完美的,这就是你在这里试图解决这个问题的原因。事实是,如果图像没有resizeresizeMethod 以使其具有正确的resizeMode: "repeat" 绘图,则图像将不会呈现在原始肖像尺寸之上。祝你好运找到更好的解决方案。
  • @ChristosLytras 我也尝试过使用宽度:'100% 和高度:'100%',但仍然遇到同样的问题。我也觉得你误会了我。通过“违反直觉”,我是说文档似乎与我们实际看到的内容没有意义。我并没有说我试图找到更好的解决方案。您的解决方案工作得很好,所以这就是我正在使用的。
  • 您的出色回答让我大吃一惊。像往常一样。这才是重点。 ?
【解决方案2】:

您可以仅使用 CSS 自动指定宽度和高度,这将确保您的视图采用每个尺寸/布局的完整宽度/高度。

     <View
        onLayout={(event) => this.mylayoutchange(event)}
        style={{flex: 1, backgroundColor: 'green'}}>
        <ImageBackground
          style={{
            flex: 1,
            width: '100%',
            height: '100%',
          }}
          imageStyle={{resizeMode: 'repeat'}}
          source={require('../assets/images/my_background.jpg')}>
          <View>
            <Text>.... Other code here....</Text>
          </View>
        </ImageBackground>
      </View>

注意事项:

  • React Native 中的一个错误可能仍然存在于您的 RN 版本中,您可以查看 issue
  • 如果您仍需要使用this.state.widththis.state.height 指定宽度和高度,请考虑使用usewindowdimensions,因为它会自动更新,因此更合适。

【讨论】:

  • 我原本是 100% 的,但在旋转时它仍然给我同样的问题。这就是为什么我切换到基于状态的选项,因为我认为重新渲染会更新它。还是不行。
  • @kojow7 这个答案中提到的错误绝对是您遇到问题的原因,因为它已关闭,您可以在 github 中打开一个新问题,以便再次引起注意
【解决方案3】:

我刚刚遇到了类似的问题。我发现的一种解决方法是在每次方向更改时生成一个唯一 ID (UUID),并将该 UUID 用作 ImageBackground 的键。这将重新挂载映像并解决该错误。

【讨论】:

  • 这似乎对我不起作用。不过谢谢。
猜你喜欢
  • 2018-08-30
  • 2019-10-14
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2022-12-24
  • 2021-05-11
相关资源
最近更新 更多