forked from xamarin/Xamarin.Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] new IImageViewHandler API (xamarin#3045)
* [android] new IImageViewHandler API Context: https://github.com/bumptech/glide https://github.com/jonathanpeppers/glidex Currently the way my "proof-of-concept" GlideX library works by completely bypassing `IImageSourceHandler`. GlideX provides its own custom `ImageRenderer` and `ImageCellRenderer`. This was required due to how *opinionated* the Glide library is. Glide's approach is to never allow a developer access to a `Android.Graphics.Bitmap` or `Android.Graphics.Drawable` because we would likely do it wrong... and developers do all the time! To evolve Xamarin.Forms to where images could be better handled down the road, I've introduced a new interface: public interface IImageViewHandler : IRegisterable { Task LoadImageAsync(ImageSource imageSource, ImageView imageView, CancellationToken cancellationToken = default(CancellationToken)); } The idea is that we can query for `IImageViewHandler` and fall back to `IImageSourceHandler`. This would allow GlideX to just be an `IImageViewHandler` and not mess with creating custom renderers. We can also implement `IImageViewHandler` for `FileImageSource`, to get some general performance improvements around loading files from disk: string file = ((FileImageSource)imagesource).File; if (File.Exists(file)) { //Load with imageView.SetImageURI(Android.Net.Uri) } else { //Load with imageView.SetImageResource(int) } I tested this change with new performance tests in `ImageScenarios`: - Load 100 images using `AndroidResource` - Load 100 images from disk - I conveniently prefixed these with `.`, so they appeared first in the scenario list Here are the results from three runs, using `IImageSourceHandler` versus `IImageViewHandler`, in a HAXM emulator: `IImageSourceHandler` - 100x `AndroidResource` - 6059.899 - 3297.885 - 3015.179 `IImageSourceHandler` - 100x from disk - 12398.71 - 14146.41 - 16060.88 `IImageViewHandler` - 100x `AndroidResource` - 6748.766 - 2817.975 - 2456.197 `IImageViewHandler` - 100x from disk - 7326.745 - 4799.001 - 5411.515 There is not going to be as much as an improvement for `AndroidResource` (maybe not any), since Xamarin.Forms looks for `Drawable` since: xamarin#1973 NOTE: that these scenarios are probably too slow to keep, as it seems these performance tests are geared to fail if they take longer than 250ms. I can remove these before we merge this PR. Other changes to make this happen: - `Registrar` was failing with `InvalidCastException` instead of returning `null` when querying for `IImageViewHandler`. I switched to using `as` instead of a hard cast. I am not sure why it was not already setup this way, since `Registrar.Registered.GetHandlerForObject<T>` appears to be called and checked for `null` everywhere. * Changes to get this puppy "mergeable" - Removed code from xamarin#1973 that is obsolete, we should not be able to hit that codepath anymore - Restored: https://github.com/xamarin/Xamarin.Forms/blob/e8660383b03b4010724b36c6eef471a2e74e7c98/Xamarin.Forms.Platform.Android/Extensions/ImageViewExtensions.cs#L55 - Used `SetImageDrawable` and `Drawable` so that `Bugzilla51173_InvalidImage` should pass now - Made adjustments to the perf scenarios
- Loading branch information
1 parent
0c1127a
commit fd73303
Showing
6 changed files
with
115 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
Xamarin.Forms.Platform.Android/Renderers/IImageViewHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Android.Widget; | ||
|
||
namespace Xamarin.Forms.Platform.Android | ||
{ | ||
/// <summary> | ||
/// The successor to IImageSourceHandler, the goal being we can achieve better performance by never creating an Android.Graphics.Bitmap instance | ||
/// </summary> | ||
public interface IImageViewHandler : IRegisterable | ||
{ | ||
Task LoadImageAsync(ImageSource imagesource, ImageView imageView, CancellationToken cancellationToken = default(CancellationToken)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters