-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathImageViewAsync.cs
More file actions
165 lines (144 loc) · 5.51 KB
/
ImageViewAsync.cs
File metadata and controls
165 lines (144 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using Android.Content;
using Android.Util;
using Android.Runtime;
using Android.Widget;
namespace FFImageLoading.Views
{
[Register("ffimageloading.views.ImageViewAsync")]
[Obsolete("You can now use Android's ImageView")]
public class ImageViewAsync : ImageView
{
private bool _customFit;
public ImageViewAsync(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public ImageViewAsync(Context context) : base(context)
{
}
public ImageViewAsync(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public ImageViewAsync(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
public void CancelLoading()
{
ImageService.Instance.CancelWorkForView(this);
}
private bool _scaleToFit;
/// <summary>
/// Gets or sets a value if the image should be scale to fit in the available space keeping aspect ratio.
/// <remarks>AdjustViewToBounds should be false and ScaleType should be matrix.</remarks>
/// </summary>
public bool ScaleToFit
{
get => _scaleToFit;
set
{
_customFit = true;
_scaleToFit = value;
SetAdjustViewBounds(false);
SetScaleType(ScaleType.Matrix);
}
}
private AlignMode _bottomAlign;
/// <summary>
/// Gets or sets a value if the image should be aligned to left and bottom in the available space.
/// <remarks>AdjustViewToBounds should be false and ScaleType should be matrix.</remarks>
/// </summary>
public AlignMode AlignMode
{
get => _bottomAlign;
set
{
_customFit = true;
_bottomAlign = value;
SetAdjustViewBounds(false);
SetScaleType(ScaleType.Matrix);
}
}
/* FMT: this is not fine when working with RecyclerView... It can detach and cache the view, then reattach it
protected override void OnDetachedFromWindow()
{
CancelLoading();
base.OnDetachedFromWindow();
}*/
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (_customFit && Drawable == null)
{
SetMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
else
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
protected override bool SetFrame(int l, int t, int r, int b)
{
if (_customFit && Drawable != null && Drawable.IntrinsicWidth != 0)
{
var bottomAlignmentDefined = AlignMode != AlignMode.None;
if (ScaleToFit || bottomAlignmentDefined)
{
var matrix = ImageMatrix;
var scaleFactor = 1f;
if (ScaleToFit)
{
var scaleFactorWidth = (float)Width / Drawable.IntrinsicWidth;
var scaleFactorHeight = (float)Height / Drawable.IntrinsicHeight;
if (scaleFactorHeight < scaleFactorWidth)
{
scaleFactor = scaleFactorHeight;
}
else
{
scaleFactor = scaleFactorWidth;
}
if (Math.Abs(scaleFactor - 1f) > float.Epsilon)
{
matrix.SetScale(scaleFactor, scaleFactor, 0, 0);
}
}
if (AlignMode != AlignMode.None)
{
if (AlignMode != AlignMode.TopCenter && Height - (Drawable.IntrinsicHeight * scaleFactor) > 0)
{
//align to the bottom
matrix.PostTranslate(0, Height - (Drawable.IntrinsicHeight * scaleFactor));
}
if (Width - (Drawable.IntrinsicWidth * scaleFactor) > 0)
{
switch (AlignMode)
{
case AlignMode.BottomLeft:
//by default is aligned to the left
break;
case AlignMode.BottomCenter:
matrix.PostTranslate((Width - (Drawable.IntrinsicWidth * scaleFactor)) / 2, 0);
break;
case AlignMode.BottomRight:
matrix.PostTranslate(Width - (Drawable.IntrinsicWidth * scaleFactor), 0);
break;
case AlignMode.TopCenter:
matrix.PostTranslate((Width - (Drawable.IntrinsicWidth * scaleFactor)) / 2, 0);
break;
}
}
}
ImageMatrix = matrix;
}
}
return base.SetFrame(l, t, r, b);
}
}
public enum AlignMode
{
None,
TopCenter,
BottomLeft,
BottomCenter,
BottomRight
}
}