drawImage static method

dynamic drawImage(
  1. dynamic id,
  2. Canvas canvas,
  3. Paint paint,
  4. Image? image,
  5. Map imageSize,
  6. bool isCircle,
  7. dynamic borderColor,
  8. double x,
  9. double y,
  10. double? w,
  11. double? h,
)

Implementation

static drawImage(id,Canvas canvas, Paint paint, ui.Image? image,Map imageSize,bool isCircle,dynamic borderColor, double x, double y,
    double? w, double? h) {
  if (image != null) {
    double imw = image.width.toDouble();
    double imh = image.height.toDouble();
    double iw = (w != null && w>0)?w:imw;
    double ih = (h != null && h>0)?h:imh;
    double ix = x;
    double iy = y;
    if(imageSize[id] == null){
      //坐标还没有计算精准,因为计算时图片还没有加载完成
      imageSize[id] = {'x':ix,'y':iy,'w':iw,'h':ih};
    }else{
      //图片已加载完成,坐标已经准计算,可以绘制图形
      imageSize[id] = {'x':ix,'y':iy,'w':iw,'h':ih};
      if(isCircle){
        canvas.saveLayer(Rect.largest, Paint());
        canvas.clipPath(Path()..addOval(Rect.fromCircle(center: Offset((ix+iw/2), (iy+ih/2)), radius: (iw/2))));
        canvas.drawImageRect(
          image,
          Rect.fromLTWH(0.0, 0.0, imw, imh),
          Rect.fromLTWH(ix, iy, iw, ih),
          paint);
        if(borderColor != null){
          paint.color = borderColor;
          canvas.drawCircle(Offset((ix+iw/2), (iy+ih/2)) , (iw/2), paint);
        }
        canvas.restore();
      }else{
        canvas.drawImageRect(
          image,
          Rect.fromLTWH(0.0, 0.0, imw, imh),
          Rect.fromLTWH(ix, iy, iw, ih),
          paint);
        if(borderColor != null){
          paint.color = borderColor;
          canvas.drawRRect(RRect.fromRectAndCorners(
            Rect.fromLTWH(ix, iy, iw, ih),
            topLeft: Radius.circular(2),
            topRight: Radius.circular(2),
            bottomRight: Radius.circular(2),
            bottomLeft: Radius.circular(2),
          ), paint);
        }
      }
    }
  }
}