问题
I did this code to get an image from firestore and use it as an icon for a Map Marker.
final StorageReference storageReference =
FirebaseStorage().ref().child("ProfilePictures/" + widget.userId);
String avatarDownloadPath = await storageReference.getDownloadURL();
final File _avatar = await DefaultCacheManager().getSingleFile(avatarDownloadPath);
Uint8List __avatar = await _avatar.readAsBytes();
BitmapDescriptor avatar = BitmapDescriptor.fromBytes(__avatar);
setState(() {
_markers.add(Marker(markerId: MarkerId("UserPosition"), position: userLocation, icon: avatar ));
});
This code works but I want to set the image a round shape and I don't know how to do this...
If you also know how to add a circle around and animate it like this I will be really satisfied:
(I didn't find something more representative but I just want one circle)
回答1:
You can use use Canvas to draw a round image and use PictureRecorder to save as a final image and put it in the marker bitmapdescriptor, fyi: custom marker
Replace TextPainter with ImagePainter in the article
OR
Follow this article by using widget, custom marker
回答2:
After some coding, research and reflexion here my code:
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
Future<BitmapDescriptor> convertImageFileToBitmapDescriptor(File imageFile,
{int size = 150,
bool addBorder = false,
Color borderColor = Colors.white,
double borderSize = 10,
String title,
Color titleColor = Colors.white,
Color titleBackgroundColor = Colors.black}) async {
final ui.PictureRecorder pictureRecorder = ui.PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
final Paint paint = Paint()..color;
final TextPainter textPainter = TextPainter(
textDirection: TextDirection.ltr,
);
final double radius = size / 2;
//make canvas clip path to prevent image drawing over the circle
final Path clipPath = Path();
clipPath.addRRect(RRect.fromRectAndRadius(
Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
Radius.circular(100)));
clipPath.addRRect(RRect.fromRectAndRadius(
Rect.fromLTWH(0, size * 8 / 10, size.toDouble(), size * 3 / 10),
Radius.circular(100)));
canvas.clipPath(clipPath);
//paintImage
final Uint8List imageUint8List = await imageFile.readAsBytes();
final ui.Codec codec = await ui.instantiateImageCodec(imageUint8List);
final ui.FrameInfo imageFI = await codec.getNextFrame();
paintImage(
canvas: canvas,
rect: Rect.fromLTWH(0, 0, size.toDouble(), size.toDouble()),
image: imageFI.image);
if (addBorder) {
//draw Border
paint..color = borderColor;
paint..style = PaintingStyle.stroke;
paint..strokeWidth = borderSize;
canvas.drawCircle(Offset(radius, radius), radius, paint);
}
if (title != null) {
if (title.length > 9) {
title = title.substring(0, 9);
}
//draw Title background
paint..color = titleBackgroundColor;
paint..style = PaintingStyle.fill;
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(0, size * 8 / 10, size.toDouble(), size * 3 / 10),
Radius.circular(100)),
paint);
//draw Title
textPainter.text = TextSpan(
text: title,
style: TextStyle(
fontSize: radius / 2.5,
fontWeight: FontWeight.bold,
color: titleColor,
));
textPainter.layout();
textPainter.paint(
canvas,
Offset(radius - textPainter.width / 2,
size * 9.5 / 10 - textPainter.height / 2));
}
//convert canvas as PNG bytes
final _image =
await pictureRecorder.endRecording().toImage(size, (size * 1.1).toInt());
final data = await _image.toByteData(format: ui.ImageByteFormat.png);
//convert PNG bytes as BitmapDescriptor
return BitmapDescriptor.fromBytes(data.buffer.asUint8List());
}
and here the result:
thanks @Jim Chiu for helping :)
回答3:
Do you want a round border on an image that is not round? You can use box decoration and set the decoration to round image.
https://medium.com/@boldijar.paul/circle-image-view-in-flutter-965963c46cf5
回答4:
Have you tried this?
Material(
elevation: 4.0,
shape: CircleBorder(),
clipBehavior: Clip.hardEdge,
color: Colors.transparent,
child: Ink.image(
image: AssetImage('assets/profile_default.jpg'),
fit: BoxFit.cover,
width: 120.0,
height: 120.0,
child: InkWell(
onTap: () {},
),
),
)
回答5:
The easiest way to round an image is with a ClipRRect
To implement a perfectly rounded image, you can wrap your image like this:
double size = 100;
return ClipRRect(
borderRadius: BorderRadius.circular(size / 2),
child: Image.file(/*YOUR CHILD */, height: size, width: size,),
);
来源:https://stackoverflow.com/questions/61174307/round-image-file