我有一个包含多个较小图像的大图像(spritesheet)
如何显示特定的“sprite”?
我已经找到了一些答案,但它们都与游戏有关,而我的应用程序不是游戏。
这是我目前为止得到的结果,但这会引发一个错误
Failed assertion: line 535 pos 12: 'sourceSize == inputSize': centerSlice was used
with a BoxFit that does not guarantee that the image is fully visible
我所要做的事情的全部要点是,图像不是完全可见的,而是只有一小部分。
class SpriteImage extends StatelessWidget {
final String assetName;
final int index;
final int rowCount;
final int columnCount;
final double width;
final double height;
SpriteImage({
required this.assetName,
required this.index,
required this.rowCount,
required this.columnCount,
required this.width,
required this.height,
});
@override
Widget build(BuildContext context) {
int rowIndex = index ~/ columnCount;
int columnIndex = index % columnCount;
double srcX = columnIndex * width;
double srcY = rowIndex * height;
return Image.asset(
assetName,
width: width,
height: height,
fit: BoxFit.fill,
alignment: Alignment.topLeft,
// Clip the image to the desired sprite.
repeat: ImageRepeat.noRepeat,
scale: 1.0,
// Define the source rectangle to crop the sprite from the sprite sheet.
centerSlice: Rect.fromLTRB(srcX + 1, srcY + 1, srcX + width - 1, srcY + height - 1),
);
}
}
1条答案
按热度按时间lskq00tm1#
多亏了pskink上面的回答,我才能够创建一个解决方案: