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:
- Shared Code
- Xamarin.Android
- 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.
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.
Then, we have to give the app a name; i.e., CircleImageDemo.
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.
After clicking on the create button it will create the CircleImageDemo Xamarin.Forms project like below.
And the project structure will be,
- CircleImageDemo
It is for Shared Code
- CircleImageDemo.Droid
It is for Android.
- CircleImageDemo.iOS
It is for iOS
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
Now, Search plugin with ImageCircle name.
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
- <?xml version="1.0" encoding="utf-8"?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- BackgroundColor="White"
- xmlns:custom="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin.Abstractions"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="CircleImageDemo.CircleImageDemoPage">
- <Grid Padding="10,20,10,10" RowSpacing="10">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto" />
- <RowDefinition Height="*" />
- <RowDefinition Height="Auto" />
- <RowDefinition Height="Auto" /> </Grid.RowDefinitions>
- <Label Text="Normal Image" Grid.Row="0" FontSize="20" HorizontalOptions="CenterAndExpand" />
- <Image Aspect="AspectFill" Source="BG.png" Grid.Row="1" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" />
- <Label Text="Circle Image" Grid.Row="2" FontSize="20" HorizontalOptions="CenterAndExpand" />
- <custom:CircleImage Grid.Row="3" Source="BG.png" WidthRequest="100" HeightRequest="100" Aspect="AspectFill" VerticalOptions="Start" HorizontalOptions="CenterAndExpand" /> </Grid>
- </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
- using Xamarin.Forms;
- namespace CircleImageDemo {
- public partial class CircleImageDemoPage: ContentPage {
- public CircleImageDemoPage() {
- InitializeComponent();
- }
- }
- }
Xamarin.Andriod
MainActivity.cs
- using Android.App;
- using Android.Content.PM;
- using Android.OS;
- using ImageCircle.Forms.Plugin.Droid;
- namespace CircleImageDemo.Droid {
- [Activity(Label = "CircleImageDemo.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
- public class MainActivity: global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
- protected override void OnCreate(Bundle bundle) {
- TabLayoutResource = Resource.Layout.Tabbar;
- ToolbarResource = Resource.Layout.Toolbar;
- base.OnCreate(bundle);
- ImageCircleRenderer.Init();
- global::Xamarin.Forms.Forms.Init(this, bundle);
- LoadApplication(new App());
- }
- }
- }
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.iOS
In iOS project, create a class name:
AppDelegate.cs
- using Foundation;
- using UIKit;
- using ImageCircle.Forms.Plugin.iOS;
- namespace CircleImageDemo.iOS {
- [Register("AppDelegate")]
- public partial class AppDelegate: global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate {
- public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
- global::Xamarin.Forms.Forms.Init();
- ImageCircleRenderer.Init();
- LoadApplication(new App());
- return base.FinishedLaunching(app, options);
- }
- }
- }
The call to the ImageCircleRenderer.Init() instantiates an iOS UIImage, with a reference to the control being assigned to the renderer's property.
Output:
Please, download the sample from here.