Skip to content

Commit 8ab40c9

Browse files
Implemented IImageViewHandler for Android
1 parent fe4ce5b commit 8ab40c9

11 files changed

Lines changed: 110 additions & 15 deletions

File tree

samples/ImageLoading.Forms.Sample/Droid/MainActivity.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override void OnCreate(Bundle bundle)
2424
base.OnCreate(bundle);
2525

2626
Xamarin.Forms.Forms.SetFlags("FastRenderers_Experimental");
27-
CachedImageRenderer.Init(true);
27+
2828

2929
var config = new FFImageLoading.Config.Configuration()
3030
{
@@ -37,7 +37,11 @@ protected override void OnCreate(Bundle bundle)
3737
ImageService.Instance.Initialize(config);
3838

3939
global::Xamarin.Forms.Forms.Init(this, bundle);
40-
LoadApplication(new App());
40+
41+
CachedImageRenderer.Init(true);
42+
CachedImageRenderer.InitImageViewHandler();
43+
44+
LoadApplication(new App());
4145
}
4246

4347
public class CustomLogger : FFImageLoading.Helpers.IMiniLogger

source/FFImageLoading.Forms.Droid/CachedImageRenderer.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,21 @@ public static void Init(bool? enableFastRenderer)
5353
enabled = CachedImageFastRenderer.ElementRendererType != null;
5454
}
5555

56-
RegisterRenderer(typeof(CachedImage), enabled ? typeof(CachedImageFastRenderer) : typeof(CachedImageRenderer));
57-
}
58-
59-
private static void RegisterRenderer(Type type, Type renderer)
60-
{
61-
var assembly = typeof(Image).Assembly;
62-
var registrarType = assembly.GetType("Xamarin.Forms.Internals.Registrar") ?? assembly.GetType("Xamarin.Forms.Registrar");
63-
var registrarProperty = registrarType.GetProperty("Registered", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
56+
Helpers.Dependency.Register(typeof(CachedImage), enabled ? typeof(CachedImageFastRenderer) : typeof(CachedImageRenderer));
57+
}
6458

65-
var registrar = registrarProperty.GetValue(registrarType, null);
66-
var registerMethod = registrar.GetType().GetRuntimeMethod("Register", new[] { typeof(Type), typeof(Type) });
67-
registerMethod.Invoke(registrar, new[] { type, renderer });
68-
}
59+
/// <summary>
60+
/// Call this after Xamarin.Forms.Init to use FFImageLoading in all Xamarin.Forms views
61+
/// Including Xamarin.Forms.Image
62+
/// </summary>
63+
public static void InitImageViewHandler()
64+
{
65+
Helpers.Dependency.Register(typeof(FileImageSource), typeof(FFImageLoadingImageViewHandler));
66+
Helpers.Dependency.Register(typeof(StreamImageSource), typeof(FFImageLoadingImageViewHandler));
67+
Helpers.Dependency.Register(typeof(UriImageSource), typeof(FFImageLoadingImageViewHandler));
68+
Helpers.Dependency.Register(typeof(EmbeddedResourceImageSource), typeof(FFImageLoadingImageViewHandler));
69+
Helpers.Dependency.Register(typeof(DataUrlImageSource), typeof(FFImageLoadingImageViewHandler));
70+
}
6971

7072
private bool _isDisposed;
7173
private IScheduledWork _currentTask;

source/FFImageLoading.Forms.Droid/FFImageLoading.Forms.Droid.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<WarningLevel>4</WarningLevel>
2727
<AndroidLinkMode>None</AndroidLinkMode>
2828
<ConsolePause>false</ConsolePause>
29+
<LangVersion>latest</LangVersion>
2930
</PropertyGroup>
3031
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3132
<DebugSymbols>true</DebugSymbols>
@@ -38,6 +39,7 @@
3839
<ConsolePause>false</ConsolePause>
3940
<DocumentationFile>bin\Release\FFImageLoading.Forms.Platform.xml</DocumentationFile>
4041
<DefineConstants>ANDROID;__ANDROID__</DefineConstants>
42+
<LangVersion>latest</LangVersion>
4143
</PropertyGroup>
4244
<ItemGroup>
4345
<Reference Include="System" />

source/FFImageLoading.Forms.Droid/FFImageLoadingImageViewHandler.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,24 @@
88
using TNativeImageView = Android.Widget.ImageView;
99
#endif
1010

11+
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.FileImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
12+
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.StreamImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
13+
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(Xamarin.Forms.UriImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
14+
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(FFImageLoading.Forms.EmbeddedResourceImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
15+
//[assembly: Xamarin.Forms.ExportImageSourceHandler(typeof(FFImageLoading.Forms.DataUrlImageSource), typeof(FFImageLoading.Forms.Platform.FFImageLoadingImageViewHandler))]
16+
1117
namespace FFImageLoading.Forms.Platform
1218
{
19+
[Preserve(AllMembers = true)]
1320
public class FFImageLoadingImageViewHandler : IImageViewHandler
1421
{
1522
public Task LoadImageAsync(Xamarin.Forms.ImageSource imagesource, TNativeImageView imageView, CancellationToken cancellationToken = default)
1623
{
24+
#if __ANDROID__
25+
if (!IsValid(imageView))
26+
return Task.CompletedTask;
27+
#endif
28+
1729
var source = ImageSourceBinding.GetImageSourceBinding(imagesource, null);
1830
if (source == null)
1931
{
@@ -27,7 +39,13 @@ public Task LoadImageAsync(Xamarin.Forms.ImageSource imagesource, TNativeImageVi
2739

2840
if (source.ImageSource == ImageSource.Url)
2941
{
30-
imageLoader = ImageService.Instance.LoadUrl(source.Path);
42+
var urlSource = (Xamarin.Forms.UriImageSource)imagesource;
43+
imageLoader = ImageService.Instance.LoadUrl(source.Path, urlSource.CacheValidity);
44+
45+
if (!urlSource.CachingEnabled)
46+
{
47+
imageLoader.WithCache(Cache.CacheType.None);
48+
}
3149
}
3250
else if (source.ImageSource == ImageSource.CompiledResource)
3351
{
@@ -59,6 +77,7 @@ public Task LoadImageAsync(Xamarin.Forms.ImageSource imagesource, TNativeImageVi
5977
var tcs = new TaskCompletionSource<IScheduledWork>();
6078

6179
imageLoader
80+
.FadeAnimation(false, false)
6281
.Error(ex => {
6382
tcs.TrySetException(ex);
6483
})
@@ -83,5 +102,28 @@ public Task LoadImageAsync(Xamarin.Forms.ImageSource imagesource, TNativeImageVi
83102

84103
return Task.CompletedTask;
85104
}
105+
#if __ANDROID__
106+
private static bool IsValid(TNativeImageView imageView)
107+
{
108+
if (imageView == null || imageView.Handle == IntPtr.Zero)
109+
return false;
110+
111+
//NOTE: in some cases ContextThemeWrapper is Context
112+
var activity = imageView.Context as Android.App.Activity ?? (Android.App.Activity)Xamarin.Forms.Forms.Context;
113+
if (activity != null)
114+
{
115+
if (activity.IsFinishing)
116+
return false;
117+
if (activity.IsDestroyed)
118+
return false;
119+
}
120+
else
121+
{
122+
return false;
123+
}
124+
125+
return true;
126+
}
127+
#endif
86128
}
87129
}

source/FFImageLoading.Forms.Mac/FFImageLoading.Forms.Mac.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<LinkMode>None</LinkMode>
3232
<XamMacArch></XamMacArch>
3333
<AOTMode>None</AOTMode>
34+
<LangVersion>latest</LangVersion>
3435
</PropertyGroup>
3536
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3637
<DebugSymbols>true</DebugSymbols>
@@ -50,6 +51,7 @@
5051
<LinkMode>None</LinkMode>
5152
<XamMacArch></XamMacArch>
5253
<AOTMode>None</AOTMode>
54+
<LangVersion>latest</LangVersion>
5355
</PropertyGroup>
5456
<ItemGroup>
5557
<Reference Include="System" />

source/FFImageLoading.Forms.Mock/FFImageLoading.Forms.Mock.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
2020
<DebugType>none</DebugType>
2121
<DebugSymbols>false</DebugSymbols>
22+
<LangVersion>latest</LangVersion>
2223
</PropertyGroup>
2324

25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<LangVersion>latest</LangVersion>
27+
</PropertyGroup>
2428
<ItemGroup>
2529
<PackageReference Include="Microsoft.NETCore.Portable.Compatibility" Version="1.0.1" />
2630
<PackageReference Include="Xamarin.Forms" Version="3.6.0.344457" />

source/FFImageLoading.Forms.Tizen/FFImageLoading.Forms.Tizen.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
<Copyright>Daniel Luberda</Copyright>
2121
</PropertyGroup>
2222

23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
24+
<LangVersion>latest</LangVersion>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<LangVersion>latest</LangVersion>
28+
</PropertyGroup>
2329
<ItemGroup>
2430
<PackageReference Include="Tizen.NET" Version="5.0.0.14629" />
2531
<PackageReference Include="Xamarin.Forms.Platform.Tizen" Version="2.5.1.444934" />

source/FFImageLoading.Forms.Touch/FFImageLoading.Forms.Touch.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
2323
<ConsolePause>false</ConsolePause>
24+
<LangVersion>latest</LangVersion>
2425
</PropertyGroup>
2526
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2627
<DebugSymbols>true</DebugSymbols>
@@ -32,6 +33,7 @@
3233
<WarningLevel>4</WarningLevel>
3334
<ConsolePause>false</ConsolePause>
3435
<DocumentationFile>bin\Release\FFImageLoading.Forms.Platform.xml</DocumentationFile>
36+
<LangVersion>latest</LangVersion>
3537
</PropertyGroup>
3638
<ItemGroup>
3739
<Reference Include="System" />

source/FFImageLoading.Forms.WinUWP/FFImageLoading.Forms.WinUWP.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP;__WINDOWS__</DefineConstants>
3131
<ErrorReport>prompt</ErrorReport>
3232
<WarningLevel>4</WarningLevel>
33+
<LangVersion>Latest</LangVersion>
3334
</PropertyGroup>
3435
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3536
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -41,6 +42,7 @@
4142
<ErrorReport>prompt</ErrorReport>
4243
<WarningLevel>4</WarningLevel>
4344
<DocumentationFile>bin\Release\FFImageLoading.Forms.Platform.xml</DocumentationFile>
45+
<LangVersion>Latest</LangVersion>
4446
</PropertyGroup>
4547
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
4648
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -51,6 +53,7 @@
5153
<PlatformTarget>AnyCPU</PlatformTarget>
5254
<UseVSHostingProcess>false</UseVSHostingProcess>
5355
<ErrorReport>prompt</ErrorReport>
56+
<LangVersion>Latest</LangVersion>
5457
</PropertyGroup>
5558
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
5659
<PlatformTarget>ARM</PlatformTarget>
@@ -63,6 +66,7 @@
6366
<UseVSHostingProcess>false</UseVSHostingProcess>
6467
<ErrorReport>prompt</ErrorReport>
6568
<DocumentationFile>bin\ARM\Release\FFImageLoading.Forms.Platform.xml</DocumentationFile>
69+
<LangVersion>Latest</LangVersion>
6670
</PropertyGroup>
6771
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
6872
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -73,6 +77,7 @@
7377
<PlatformTarget>AnyCPU</PlatformTarget>
7478
<UseVSHostingProcess>false</UseVSHostingProcess>
7579
<ErrorReport>prompt</ErrorReport>
80+
<LangVersion>Latest</LangVersion>
7681
</PropertyGroup>
7782
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
7883
<PlatformTarget>x64</PlatformTarget>
@@ -85,6 +90,7 @@
8590
<DocumentationFile>bin\x64\Release\FFImageLoading.Forms.Platform.xml</DocumentationFile>
8691
<UseVSHostingProcess>false</UseVSHostingProcess>
8792
<ErrorReport>prompt</ErrorReport>
93+
<LangVersion>Latest</LangVersion>
8894
</PropertyGroup>
8995
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
9096
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -96,6 +102,7 @@
96102
<UseVSHostingProcess>false</UseVSHostingProcess>
97103
<ErrorReport>prompt</ErrorReport>
98104
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
105+
<LangVersion>Latest</LangVersion>
99106
</PropertyGroup>
100107
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
101108
<PlatformTarget>x86</PlatformTarget>
@@ -109,6 +116,7 @@
109116
<UseVSHostingProcess>false</UseVSHostingProcess>
110117
<ErrorReport>prompt</ErrorReport>
111118
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
119+
<LangVersion>Latest</LangVersion>
112120
</PropertyGroup>
113121
<ItemGroup>
114122
<Compile Include="Properties\AssemblyInfo.cs" />

source/FFImageLoading.Forms/FFImageLoading.Forms.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@
4242
<ProjectReference Include="..\FFImageLoading.Forms.Mock\FFImageLoading.Forms.Mock.csproj" />
4343
<ProjectReference Include="..\FFImageLoading.Mock\FFImageLoading.Mock.csproj" PrivateAssets="All" />
4444
</ItemGroup>
45+
<ItemGroup>
46+
<Folder Include="Helpers\" />
47+
</ItemGroup>
4548
</Project>

0 commit comments

Comments
 (0)