【问题标题】:How can I repeat a pattern image to create a background in React Native?如何在 React Native 中重复图案图像以创建背景?
【发布时间】:2016-11-01 14:59:07
【问题描述】:

我正在为 iOS 构建一个简单的 ReactNative 应用程序,并且我正在尝试添加背景图像。似乎没有 backgroundImage 标记,虽然我确实设法在页面上显示了一次图像,但我不能像使用 CSS 那样在整个页面中重复它。有什么建议吗?

【问题讨论】:

    标签: ios reactjs react-native


    【解决方案1】:

    iOS

    iOS 上的图像现在包含 repeat resizeMode property

    像这样使用它:

    <Image
        src={/* whatever your source is */}
        resizeMode="repeat"
    />
    

    安卓

    2018 年 6 月更新:

    从 react-native 0.56 Android 图片开始也支持 repeat 属性。 (https://github.com/facebook/react-native/commit/0459e4ffaadb161598ce1a5b14c08d49a9257c9c)

    2018 年 6 月之前:

    Android 上,repeat 属性不起作用,因此: 你必须使用类似Shiraman's answer

    外部库:

    现在有一个名为react-native-bgimage(由Alex Melanchenko 创建)的伟大项目运行良好:

    我是这样使用它的:

    import RepeatingPattern from 'react-native-bgimage';
    
    <RepeatingPattern
        drawable="block_pattern"
        style={{
          height: 45,
        }}
    />
    

    然后我在android/app/src/main/res/drawable/block_pattern.png中添加一个模式png文件

    【讨论】:

    • 是的,它在答案中说明了这一点 - 也是专门针对 iOS 提出问题的人。话虽如此,在 Android 上,您必须执行与 Sriraman 建议类似的操作。
    • 你应该包含一个代码示例,它不包含在文档中,人们可能需要相当长的时间来实现它。
    • 你在说什么@thibautnoah 它包含在我发布的链接的文档中。
    • @SudoPlz facebook 文档中没有任何示例
    • 我很困惑@thibautnoah 你在找什么样的样品?你这样使用它:&lt;Image source={{uri: 'http://xxx.png'}} resizeMode="repeat" /&gt; 这是你要找的吗?
    【解决方案2】:

    尽管这个问题已经很老了,但我想把我的两分钱放进去。也可以通过&lt;ImageBackground&gt; 组件 (reference) 来完成。

    例子:

    <ImageBackground source={require('./path/to/image.png')} style={{width: '100%', height: '100%'}} imageStyle={{resizeMode: 'repeat'}}>
      // Your inside content goes here
    </ImageBackground>
    

    不要忘记在文件开头导入组件,例如import { ImageBackground } from 'react-native';

    【讨论】:

    • 这里需要注意的是,我们不能在当前版本的 RN (v0.62.2) 中使用 .svg 图像
    • 我们可以使用svg文件! @GaleelBhasha 使用 react native svg
    【解决方案3】:

    我想扩展 Sriraman 的答案。要将重复的图像作为背景,您需要采取一个额外的步骤,即添加一个新视图并使其位置绝对和背景透明,然后在其中添加所有其他组件。

    var React = require('react-native');
    var Dimensions = require('Dimensions');
    
    var {
      Image
    } = React;
    
    var RepeatImage = React.createClass({
        render: function(){
        var images = [],  
        imgWidth = 7,
        winWidth =Dimensions.get('window').width;
    
        for(var i=0;i<Math.ceil(winWidth / imgWidth);i++){
            images.push((
               <Image source={{uri: 'http://xxx.png'}} style={} />
            ))
        }
    
        return (
            <View style={{flex:1,flexDirection:'row'}}>
            {
             images.map(function(img,i){
             return img;
             })
            }
              <View style={{position: 'absolute', top: 0, bottom: 0, left: 0, right: 0}}>
                  {/*Place all you compoents inside this view*/}
              </View>
            </View>
        )
      }
    });

    【讨论】:

    • 感谢您的回答!如果我不知道这个背景的视图尺寸怎么办?
    • ^ 这个,它只能水平而不是垂直工作。我想我们不能两者兼得。
    • @Aakash 我们到底在做什么,绝对位置 top , bottom , left , right 0 ??
    【解决方案4】:

    你不能像 React Native 中的 CSS 那样重复背景图片。但是,您可以通过像这样迭代图像来实现它

    var React = require('react-native');
    var Dimensions = require('Dimensions');
    
    var {
      Image
    } = React;
    
    var RepeatImage = React.createClass({
        render: function(){
        var images = [],  
        imgWidth = 7,
        winWidth =Dimensions.get('window').width;
    
        for(var i=0;i<Math.ceil(winWidth / imgWidth);i++){
            images.push((
               <Image source={{uri: 'http://xxx.png'}} style={} />
            ))
        }
    
        return (
            <View style={{flex:1,flexDirection:'row'}}>
            {
             images.map(function(img,i){
             return img;
             })
            }
            </View>
        )
      }
    });
    

    【讨论】:

    • 这样可行的内存明智吗?
    • 其实可以的
    • 但这只会显示单行或重复的图像吗?这看起来只是一个部分解决方案,它与 iO 上的repeat 模式并不完全相同。
    • 不好的解决方案不要使用这个
    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-02
    • 2015-11-11
    • 1970-01-01
    相关资源
    最近更新 更多