Circle Image In Xamarin.Forms

Introduction

Circular images in apps are very popular in app development these days. For most of the applications we need to make the user profile a circular image. So in this article, we can learn how to make an image circle using Xam.Plugins.Forms.ImageCircle plugin.

If you want to make an Image Circle without a plugin follow my previous article.

Requirements

  • This article's source code is prepared by using Visual Studio. And it is better to install the latest Visual Studio updates from here.
  • This article is prepared on a MAC machine.
  • This sample project is Xamarin.Forms PCL project.
  • This sample app is targeted for Android, iOS. And tested for Android & iOS.

Description

The creation of a Xamarin.Forms project is very simple in Visual Studio for Mac. It will create three projects:

  1. Shared Code
  2. Xamarin.Android
  3. Xamarin.iOS

The Mac system with Visual Studio for Mac  doesn't support Windows projects (UWP, Windows, Windows Phone).

The following steps will show you how to create Xamarin.Forms projects in a Mac system with Visual Studio.

First, open Visual Studio for Mac and click on New Project.

Xamarin

After that, we need to select whether you're doing Xamarin.Forms or Xamarin.Android or Xamarin.iOS project. If we want to create Xamarin.Forms project just follow the below screenshot.

Xamarin

Then, we have to give the app a name; i.e., CircleImageDemo.

Xamarin

Note

In the above screen under Shared Code, select Portable class Library or Use Shared Library.

Then, click on Next Button and the following screenshot will be displayed. In that screen, we have to browse the file path where we want to save that application on our PC.

Xamarin

After clicking on the create button it will create the CircleImageDemo Xamarin.Forms project like below.

Xamarin

And the project structure will be,

  • CircleImageDemo
    It is for Shared Code

  • CircleImageDemo.Droid
    It is for Android.

  • CircleImageDemo.iOS
    It is for iOS

Xamarin

We need to follow the below steps to make Circle Image.

Portable Class Library (PCL)

Step 1

First, we need to add Xam.Plugins.Forms.ImageCircle. plugin on all platforms through NuGet.

Right, click on Pakcages->AddPackages
Xamarin

Now, Search plugin with ImageCircle name.

Xamarin

Step 2

Create your own Xaml page named ImageCirclePage.xaml, and make sure to refer to "CircleImage" class in Xaml by declaring a namespace for its location and using the namespace prefix on the control element. The following code example shows how the "CircleImage" plugin renderer class can be consumed by a Xaml page:

ImageCirclePage.xaml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. BackgroundColor="White" 
  4. xmlns:custom="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions" 
  5. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
  6. x:Class="CircleImageDemo.CircleImageDemoPage">  
  7.     <Grid Padding="10,20,10,10" RowSpacing="10">  
  8.         <Grid.RowDefinitions>  
  9.             <RowDefinition Height="Auto" />  
  10.             <RowDefinition Height="*" />  
  11.             <RowDefinition Height="Auto" />  
  12.             <RowDefinition Height="Auto" /> </Grid.RowDefinitions>  
  13.         <Label Text="Normal Image" Grid.Row="0" FontSize="20" HorizontalOptions="CenterAndExpand" />  
  14.         <Image Aspect="AspectFill" Source="BG.png" Grid.Row="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />  
  15.         <Label Text="Circle Image" Grid.Row="2" FontSize="20" HorizontalOptions="CenterAndExpand" />  
  16.         <custom:CircleImage Grid.Row="3" Source="BG.png" WidthRequest="100" HeightRequest="100" Aspect="AspectFill" VerticalOptions="Start" HorizontalOptions="CenterAndExpand" /> </Grid>  
  17. </ContentPage>  

Note

The "custom" namespace prefix can be named anything. However, the clr-namespace and assembly values must match the details of the custom renderer class. Once the namespace is declared the prefix is used to reference the custom control.

ImageCirclePage.xaml.cs

  1. using Xamarin.Forms;  
  2. namespace CircleImageDemo {  
  3.     public partial class CircleImageDemoPage: ContentPage {  
  4.         public CircleImageDemoPage() {  
  5.             InitializeComponent();  
  6.         }  
  7.     }  
  8. }  

Xamarin.Andriod  

MainActivity.cs

  1. using Android.App;  
  2. using Android.Content.PM;  
  3. using Android.OS;  
  4. using ImageCircle.Forms.Plugin.Droid;  
  5. namespace CircleImageDemo.Droid {  
  6.     [Activity(Label = "CircleImageDemo.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]  
  7.     public class MainActivity: global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {  
  8.         protected override void OnCreate(Bundle bundle) {  
  9.             TabLayoutResource = Resource.Layout.Tabbar;  
  10.             ToolbarResource = Resource.Layout.Toolbar;  
  11.             base.OnCreate(bundle);  
  12.             ImageCircleRenderer.Init();  
  13.             global::Xamarin.Forms.Forms.Init(this, bundle);  
  14.             LoadApplication(new App());  
  15.         }  
  16.     }  
The call to the ImageCircleRenderer.Init() instantiates an Android ImageView, with a reference to the control being assigned to the renderer's property.

Output:

Xamarin

Xamarin.iOS

In iOS project, create a class name:

AppDelegate.cs

  1. using Foundation;  
  2. using UIKit;  
  3. using ImageCircle.Forms.Plugin.iOS;  
  4. namespace CircleImageDemo.iOS {  
  5.     [Register("AppDelegate")]  
  6.     public partial class AppDelegate: global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate {  
  7.         public override bool FinishedLaunching(UIApplication app, NSDictionary options) {  
  8.             global::Xamarin.Forms.Forms.Init();  
  9.             ImageCircleRenderer.Init();  
  10.             LoadApplication(new App());  
  11.             return base.FinishedLaunching(app, options);  
  12.         }  
  13.     }  

The call to the ImageCircleRenderer.Init() instantiates an iOS UIImage, with a reference to the control being assigned to the renderer's property.

Output:

Xamarin

 

Please, download the sample from here.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now