satish
satish

Reputation: 1374

Flutter- How to resize height and width of an image in custom Paint?

Here I am passing a list of the image to ImagePaint class to clip image in a circular, Its fine the images are clipping but only clipping some part of the image, If I could decrease this height and width of the image. Then it will fit the circle, Please comment if any have an idea.

void paint(Canvas canvas, Size size) {
int c = 0;
canvas.save();
canvas.translate(size.width / 2, size.height / 2);
canvas.rotate(-rotation);

for (var i = 0; i < 16; ++i) {
  if (i % 2 == 0) {
    canvas.drawLine(
      new Offset(0.0, 0.0),
      new Offset(0.0, size.width / 2 - 4.2),
      tickPaint,
    );
  } else {
    canvas.save();
    canvas.translate(-0.0, -((size.width) / 2));
    canvas.clipPath(path);
    if (images[c] != null) {
      ui.Image img = images[c];
      canvas.drawImage(img, Offset(0.0, 0.0), new Paint());
    }
    canvas.rotate(2 * pi);
    canvas.restore();
    c++;
  }
  canvas.rotate(2 * pi / 16);
}
canvas.restore();}

This image is printed in a circular manner: this image is printed in a circular manner

This is my app I am clipping image and printing in circular: This is my app I am clipping image and printing in circular

Upvotes: 6

Views: 8330

Answers (1)

DomingoMG
DomingoMG

Reputation: 1857

I just figured out how to reduce the image and send it to the painter for drawing.

The method that shrinks the image and what it returns is sent to the drawing class.

If it works for you, thank me for my reply with a thumbs up, thanks.

import 'dart:ui' as ui;
import 'package:image/image.dart' as IMG;

Future<ui.Image> loadImage( List<int> data ) async {
   final IMG.Image image = IMG.decodeImage(data);
   final IMG.Image resized = IMG.copyResize(image, width: 200);
   final List<int> resizedBytes = IMG.encodePng(resized);
   final Completer<ui.Image> completer = new Completer();

   ui.decodeImageFromList(resizedBytes, (ui.Image img) => completer.complete(img));
   return completer.future;
}

Drawing class.

class CustomDraw extends CustomPainter {
  final ui.Image image;

   const CustomDraw({this.image});

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = new Paint();
    canvas.drawImage(image, new Offset(40.0, 10.0), paint);
  }

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

Upvotes: 8

Related Questions