forked from MMP-forTour/cropimage
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCropImageView.cs
278 lines (233 loc) · 8.73 KB
/
CropImageView.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using Android.Content;
using Android.Graphics;
using Android.Util;
using Android.Views;
using System;
using System.Collections.Generic;
namespace CropImage
{
public class CropImageView : ImageViewTouchBase
{
#region Private members
private List<HighlightView> hightlightViews = new List<HighlightView>();
private HighlightView mMotionHighlightView = null;
private float mLastX;
private float mLastY;
private global::CropImage.HighlightView.HitPosition motionEdge;
private Context context;
#endregion
#region Constructor
public CropImageView(Context context, IAttributeSet attrs) : base(context, attrs)
{
SetLayerType(Android.Views.LayerType.Software, null);
this.context = context;
}
#endregion
#region Public methods
public void ClearHighlightViews()
{
this.hightlightViews.Clear();
}
public void AddHighlightView(HighlightView hv)
{
hightlightViews.Add(hv);
Invalidate();
}
#endregion
#region Overrides
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
for (int i = 0; i < hightlightViews.Count; i++)
{
hightlightViews[i].Draw(canvas);
}
}
protected override void OnLayout(bool changed, int left, int top, int right, int bottom)
{
base.OnLayout(changed, left, top, right, bottom);
if (bitmapDisplayed.Bitmap != null)
{
foreach (var hv in hightlightViews)
{
hv.matrix.Set(ImageMatrix);
hv.Invalidate();
if (hv.Focused)
{
centerBasedOnHighlightView(hv);
}
}
}
}
protected override void ZoomTo(float scale, float centerX, float centerY)
{
base.ZoomTo(scale, centerX, centerY);
foreach (var hv in hightlightViews)
{
hv.matrix.Set(ImageMatrix);
hv.Invalidate();
}
}
protected override void ZoomIn()
{
base.ZoomIn();
foreach (var hv in hightlightViews)
{
hv.matrix.Set(ImageMatrix);
hv.Invalidate();
}
}
protected override void ZoomOut()
{
base.ZoomOut();
foreach (var hv in hightlightViews)
{
hv.matrix.Set(ImageMatrix);
hv.Invalidate();
}
}
protected override void PostTranslate(float deltaX, float deltaY)
{
base.PostTranslate(deltaX, deltaY);
for (int i = 0; i < hightlightViews.Count; i++)
{
HighlightView hv = hightlightViews[i];
hv.matrix.PostTranslate(deltaX, deltaY);
hv.Invalidate();
}
}
public override bool OnTouchEvent(MotionEvent ev)
{
CropImage cropImage = (CropImage)context;
if (cropImage.Saving)
{
return false;
}
switch (ev.Action)
{
case MotionEventActions.Down:
for (int i = 0; i < hightlightViews.Count; i++)
{
HighlightView hv = hightlightViews[i];
var edge = hv.GetHit(ev.GetX(), ev.GetY());
if (edge != global::CropImage.HighlightView.HitPosition.None)
{
motionEdge = edge;
mMotionHighlightView = hv;
mLastX = ev.GetX();
mLastY = ev.GetY();
mMotionHighlightView.Mode =
(edge == global::CropImage.HighlightView.HitPosition.Move)
? HighlightView.ModifyMode.Move
: HighlightView.ModifyMode.Grow;
break;
}
}
break;
case MotionEventActions.Up:
if (mMotionHighlightView != null)
{
centerBasedOnHighlightView(mMotionHighlightView);
mMotionHighlightView.Mode = HighlightView.ModifyMode.None;
}
mMotionHighlightView = null;
break;
case MotionEventActions.Move:
if (mMotionHighlightView != null)
{
mMotionHighlightView.HandleMotion(motionEdge,
ev.GetX() - mLastX,
ev.GetY() - mLastY);
mLastX = ev.GetX();
mLastY = ev.GetY();
if (true)
{
// This section of code is optional. It has some user
// benefit in that moving the crop rectangle against
// the edge of the screen causes scrolling but it means
// that the crop rectangle is no longer fixed under
// the user's finger.
ensureVisible(mMotionHighlightView);
}
}
break;
}
switch (ev.Action)
{
case MotionEventActions.Up:
Center(true, true);
break;
case MotionEventActions.Move:
// if we're not zoomed then there's no point in even allowing
// the user to move the image around. This call to center puts
// it back to the normalized location (with false meaning don't
// animate).
if (GetScale() == 1F)
{
Center(true, true);
}
break;
}
return true;
}
#endregion
#region Private helpers
// Pan the displayed image to make sure the cropping rectangle is visible.
private void ensureVisible(HighlightView hv)
{
Rect r = hv.DrawRect;
int panDeltaX1 = Math.Max(0, IvLeft - r.Left);
int panDeltaX2 = Math.Min(0, IvRight - r.Right);
int panDeltaY1 = Math.Max(0, IvTop - r.Top);
int panDeltaY2 = Math.Min(0, IvBottom - r.Bottom);
int panDeltaX = panDeltaX1 != 0 ? panDeltaX1 : panDeltaX2;
int panDeltaY = panDeltaY1 != 0 ? panDeltaY1 : panDeltaY2;
if (panDeltaX != 0 || panDeltaY != 0)
{
PanBy(panDeltaX, panDeltaY);
}
}
// If the cropping rectangle's size changed significantly, change the
// view's center and scale according to the cropping rectangle.
private void centerBasedOnHighlightView(HighlightView hv)
{
Rect drawRect = hv.DrawRect;
float width = drawRect.Width();
float height = drawRect.Height();
float thisWidth = Width;
float thisHeight = Height;
float z1 = thisWidth / width * .6F;
float z2 = thisHeight / height * .6F;
float zoom = Math.Min(z1, z2);
zoom = zoom * this.GetScale();
zoom = Math.Max(1F, zoom);
if ((Math.Abs(zoom - GetScale()) / zoom) > .1)
{
float[] coordinates = new float[]
{
hv.CropRect.CenterX(),
hv.CropRect.CenterY()
};
ImageMatrix.MapPoints(coordinates);
ZoomTo(zoom, coordinates[0], coordinates[1], 300F);
}
ensureVisible(hv);
}
#endregion
}
}