-
Notifications
You must be signed in to change notification settings - Fork 204
/
ImageEditorCustomization.cs
371 lines (318 loc) · 13.3 KB
/
ImageEditorCustomization.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#region Copyright Syncfusion Inc. 2001-2018.
// Copyright Syncfusion Inc. 2001-2018. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using Android.App;
using Syncfusion.SfImageEditor.Android;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using Android.Content;
using Android.Content.PM;
using Android.Graphics.Drawables;
using Android.Views;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using Android.Content.Res;
using Android.Util;
namespace SampleBrowser
{
public class ImageEditorCustomization : SamplePage
{
internal static Activity Activity { get; set; }
View view { get; set; }
public static Bitmap Image { get; set; }
Context con { get; set; }
public static String Name;
SfImageEditor editor;
bool isRect, isText, isPath = false;
string location = "";
Share share;
EditText editText;
object Settings;
void Editor_ItemSelected(object sender, ItemSelectedEventArgs e)
{
Settings = e.Settings;
}
public override Android.Views.View GetSampleContent(Android.Content.Context context)
{
var deviceDensity = (int)context.Resources.DisplayMetrics.Density;
// Set our view from the "main" layout resource
var layoutInflater = LayoutInflater.From(context);
view = layoutInflater.Inflate(Resource.Layout.EditorCustomization, null);
FrameLayout mainLayout = view.FindViewById<FrameLayout>
(Resource.Id.CustomizationMain);
editor = new SfImageEditor(context);
editor.Bitmap = BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.Customize);
editor.ItemSelected += Editor_ItemSelected;
editor.ToolbarSettings.IsVisible = false;
editor.SetBackgroundColor(Color.Black);
editor.ToolbarSettings.SubItemToolbarHeight = 0;
var bottomview = layoutInflater.Inflate(Resource.Layout.BottomView, mainLayout, true);
var topview = layoutInflater.Inflate(Resource.Layout.TopView, mainLayout, true);
var rightview = layoutInflater.Inflate(Resource.Layout.RightView, mainLayout, true);
//Bottom View------------------------------------
var bottomView = bottomview.FindViewById<LinearLayout>(Resource.Id.bottomView);
bottomView.SetGravity(GravityFlags.Bottom);
var bParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MatchParent, 50 * deviceDensity, GravityFlags.Bottom);
bottomView.SetBackgroundColor(Color.Transparent);
bParams.SetMargins(0, 0, 0, 10 * deviceDensity);
bottomView.LayoutParameters = bParams;
//Top View------------------------------------
var topView = topview.FindViewById<LinearLayout>(Resource.Id.topView);
var tParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MatchParent, 50 * deviceDensity, GravityFlags.Top);
tParams.SetMargins(0, 10 * deviceDensity, 0, 0);
topView.LayoutParameters = tParams;
//Right View------------------------------------
var rightView = rightview.FindViewById<LinearLayout>(Resource.Id.rightView);
var rParams = new FrameLayout.LayoutParams(65 * deviceDensity, FrameLayout.LayoutParams.MatchParent, GravityFlags.Right);
rParams.SetMargins(0, 250, 0, 250);
rightView.SetBackgroundColor(Color.Transparent);
rightView.SetPadding(0, 0, 15 * deviceDensity, 15 * deviceDensity);
rightView.LayoutParameters = rParams;
rightView.Visibility = ViewStates.Invisible;
topView.Visibility = ViewStates.Invisible;
mainLayout.RemoveAllViews();
(mainLayout.Parent as ViewGroup)?.RemoveAllViews();
mainLayout.AddView(editor);
mainLayout.AddView(topView);
mainLayout.AddView(bottomView);
mainLayout.AddView(rightView);
Button dummyLayout = new Button(context);
dummyLayout.SetBackgroundColor(Color.Transparent);
dummyLayout.Alpha = 0.5F;
mainLayout.AddView(dummyLayout);
dummyLayout.Click += (sender, e) =>
{
topView.Visibility = ViewStates.Visible;
dummyLayout.Visibility = ViewStates.Invisible;
};
//Top view
var reset = topView.FindViewById<ImageButton>(Resource.Id.resetButton);
var undo = topView.FindViewById<ImageButton>(Resource.Id.undoButton);
var rect = topView.FindViewById<ImageButton>(Resource.Id.rectButton);
var text = topView.FindViewById<ImageButton>(Resource.Id.textButton);
var path = topView.FindViewById<ImageButton>(Resource.Id.pathButton);
reset.Click += (sender, e) =>
{
if (editor.IsImageEdited)
{
editor.Reset();
}
rightView.Visibility = ViewStates.Invisible;
topView.Visibility = ViewStates.Invisible;
dummyLayout.Visibility = ViewStates.Visible;
isPath = false;
isText = false;
isRect = false;
};
undo.Click += (sender, e) =>
{
if (editor.IsImageEdited)
{
editor.Undo();
}
};
rect.Click += (sender, e) =>
{
isPath = false;
isText = false;
isRect = true;
AddShapes();
rightView.Visibility = ViewStates.Visible;
};
text.Click += (sender, e) =>
{
isPath = false;
isRect = false;
isText = true;
AddShapes();
rightView.Visibility = ViewStates.Visible;
};
path.Click += (sender, e) =>
{
isPath = true;
isRect = false;
isText = false;
rightView.Visibility = ViewStates.Visible;
editor.AddShape();
};
// colorLayout
var firstBut = rightview.FindViewById<Button>(Resource.Id.firstButton);
var secondBut = rightview.FindViewById<Button>(Resource.Id.secondButton);
var thirdBut = rightview.FindViewById<Button>(Resource.Id.thirdButton);
var fourthBut = rightview.FindViewById<Button>(Resource.Id.fourthButton);
var fifthBut = rightview.FindViewById<Button>(Resource.Id.fifthButton);
var sixthBut = rightview.FindViewById<Button>(Resource.Id.sixthButton);
var seventhBut = rightview.FindViewById<Button>(Resource.Id.seventhButton);
var eightBut = rightview.FindViewById<Button>(Resource.Id.eightButton);
var ninthBut = rightview.FindViewById<Button>(Resource.Id.ninthButton);
var tenthBut = rightview.FindViewById<Button>(Resource.Id.tenthButton);
firstBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#4472c4");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
secondBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#ed7d31");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
thirdBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#ffc000");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
fourthBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#70ad47");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
fifthBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#5b9bd5");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
sixthBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#c1c1c1");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
seventhBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#6f6fe2");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
eightBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#e269ae");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
ninthBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#9e480e");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
tenthBut.Click += (sender, e) =>
{
var color = Color.ParseColor("#997300");
setting(Settings, color);
if (isPath)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Path, new PenSettings() { Color = color });
}
};
//Share
var share = bottomView.FindViewById<ImageButton>(Resource.Id.sharecustomization);
editText = bottomView.FindViewById<EditText>(Resource.Id.captionText);
share.Click += async (sender, e) =>
{
await ShareImageAsync();
};
return mainLayout;
}
void setting(object Settings,Color color)
{
if (isPath) return;
if (Settings is TextSettings)
(Settings as TextSettings).Color = color;
else if(Settings is PenSettings)
(Settings as PenSettings).Color = color;
}
public static bool IsTabletDevice(Android.Content.Context context)
{
try
{
DisplayMetrics displayMetrics = context.Resources.DisplayMetrics;
float screenWidth = displayMetrics.WidthPixels / displayMetrics.Xdpi;
float screenHeight = displayMetrics.HeightPixels / displayMetrics.Ydpi;
double size = System.Math.Sqrt(System.Math.Pow(screenWidth, 2) + System.Math.Pow(screenHeight, 2));
return size >= 6;
}
catch
{
return false;
}
}
async Task ShareImageAsync()
{
editor.Save();
editor.ImageSaving += (sender, e) => {
e.Cancel = false;
};
editor.ImageSaved += (sender, e) => {
location = e.Location;
};
await DelayActionAsync(2500, Sharing);
}
public async Task DelayActionAsync(int delay, Action action)
{
await Task.Delay(delay);
action();
}
void Sharing()
{
share = new Share();
share.Show("Title", string.IsNullOrEmpty(editText.Text) ? "Message" : editText.Text.ToString(), location);
}
void AddShapes()
{
if (isRect)
{
editor.AddShape(Syncfusion.SfImageEditor.Android.ShapeType.Rectangle, new PenSettings());
isRect = true;
isText = false;
isPath = false;
}
else if (isText)
{
editor.AddText("Enter Text", new TextSettings());
isRect = false;
isText = true;
isPath = false;
}
}
}
}