【问题标题】:How i can randomise my background image React Native我如何随机化我的背景图像 React Native
【发布时间】:2021-04-05 05:53:14
【问题描述】:

我想随机返回文件夹中的图像,但出现错误“无效调用”

我的功能:

  returnGif = () => {
    min = Math.ceil(1);
    max = Math.floor(53);
    const gifNumber = Math.floor(Math.random() * (max - min +1)) + min;
    var gifFont = require('../config/Images/Gifs/' + gifNumber + '_2.png');
    if (gifFont) {
        if (Math.random()>0.5) gifFont = require('../config/Images/Gifs/' + gifNumber + '_2.png');
        else gifFont = require('../config/Images/Gifs/' + gifNumber + '.png');
    }
    else gifFont = require('../config/Images/Gifs/' + gifNumber + '.png');
    return gifFont;
  }

【问题讨论】:

    标签: react-native imagebackground


    【解决方案1】:

    在 react native 中,目前不支持 require 中的动态路径。

    您需要导入所有图像,然后用例进行随机化

    例如

     function getavatarImage(avatar) {
      switch (avatar) {
       case "spiderman":
        return require('./spiderman.png');
       case "batman":
        return require('./batman.png');
       case "hulk":
        return require('./hulk.png');
       default:
        return require('./no-image.png');
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      相关资源
      最近更新 更多