我如何随机化我的背景图像 React Native
How i can randomise my background image React Native
我想随机化文件夹中图像的 return,但出现错误 'Invalid call'
我的函数:
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中,目前不支持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');
}
}
我想随机化文件夹中图像的 return,但出现错误 'Invalid call'
我的函数:
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中,目前不支持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');
}
}