gpt4 book ai didi

flutter - 将图像切成相等的部分-还是仅调用图像的一部分? flutter - Dart

转载 作者:行者123 更新时间:2023-12-03 04:01:03 26 4
gpt4 key购买 nike

我需要将图片的一部分分配给竞争者示例-益智游戏示例。使用 flutter

最佳答案

看这是一些如何执行此操作的代码:

import 'dart:math';

import 'package:flutter/material.dart';

class PuzzlePiece extends StatefulWidget {
final Image image;
final Size imageSize;
final int row;
final int col;
final int maxRow;
final int maxCol;

PuzzlePiece(
{Key key,
@required this.image,
@required this.imageSize,
@required this.row,
@required this.col,
@required this.maxRow,
@required this.maxCol})
: super(key: key);

@override
PuzzlePieceState createState() {
return new PuzzlePieceState();
}
}

class PuzzlePieceState extends State<PuzzlePiece> {
double top;
double left;

@override
Widget build(BuildContext context) {
final imageWidth = MediaQuery.of(context).size.width;
final imageHeight = MediaQuery.of(context).size.height * MediaQuery.of(context).size.width / widget.imageSize.width;
final pieceWidth = imageWidth / widget.maxCol;
final pieceHeight = imageHeight / widget.maxRow;

if (top == null) {
top = Random().nextInt((imageHeight - pieceHeight).ceil()).toDouble();
top -= widget.row * pieceHeight;
}
if (left == null) {
left = Random().nextInt((imageWidth - pieceWidth).ceil()).toDouble();
left -= widget.col * pieceWidth;
}

return Positioned(
top: top,
left: left,
width: imageWidth,
child: ClipPath(
child: CustomPaint(
foregroundPainter: PuzzlePiecePainter(widget.row, widget.col, widget.maxRow, widget.maxCol),
child: widget.image
),
clipper: PuzzlePieceClipper(widget.row, widget.col, widget.maxRow, widget.maxCol),
),
);
}
}

// this class is used to clip the image to the puzzle piece path
class PuzzlePieceClipper extends CustomClipper<Path> {
final int row;
final int col;
final int maxRow;
final int maxCol;

PuzzlePieceClipper(this.row, this.col, this.maxRow, this.maxCol);

@override
Path getClip(Size size) {
return getPiecePath(size, row, col, maxRow, maxCol);
}

@override
bool shouldReclip(CustomClipper<Path> oldClipper) => false;
}

// this class is used to draw a border around the clipped image
class PuzzlePiecePainter extends CustomPainter {
final int row;
final int col;
final int maxRow;
final int maxCol;

PuzzlePiecePainter(this.row, this.col, this.maxRow, this.maxCol);

@override
void paint(Canvas canvas, Size size) {
final Paint paint = Paint()
..color = Color(0x80FFFFFF)
..style = PaintingStyle.stroke
..strokeWidth = 1.0;

canvas.drawPath(getPiecePath(size, row, col, maxRow, maxCol), paint);
}

@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}

// this is the path used to clip the image and, then, to draw a border around it; here we actually draw the puzzle piece
Path getPiecePath(Size size, int row, int col, int maxRow, int maxCol) {
final width = size.width / maxCol;
final height = size.height / maxRow;
final offsetX = col * width;
final offsetY = row * height;
final bumpSize = height / 4;

var path = Path();
path.moveTo(offsetX, offsetY);

if (row == 0) {
// top side piece
path.lineTo(offsetX + width, offsetY);
} else {
// top bump
path.lineTo(offsetX + width / 3, offsetY);
path.cubicTo(offsetX + width / 6, offsetY - bumpSize, offsetX + width / 6 * 5, offsetY - bumpSize, offsetX + width / 3 * 2, offsetY);
path.lineTo(offsetX + width, offsetY);
}

if (col == maxCol - 1) {
// right side piece
path.lineTo(offsetX + width, offsetY + height);
} else {
// right bump
path.lineTo(offsetX + width, offsetY + height / 3);
path.cubicTo(offsetX + width - bumpSize, offsetY + height / 6, offsetX + width - bumpSize, offsetY + height / 6 * 5, offsetX + width, offsetY + height / 3 * 2);
path.lineTo(offsetX + width, offsetY + height);
}

if (row == maxRow - 1) {
// bottom side piece
path.lineTo(offsetX, offsetY + height);
} else {
// bottom bump
path.lineTo(offsetX + width / 3 * 2, offsetY + height);
path.cubicTo(offsetX + width / 6 * 5, offsetY + height - bumpSize, offsetX + width / 6, offsetY + height - bumpSize, offsetX + width / 3, offsetY + height);
path.lineTo(offsetX, offsetY + height);
}

if (col == 0) {
// left side piece
path.close();
} else {
// left bump
path.lineTo(offsetX, offsetY + height / 3 * 2);
path.cubicTo(offsetX - bumpSize, offsetY + height / 6 * 5, offsetX - bumpSize, offsetY + height / 6, offsetX, offsetY + height / 3);
path.close();
}

return path;
}

我找到了这段代码 Here,但是我选择的那部分是您发送的那一部分,然后对它进行图像处理,现在,它的重要性可以知道它是如何被用来使其成为新的Stateful Class来接收Image和您需要的其他一些参数的看起来可以自定义您需要的东西,例如行数和列数,最小y最大和图像本身,用新类完成的方式扩展了CustomClipper并实际击中了创建裁切计算的零件以及行和列的数量以及接收到的图像的宽度和高度。

现在,他像 path.cubicTo(offsetX + width - bumpSize, offsetY + height / 6, offsetX + width - bumpSize, offsetY + height / 6 * 5, offsetX + width, offsetY + height / 3 * 2);这样使用它,但是它可以创建拼图凹凸形式,因此,如果您不需要它,可以忽略它。

您可以稍微试用一下该代码,看看它会如何适合您的需求。

关于flutter - 将图像切成相等的部分-还是仅调用图像的一部分? flutter - Dart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56715839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com