forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerializedTemplate.cs
352 lines (306 loc) · 13.4 KB
/
SerializedTemplate.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
#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 Android.Support.V4.Widget;
using System.Threading.Tasks;
using System.IO;
using Android.Content.Res;
using System.Text;
namespace SampleBrowser
{
public class SerializedTemplate : SamplePage
{
internal static Activity Activity { get; set; }
View view { get; set; }
public static Bitmap Image { get; set; }
ImageButton imageview1;
ImageButton imageview2;
ImageButton imageview3;
Context con { get; set; }
public static String Name;
public override Android.Views.View GetSampleContent(Android.Content.Context context)
{
con = context;
LayoutInflater layoutInflater = LayoutInflater.From(context);
view = layoutInflater.Inflate(Resource.Layout.ImageEditorSerialization, null);
imageview1 = view.FindViewById<ImageButton>(Resource.Id.banner1);
imageview2 = view.FindViewById<ImageButton>(Resource.Id.banner2);
imageview3 = view.FindViewById<ImageButton>(Resource.Id.banner3);
imageview1.Click += pictureOnClick1;
imageview2.Click += pictureOnClick2;
imageview3.Click += pictureOnClick3;
return view;
}
public void pictureOnClick1(object sender, EventArgs e)
{
Name = "Coffee";
Activity = con as Activity;
Activity?.StartActivity(typeof(ImageEditorActivity));
Image = ((BitmapDrawable)imageview1.Drawable).Bitmap;
}
public void pictureOnClick2(object sender, EventArgs e)
{
Name = "Food";
Activity = con as Activity;
Activity?.StartActivity(typeof(ImageEditorActivity));
Image = ((BitmapDrawable)imageview2.Drawable).Bitmap;
}
public void pictureOnClick3(object sender, EventArgs e)
{
Name = "Syncfusion";
Activity = con as Activity;
Activity?.StartActivity(typeof(ImageEditorActivity));
Image = ((BitmapDrawable)imageview3.Drawable).Bitmap;
}
[Activity(Label = "SfImageEditor", ScreenOrientation = ScreenOrientation.Portrait,
Icon = "@drawable/icon")]
public class ImageEditorActivity : Activity
{
View view { get; set; }
LinearLayout contentView { get; set; }
private DrawerLayout mDrawerLayout;
List<TableItem> tableItems = new List<TableItem>();
ListView listView;
SfImageEditor editor;
Share share;
string location = "";
string content;
ViewModel viewModel;
AssetManager assets;
protected override void OnCreate(Bundle savedInstanceState)
{
viewModel = new ViewModel();
LayoutInflater layoutInflater = LayoutInflater.From(this);
view = layoutInflater.Inflate(Resource.Layout.EditorSerializationMain, null);
SetContentView(view);
LinearLayout mainLayout = FindViewById<LinearLayout>
(Resource.Id.ContentView);
listView = FindViewById<ListView>(Resource.Id.right_drawer);
mDrawerLayout = (DrawerLayout) FindViewById(Resource.Id.drawer_layout);
tableItems.Add(new TableItem() { ImageResourceId = viewModel.ModelList[0].Name,ImageName ="Coffee"});
tableItems.Add(new TableItem() { ImageResourceId = viewModel.ModelList[1].Name,ImageName ="Food"});
tableItems.Add(new TableItem() { ImageResourceId = viewModel.ModelList[2].Name,ImageName ="Syncfusion"});
listView.Adapter = new HomeScreenAdapter(this, tableItems);
listView.ItemClick += OnListItemClick;
editor = new SfImageEditor(this);
editor.ImageSaving+= Editor_ImageSaving;
editor.Bitmap = SerializedTemplate.Image;
assets = this.Assets;
LoadStream();
DelayActionAsync(500, LoadStreamToEditor);
//Add ToolBar items
HeaderToolBarItem item1 = new HeaderToolBarItem();
item1.Text = "Settings";
item1.Icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.sett);
HeaderToolBarItem item2 = new HeaderToolBarItem();
item2.Text = "Share";
item2.Icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.share);
HeaderToolBarItem item3 = new HeaderToolBarItem();
item3.Icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.info_24);
editor.ToolbarSettings.ToolbarItems.Add(item1);
editor.ToolbarSettings.ToolbarItems.Add(item2);
editor.ToolbarSettings.ToolbarItems.Add(item3);
editor.ToolbarSettings.ToolbarItemSelected += (sender, e) => {
if (e.Toolbar is HeaderToolBarItem)
{
var text = (e.Toolbar as HeaderToolBarItem).Text;
if (e.Toolbar is HeaderToolBarItem)
{
if ((e.Toolbar as HeaderToolBarItem).Text == "Share")
{
ShareImage();
}
if ((e.Toolbar as HeaderToolBarItem).Text == "Settings")
{
mDrawerLayout.OpenDrawer(listView);
}
if ((text != "Reset" && text != "undo" && text != "redo" && text != "Save" && text != "Settings" && text != "Share"))
{
string strin = "ImageEditor allows you to serialize and deserialize any custom edits(Shapes,Text,Path) over an image. In this sample we have deserialized some custom edits and loaded in to the editor.";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("About this sample");
alert.SetMessage(strin.ToString());
alert.SetNegativeButton("Ok", (senderAlert, args) => {
});
Dialog dialog = alert.Create();
dialog.Show();
}
}
}
};
mainLayout.AddView(editor);
base.OnCreate(savedInstanceState);
}
void LoadStreamToEditor()
{
for (int i = 0; i < viewModel.ModelList.Count(); i++)
{
if (SerializedTemplate.Name == viewModel.ModelList[i].ImageName)
{
using (StreamReader sr = new StreamReader(assets.Open(viewModel.ModelList[i].Imagestream)))
{
content = sr.ReadToEnd();
byte[] byteArray = Encoding.ASCII.GetBytes(content);
MemoryStream stream = new MemoryStream(byteArray);
editor.LoadAsJson(stream);
viewModel.ModelList[i].Strm = stream;
}
}
}
}
void LoadStream()
{
for (int i = 0; i < viewModel.ModelList.Count(); i++)
{
using (StreamReader sr = new StreamReader(assets.Open(viewModel.ModelList[i].Imagestream)))
{
content = sr.ReadToEnd();
byte[] byteArray = Encoding.ASCII.GetBytes(content);
MemoryStream stream = new MemoryStream(byteArray);
viewModel.ModelList[i].Strm = stream;
}
}
}
void Editor_ImageSaving(object sender, ImageSavingEventArgs e)
{
for (int i = 0; i < viewModel.ModelList.Count(); i++)
{
if (SerializedTemplate.Name == viewModel.ModelList[i].ImageName)
{
var stream= editor.GetSerializedObject();
viewModel.ModelList[i].Strm=stream;
}
}
}
void ShareImage()
{
editor.Save();
editor.ImageSaving+= (sender, e) => {
e.Cancel = false;
};
editor.ImageSaved+= (sender, e) => {
location = e.Location;
};
DelayActionAsync(2500, Sharing);
}
public async Task DelayActionAsync(int delay, Action action)
{
await Task.Delay(delay);
action();
}
void Sharing()
{
share = new Share();
share.Show("Share", "Message", location);
}
protected void OnListItemClick(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
{
var listView = sender as ListView;
var t = tableItems[e.Position];
editor.Bitmap = BitmapFactory.DecodeResource(Resources, t.ImageResourceId);
for (int i = 0; i < viewModel.ModelList.Count(); i++)
{
if (t.ImageResourceId == viewModel.ModelList[i].Name)
{
Stream str = viewModel.ModelList[i].Strm;
editor.LoadAsJson(str);
SerializedTemplate.Name = t.ImageName;
}
}
mDrawerLayout.CloseDrawer(listView);
}
}
public class TableItem
{
public int ImageResourceId { get; set; }
public int Name { get; set; }
public string ImageName { get; set; }
}
public class HomeScreenAdapter : BaseAdapter<TableItem>
{
List<TableItem> items;
Activity context;
public HomeScreenAdapter(Activity context, List<TableItem> items)
: base()
{
this.context = context;
this.items = items;
}
public override long GetItemId(int position)
{
return position;
}
public override TableItem this[int position]
{
get { return items[position]; }
}
public override int Count
{
get { return items.Count; }
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = items[position];
View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.CustomListView, null);
view.FindViewById<ImageView>(Resource.Id.Image).SetImageResource(item.ImageResourceId);
return view;
}
}
}
//public class Share
//{
// private readonly Context _context;
// public Share()
// {
// _context = Application.Context;
// }
// public Task Show(string title, string message, string filePath)
// {
// var extension = filePath.Substring(filePath.LastIndexOf(".") + 1).ToLower();
// var contentType = string.Empty;
// switch (extension)
// {
// case "pdf":
// contentType = "application/pdf";
// break;
// case "png":
// contentType = "image/png";
// break;
// case "jpg":
// contentType = "image/jpg";
// break;
// default:
// contentType = "application/octetstream";
// break;
// }
// var intent = new Intent(Intent.ActionSend);
// intent.SetType(contentType);
// intent.PutExtra(Intent.ExtraStream, Android.Net.Uri.Parse("file://" + filePath));
// intent.PutExtra(Intent.ExtraText, message ?? string.Empty);
// intent.PutExtra(Intent.ExtraSubject, title ?? string.Empty);
// var chooserIntent = Intent.CreateChooser(intent, title ?? string.Empty);
// chooserIntent.SetFlags(ActivityFlags.ClearTop);
// chooserIntent.SetFlags(ActivityFlags.NewTask);
// _context.StartActivity(chooserIntent);
// return Task.FromResult(true);
// }
//}
}