Skip to content

Commit

Permalink
Added possibility to control if fade animation should be enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-luberda committed Sep 28, 2015
1 parent 2462c15 commit 78677fd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
9 changes: 8 additions & 1 deletion FFImageLoading.Common/Config/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace FFImageLoading.Config
public class Configuration
{
public Configuration(int maxCacheSize = 0, HttpClient httpClient = null, IWorkScheduler scheduler = null, IMiniLogger logger = null,
IDiskCache diskCache = null, IDownloadCache downloadCache = null, bool loadWithTransparencyChannel = false)
IDiskCache diskCache = null, IDownloadCache downloadCache = null, bool loadWithTransparencyChannel = false, bool fadeAnimationEnabled=false)
{
MaxCacheSize = maxCacheSize;
HttpClient = httpClient;
Expand All @@ -18,6 +18,7 @@ public Configuration(int maxCacheSize = 0, HttpClient httpClient = null, IWorkSc
DiskCache = diskCache;
DownloadCache = downloadCache;
LoadWithTransparencyChannel = loadWithTransparencyChannel;
FadeAnimationEnabled = fadeAnimationEnabled;
}

/// <summary>
Expand Down Expand Up @@ -61,6 +62,12 @@ public Configuration(int maxCacheSize = 0, HttpClient httpClient = null, IWorkSc
/// </summary>
/// <value><c>true</c> if FFIMageLoading loads images with transparency; otherwise, <c>false</c>.</value>
public bool LoadWithTransparencyChannel { get; private set; }

/// <summary>
/// Gets a value indicating whether this <see cref="FFImageLoading.Config.Configuration"/> fade animation enabled.
/// </summary>
/// <value><c>true</c> if fade animation enabled; otherwise, <c>false</c>.</value>
public bool FadeAnimationEnabled { get; private set; }
}
}

13 changes: 13 additions & 0 deletions FFImageLoading.Common/Work/TaskParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public void Dispose()

public bool? LoadTransparencyChannel { get; private set; }

public bool? FadeAnimationEnabled { get; private set; }

public TaskParameter Transform(ITransformation transformation)
{
if (transformation == null)
Expand Down Expand Up @@ -190,6 +192,17 @@ public TaskParameter TransparencyChannel(bool loadTransparencyChannel)
return this;
}

/// <summary>
/// Indicates if the fade animation should be enabled.
/// </summary>
/// <returns>The TaskParameter instance for chaining the call.</returns>
/// <param name="enabled">If set to <c>true</c> enabled.</param>
public TaskParameter FadeAnimation(bool enabled)
{
FadeAnimationEnabled = enabled;
return this;
}

/// <summary>
/// If image loading fails automatically retry it a number of times, with a specific delay.
/// </summary>
Expand Down
17 changes: 11 additions & 6 deletions FFImageLoading.Droid/Work/ImageLoaderTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public ImageLoaderTask(IDownloadCache downloadCache, IMainThreadDispatcher mainT
{
DownloadCache = downloadCache;
_imageWeakReference = new WeakReference<ImageView>(imageView);

UseFadeInBitmap = true;
}

/// <summary>
Expand Down Expand Up @@ -105,7 +103,14 @@ await MainThreadDispatcher.PostAsync(() =>
/// Gets or sets a value indicating whether a fade in transition is used to show the image.
/// </summary>
/// <value><c>true</c> if a fade in transition is used; otherwise, <c>false</c>.</value>
public bool UseFadeInBitmap { get; set; }
public bool UseFadeInBitmap
{
get
{
return Parameters.FadeAnimationEnabled.HasValue ?
Parameters.FadeAnimationEnabled.Value : ImageService.Config.FadeAnimationEnabled;
}
}

protected IDownloadCache DownloadCache { get; private set; }

Expand Down Expand Up @@ -392,7 +397,7 @@ protected virtual async Task<WithLoadingResult<BitmapDrawable>> GetDrawableAsync
_loadingPlaceholderWeakReference.TryGetTarget(out placeholderDrawable);
}
return WithLoadingResult.Encapsulate<BitmapDrawable>(new FFBitmapDrawable(Context.Resources, bitmap, placeholderDrawable, FADE_TRANSITION_MILISECONDS), streamWithResult.Result);
return WithLoadingResult.Encapsulate<BitmapDrawable>(new FFBitmapDrawable(Context.Resources, bitmap, placeholderDrawable, FADE_TRANSITION_MILISECONDS, UseFadeInBitmap), streamWithResult.Result);
}
}
finally
Expand Down Expand Up @@ -612,12 +617,12 @@ public class FFBitmapDrawable : BitmapDrawable
private Drawable _placeholder;
private volatile bool _animating;

public FFBitmapDrawable(Resources res, Bitmap bitmap, Drawable placeholder, float fadingTime)
public FFBitmapDrawable(Resources res, Bitmap bitmap, Drawable placeholder, float fadingTime, bool fadeEnabled)
: base(res, bitmap)
{
_placeholder = placeholder;
_fadingTime = fadingTime;
_animating = true;
_animating = fadeEnabled;
_startTimeMillis = SystemClock.UptimeMillis();
}

Expand Down
7 changes: 6 additions & 1 deletion FFImageLoading.Forms.Droid/CachedImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ private void UpdateBitmap(Image previous = null)
}

// TransparencyChannel
imageLoader.TransparencyChannel(Element.TransparencyEnabled);
if (Element.TransparencyEnabled.HasValue)
imageLoader.TransparencyChannel(Element.TransparencyEnabled.Value);

// FadeAnimation
if (Element.FadeAnimationEnabled.HasValue)
imageLoader.FadeAnimation(Element.FadeAnimationEnabled.Value);

imageLoader.Finish((work) => ImageLoadingFinished(Element));
imageLoader.Into(Control);
Expand Down
7 changes: 6 additions & 1 deletion FFImageLoading.Forms.Touch/CachedImageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ private void SetImage(CachedImage oldElement = null)
}

// TransparencyChannel
imageLoader.TransparencyChannel(Element.TransparencyEnabled);
if (Element.TransparencyEnabled.HasValue)
imageLoader.TransparencyChannel(Element.TransparencyEnabled.Value);

// FadeAnimation
if (Element.FadeAnimationEnabled.HasValue)
imageLoader.FadeAnimation(Element.FadeAnimationEnabled.Value);

imageLoader.Finish((work) => ImageLoadingFinished(Element));
imageLoader.Into(Control);
Expand Down
23 changes: 20 additions & 3 deletions FFImageLoading.Forms/CachedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,40 @@ public TimeSpan CacheDuration
}
}

public static readonly BindableProperty TransparencyEnabledProperty = BindableProperty.Create<CachedImage, bool> (w => w.TransparencyEnabled, false);
public static readonly BindableProperty TransparencyEnabledProperty = BindableProperty.Create<CachedImage, bool?> (w => w.TransparencyEnabled, null);

/// <summary>
/// Indicates if the transparency channel should be loaded. By default this value comes from ImageService.Config.LoadWithTransparencyChannel.
/// </summary>
public bool TransparencyEnabled
public bool? TransparencyEnabled
{
get
{
return (bool)GetValue(TransparencyEnabledProperty);
return (bool?)GetValue(TransparencyEnabledProperty);
}
set
{
SetValue(TransparencyEnabledProperty, value);
}
}

public static readonly BindableProperty FadeAnimationEnabledProperty = BindableProperty.Create<CachedImage, bool?> (w => w.FadeAnimationEnabled, null);

/// <summary>
/// Indicates if the fade animation effect should be enabled. By default this value comes from ImageService.Config.FadeAnimationEnabled.
/// </summary>
public bool? FadeAnimationEnabled
{
get
{
return (bool?)GetValue(FadeAnimationEnabledProperty);
}
set
{
SetValue(FadeAnimationEnabledProperty, value);
}
}

public static readonly BindableProperty LoadingPlaceholderProperty = BindableProperty.Create<CachedImage, ImageSource> (w => w.LoadingPlaceholder, null);

/// <summary>
Expand Down

0 comments on commit 78677fd

Please sign in to comment.