Container(
width: 400,height: 700,child: GestureDetector(
onLongPressStart: (LongPressStartDetails details) {
print('${details.localPosition},');
},onLongPressMoveUpdate: (LongPressMoveUpdateDetails details) {
print('${details.localPosition},onLongPressEnd: (LongPressEndDetails details) {
print('${details.localPosition},child: Image.asset(
'images/123.png',fit: BoxFit.fill,),)
- 图片显示的大小最好和最终的画布一样大小,这样得到的坐标不需要在转换。
- 图片的宽高比和画布的宽高比要一样。
static final List<Offset> flowerPoints = [
Offset(182.0,136.3),Offset(182.7,135.3),Offset(183.0,Offset(183.3,...
)
@override
void paint(Canvas canvas,Size size) {
//将花变为红色
if (flowerPaths.length >= RoseData.flowerPoints.length) {
var path = Path();
for (int i = 0; i < flowerPaths.length; i++) {
if (i == 0) {
path.moveTo(flowerPaths[i].dx,flowerPaths[i].dy);
} else {
path.lineTo(flowerPaths[i].dx,flowerPaths[i].dy);
}
}
_paint.style = PaintingStyle.fill;
_paint.color = _flowerColor;
canvas.drawPath(path,_paint);
}
//绘制线
_paint.style = PaintingStyle.stroke;
_paint.color = _strokeColor;
canvas.drawPoints(PointMode.polygon,flowerPaths,_paint);
}
AnimationController _controller;
Animation<num> _animation;
@override
void initState() {
_controller = AnimationController(
duration: Duration(seconds: 10),vsync: this)
..addListener(() {
setState(() {
_flowerPaths = RoseData.flowerPoints.sublist(0,_animation.value.floor());
});
});
_animation = Tween(
begin: 0.0,end: RoseData.flowerPoints.length)
.animate(_controller);
}
@override
Widget build(BuildContext context) {
Container(
width: 400,child: CustomPaint(
painter: RosePaint(_flowerPaths),)
}
static final List<Offset> flowerPoints = [
Offset(182.0,...
Offset(179.3,301.7),Offset(237.7,144.7),];
if (flowerPaths.length >= RoseData.flowerPoints.length) {
var path = Path();
for (int i = 0; i < flowerPaths.length; i++) {
if (i == 0) {
path.moveTo(flowerPaths[i].dx,flowerPaths[i].dy);
} else {
path.lineTo(flowerPaths[i].dx,flowerPaths[i].dy);
}
}
_paint.style = PaintingStyle.fill;
_paint.color = _flowerColor;
canvas.drawPath(path,_paint);
}
//绘制线
_paint.style = PaintingStyle.stroke;
_paint.color = _strokeColor;
//去掉最后2个点,最后2个点为了绘制红色
var points = flowerPaths.sublist(0,max(0,flowerPaths.length - 2));
canvas.drawPoints(PointMode.polygon,points,_paint);