Add border to Xamarin Pin Image

kotetotemkotetotem Member ✭✭

Hey guys, so I have menaged to add image as a custom pin. I am just wondering is there a way to make image "round" in code somehow or they are put there as they are in Drawable folder?
And is there a way to add bordrer to pin image somehow?

thx! :-)

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    a wayto make image "round" in code

    To display a rounded image in Xamarin forms, try the two tips:

    1.Create Image render to custom Image:
    https://devblogs.microsoft.com/xamarin/elegant-circle-images-in-xamarin-forms/

    2.Use xam.plugins.forms.ImageCircle plugin
    https://www.c-sharpcorner.com/article/round-image-in-xamarin-forms/

  • kotetotemkotetotem Member ✭✭

    @yelinzh said:

    a wayto make image "round" in code

    To display a rounded image in Xamarin forms, try the two tips:

    1.Create Image render to custom Image:
    https://devblogs.microsoft.com/xamarin/elegant-circle-images-in-xamarin-forms/

    2.Use xam.plugins.forms.ImageCircle plugin
    https://www.c-sharpcorner.com/article/round-image-in-xamarin-forms/

    Thx, gonna check that out when I come home! :-)

    It is doable for map pins also?

    I can squish it somewhere in here:

    var marker = new MarkerOptions();
    marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
    marker.SetTitle(pin.Label);
    marker.SetSnippet(pin.Address);
    marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
    return marker;
    

    Thx!

  • kotetotemkotetotem Member ✭✭

    Is there a way to edit a Pin Image in code?

    Or I have to make them by myself and just call a path to them, because there is no way to do it in code Image class or something like that?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    You need to look into messing with bitmaps in native Android, not really anything to do with the Xamarin.Forms.Image class.

  • kotetotemkotetotem Member ✭✭

    @JoeManke said:
    You need to look into messing with bitmaps in native Android, not really anything to do with the Xamarin.Forms.Image class.

    So is there a way to do it, or just go and make myself images that are round and import them like that?

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited July 23

    That would be the easiest way, but I'm sure you could modify the code from the link @yelinzh posted to work.

    You would start with something like this:

    var bitmap = BitmapFactory.FromResource(Resource.Drawable.pin);
    bitmap = bitmap.Copy(bitmap.GetConfig(), true);
    
    // modifications here
    
    marker.SetIcon(BitmapDescriptorFactory.FromBitmap(bitmap));
    
  • kotetotemkotetotem Member ✭✭

    @JoeManke said:
    That would be the easiest way, but I'm sure you could modify the code from the link @yelinzh posted to work.

    You would start with something like this:

    var bitmap = BitmapFactory.FromResource(Resource.Drawable.pin);
    bitmap = bitmap.Copy(bitmap.GetConfig(), true);
    
    // modifications here
    
    marker.SetIcon(BitmapDescriptorFactory.FromBitmap(bitmap));
    

    Hey, yes, something like that. But is it possbible to do renders of a Image and a Pin in same class (Which I am using for rendering Pins) ?

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    You can get a Bitmap from an ImageSource like this:

    var handler = Xamarin.Forms.Internals.Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(pin.Image);
    if (handler != null)
    {
        var bitmap = await handler.LoadImageAsync(pin.Image, Context);
        if (bitmap != null)
        {
            // do modifications here
        }
    }
    
Sign In or Register to comment.