forked from syncfusion/xamarin-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageEditorCustomView.cs
276 lines (244 loc) · 11.2 KB
/
ImageEditorCustomView.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
#region Copyright Syncfusion Inc. 2001-2022.
// Copyright Syncfusion Inc. 2001-2022. 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.Provider;
using Java.IO;
using Android.Content.PM;
using Android.Graphics.Drawables;
using Android.Runtime;
using Android.Views;
using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace SampleBrowser
{
public class ImageEditorCustomView : SamplePage
{
internal static Activity Activity { get; set; }
View view { get; set; }
public static int Image { get; set; }
ImageButton imageview3;
ImageButton imageview4;
ImageButton imageview5;
Context con { get; set; }
void View_LayoutChange(object sender, View.LayoutChangeEventArgs e)
{
var height = GetImageHeight(imageview3, view.Width / 3);
imageview3.LayoutParameters = new TableRow.LayoutParams((int)view.Width / 3, (int)height);
imageview4.LayoutParameters = new TableRow.LayoutParams((int)view.Width / 3, (int)height);
imageview5.LayoutParameters = new TableRow.LayoutParams((int)view.Width / 3, (int)height);
}
public override Android.Views.View GetSampleContent(Android.Content.Context context)
{
con = context;
LayoutInflater layoutInflater = LayoutInflater.From(context);
view = layoutInflater.Inflate(Resource.Layout.ImageEditorCustomView, null);
imageview3 = view.FindViewById<ImageButton>(Resource.Id.imageview3);
imageview4 = view.FindViewById<ImageButton>(Resource.Id.imageview4);
imageview5 = view.FindViewById<ImageButton>(Resource.Id.imageview5);
view.LayoutChange -= View_LayoutChange;
view.LayoutChange += View_LayoutChange;
imageview3.Click += pictureOnClick1;
imageview4.Click += pictureOnClick2;
imageview5.Click += pictureOnClick3;
LinearLayout layout = new LinearLayout(context);
return view;
}
public void pictureOnClick1(object sender, EventArgs e)
{
Activity = con as Activity;
Activity?.StartActivity(typeof(CustomViewActivity));
Image = Resource.Drawable.CustomViewImage1;
}
public void pictureOnClick2(object sender, EventArgs e)
{
Activity = con as Activity;
Activity?.StartActivity(typeof(CustomViewActivity));
Image = Resource.Drawable.CustomViewImage2;
}
public void pictureOnClick3(object sender, EventArgs e)
{
Activity = con as Activity;
Activity?.StartActivity(typeof(CustomViewActivity));
Image = Resource.Drawable.CustomViewImage3;
}
private float GetImageHeight(ImageButton button, float renderWidth)
{
float originalWidth = 572;
float originalHeight = 860;
float renderedHeight = (renderWidth * originalHeight) / originalWidth;
return renderedHeight;
}
[Activity(Label = "SfImageEditor", ScreenOrientation = ScreenOrientation.Portrait,
Icon = "@drawable/icon")]
public class CustomViewActivity : Activity
{
CustomViewSettings customViewSettings;
void ImageEditor_ItemSelected(object sender, ItemSelectedEventArgs e)
{
if (e.Settings != null && e.Settings is CustomViewSettings)
{
customViewSettings = e.Settings as CustomViewSettings;
}
}
List<ToolbarItem> CustomViewItems;
SfImageEditor imageEditor;
bool isReplaced; string imageName;
protected override void OnCreate(Bundle savedInstanceState)
{
imageEditor = new SfImageEditor(this);
var bitmap = BitmapFactory.DecodeResource(Resources, ImageEditorCustomView.Image);
imageEditor.Bitmap = bitmap;
imageEditor.ItemSelected += ImageEditor_ItemSelected;
SetContentView(imageEditor);
base.OnCreate(savedInstanceState);
imageEditor.SetToolbarItemVisibility("text,transform,shape,path,effects", false);
CustomViewItems = new List<ToolbarItem>()
{
new CustomToolbarItem(){Icon=BitmapFactory.DecodeResource(Resources, Resource.Drawable.ITypogy1),CustomName = "ITypogy1",IconHeight=70 },
new CustomToolbarItem(){Icon=BitmapFactory.DecodeResource(Resources, Resource.Drawable.ITypogy2),CustomName = "ITypogy2",IconHeight=70 },
new CustomToolbarItem(){Icon=BitmapFactory.DecodeResource(Resources, Resource.Drawable.ITypogy3),CustomName = "ITypogy3",IconHeight=70 },
new CustomToolbarItem(){Icon=BitmapFactory.DecodeResource(Resources, Resource.Drawable.ITypogy4),CustomName = "ITypogy4",IconHeight=70 },
new CustomToolbarItem(){Icon=BitmapFactory.DecodeResource(Resources, Resource.Drawable.ITypogy5),CustomName = "ITypogy5",IconHeight=70 },
new CustomToolbarItem(){Icon=BitmapFactory.DecodeResource(Resources, Resource.Drawable.ITypogy6),CustomName = "ITypogy6",IconHeight=70 }
};
// Add the custom tool bar items
var item = new FooterToolbarItem()
{
Text = "Add",
Icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.AddIcon),
SubItems = CustomViewItems,
TextHeight = 20,
};
imageEditor.ToolbarSettings.ToolbarItems.Add(item);
item = new FooterToolbarItem()
{
Text = "Replace",
Icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.ReplaceIcon),
SubItems = CustomViewItems,
TextHeight = 20
};
imageEditor.ToolbarSettings.ToolbarItems.Add(item);
item = new FooterToolbarItem()
{
Icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.BringFrontIcon),
Text = "Bring Front",
TextHeight = 20
};
imageEditor.ToolbarSettings.ToolbarItems.Add(item);
item = new FooterToolbarItem()
{
Icon = BitmapFactory.DecodeResource(Resources, Resource.Drawable.SendBack),
Text = "Send Back",
TextHeight = 20
};
imageEditor.ToolbarSettings.ToolbarItems.Add(item);
imageEditor.ToolbarSettings.SubItemToolbarHeight = 70;
imageEditor.ToolbarSettings.ToolbarItemSelected += OnToolbarItemSelected;
}
private void OnToolbarItemSelected(object sender, ToolbarItemSelectedEventArgs e)
{
string text = string.Empty; //e.ToolbarItem.Name;
if (text != "back")
{
if (e.ToolbarItem is FooterToolbarItem)
{
text = e.ToolbarItem.Text;
e.MoveSubItemsToFooterToolbar = true;
}
else if (e.ToolbarItem is CustomToolbarItem)
text = (e.ToolbarItem as CustomToolbarItem).CustomName;
else
text = e.ToolbarItem.Text;
if (text == "Replace")
{
isReplaced = true;
//imageEditor.ToolbarSettings.FooterToolbarHeight = 70;
}
else if (text == "back")
{
isReplaced = false;
//imageEditor.ToolbarSettings.FooterToolbarHeight = 50;
}
else if (text == "Add")
{
isReplaced = false;
//imageEditor.ToolbarSettings.FooterToolbarHeight = 70;
}
if (isReplaced && imageEditor.IsSelected && (text == "ITypogy1" ||
text == "ITypogy2" || text == "ITypogy3" || text == "ITypogy4" || text == "ITypogy5" || text == "ITypogy6"))
{
imageEditor.Delete();
AddImage(text);
}
if (!isReplaced)
AddImage(text);
if (text == "Bring Front")
imageEditor.BringToFront();
else if (text == "Send Back")
imageEditor.SendToBack();
}
else
{
isReplaced = false;
}
}
private void AddImage(string text)
{
if (text == "ITypogy1")
SetSVGImage("ITypogy1");
else if (text == "ITypogy2")
SetSVGImage("ITypogy2");
else if (text == "ITypogy3")
SetSVGImage("ITypogy3");
else if (text == "ITypogy4")
SetSVGImage("ITypogy4");
else if (text == "ITypogy5")
SetSVGImage("ITypogy5");
else if (text == "ITypogy6")
SetSVGImage("ITypogy6");
}
private void SetSVGImage(string name)
{
imageName = name;
ImageView view = new ImageView(this);
view.LayoutParameters = new ViewGroup.LayoutParams(200, 200);
if (imageName == "ITypogy1")
view.SetImageResource(Resource.Drawable.Typogy1);
else if (imageName == "ITypogy2")
view.SetImageResource(Resource.Drawable.Typogy2);
else if (imageName == "ITypogy3")
view.SetImageResource(Resource.Drawable.Typogy3);
else if (imageName == "ITypogy4")
view.SetImageResource(Resource.Drawable.Typogy4);
else if (imageName == "ITypogy5")
view.SetImageResource(Resource.Drawable.Typogy5);
else if (imageName == "ITypogy6")
view.SetImageResource(Resource.Drawable.Typogy6);
if (isReplaced)
imageEditor.AddCustomView(view, new CustomViewSettings() { Bounds = customViewSettings.Bounds });
else
{
imageEditor.AddCustomView(view, new CustomViewSettings() { });
}
}
}
public class CustomToolbarItem : ToolbarItem
{
public string CustomName { get; set; }
public CustomToolbarItem()
{
}
}
}
}