-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathDW.NativeImage.Android.pas
176 lines (152 loc) · 6.51 KB
/
DW.NativeImage.Android.pas
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
unit DW.NativeImage.Android;
{*******************************************************}
{ }
{ Kastri }
{ }
{ Delphi Worlds Cross-Platform Library }
{ }
{ Copyright 2020-2024 Dave Nottage under MIT license }
{ which is located in the root folder of this library }
{ }
{*******************************************************}
interface
implementation
uses
// RTL
System.TypInfo, System.Classes, System.SysUtils, System.UITypes,
// Android
Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Widget, Androidapi.Helpers, Androidapi.JNI.Net, Androidapi.JNI.JavaTypes,
Androidapi.JNIBridge, Androidapi.JNI, Androidapi.JNI.Support, Androidapi.JNI.Util,
// FMX
FMX.Presentation.Messages, FMX.Presentation.Android, FMX.Presentation.Factory, FMX.Controls, FMX.Controls.Presentation, FMX.Controls.Model,
FMX.Graphics, FMX.Types,
// DW
DW.NativeImage, DW.UIHelper.Android, DW.Graphics.Helpers.Android;
type
TAndroidNativeImage = class(TAndroidNativeView)
private
FImageView: JImageView;
FTextView: JTextView;
FView: JViewGroup;
function GetImageControl: TCustomNativeImage;
function GetModel: TCustomNativeImageModel;
procedure MMBackgroundColorChanged(var AMessage: TDispatchMessageWithValue<string>); message MM_NATIVEIMAGE_BACKGROUNDCOLORCHANGED;
procedure MMImageChanged(var AMessage: TDispatchMessage); message MM_NATIVEIMAGE_IMAGECHANGED;
procedure MMLoadFromFile(var AMessage: TDispatchMessageWithValue<string>); message MM_NATIVEIMAGE_LOADFROMFILE;
procedure MMLoadFromStream(var AMessage: TDispatchMessageWithValue<TStream>); message MM_NATIVEIMAGE_LOADFROMSTREAM;
procedure MMTextChanged(var AMessage: TDispatchMessageWithValue<string>); message MM_NATIVEIMAGE_TEXTCHANGED;
procedure MMTextSettingsChanged(var AMessage: TDispatchMessage); message MM_NATIVEIMAGE_TEXTSETTINGSCHANGED;
procedure PMInit(var AMessage: TDispatchMessage); message PM_INIT;
procedure UpdateText;
procedure UpdateTextSettings;
protected
function CreateView: JView; override;
function DefineModelClass: TDataModelClass; override;
property ImageControl: TCustomNativeImage read GetImageControl;
public
constructor Create; override;
destructor Destroy; override;
property Model: TCustomNativeImageModel read GetModel;
property View: JViewGroup read FView;
end;
{ TAndroidNativeImage }
constructor TAndroidNativeImage.Create;
begin
inherited;
//
end;
destructor TAndroidNativeImage.Destroy;
begin
//
inherited;
end;
function TAndroidNativeImage.CreateView: JView;
var
LLayoutParams: JRelativeLayout_LayoutParams;
begin
LLayoutParams := TJRelativeLayout_LayoutParams.JavaClass.init(TJViewGroup_LayoutParams.JavaClass.MATCH_PARENT,
TJViewGroup_LayoutParams.JavaClass.MATCH_PARENT);
FView := TJRelativeLayout.JavaClass.init(TAndroidHelper.Context);
FImageView := TJImageView.JavaClass.init(TAndroidHelper.Context);
FImageView.setScaleType(TJImageView_ScaleType.JavaClass.FIT_CENTER);
FView.addView(FImageView, LLayoutParams);
FTextView := TJTextView.JavaClass.init(TAndroidHelper.Context);
// Make the image show through
FTextView.setBackgroundColor(TJColor.JavaClass.TRANSPARENT);
FView.addView(FTextView, LLayoutParams);
Result := FView;
end;
function TAndroidNativeImage.DefineModelClass: TDataModelClass;
begin
Result := TCustomNativeImageModel;
end;
function TAndroidNativeImage.GetImageControl: TCustomNativeImage;
begin
Result := TCustomNativeImage(Control)
end;
function TAndroidNativeImage.GetModel: TCustomNativeImageModel;
begin
Result := inherited GetModel<TCustomNativeImageModel>;
end;
procedure TAndroidNativeImage.MMBackgroundColorChanged(var AMessage: TDispatchMessageWithValue<string>);
begin
View.setBackgroundColor(TAndroidHelper.AlphaColorToJColor(Model.BackgroundColor));
end;
procedure TAndroidNativeImage.MMImageChanged(var AMessage: TDispatchMessage);
begin
FImageView.setImageBitmap(Model.Image.ToJBitmap);
end;
procedure TAndroidNativeImage.MMLoadFromFile(var AMessage: TDispatchMessageWithValue<string>);
begin
FImageView.setImageURI(TAndroidHelper.JFileToJURI(TJFile.JavaClass.init(StringToJString(AMessage.Value))));
end;
procedure TAndroidNativeImage.MMLoadFromStream(var AMessage: TDispatchMessageWithValue<TStream>);
var
LBytes: TBytes;
LJBytes: TJavaArray<Byte>;
begin
if AMessage.Value.Size > 0 then
begin
SetLength(LBytes, AMessage.Value.Size);
AMessage.Value.Position := 0;
AMessage.Value.Read(LBytes, 0, Length(LBytes));
LJBytes := TAndroidHelper.TBytesToTJavaArray(LBytes);
FImageView.setImageBitmap(TJBitmapFactory.JavaClass.decodeByteArray(LJBytes, 0, LJBytes.Length));
end
else
FImageView.setImageBitmap(nil);
end;
procedure TAndroidNativeImage.MMTextChanged(var AMessage: TDispatchMessageWithValue<string>);
begin
UpdateText;
end;
procedure TAndroidNativeImage.MMTextSettingsChanged(var AMessage: TDispatchMessage);
begin
UpdateTextSettings;
end;
procedure TAndroidNativeImage.PMInit(var AMessage: TDispatchMessage);
begin
UpdateText;
UpdateTextSettings;
end;
procedure TAndroidNativeImage.UpdateText;
begin
FTextView.setText(StrToJCharSequence(Model.Text), TJTextView_BufferType.JavaClass.NORMAL);
end;
procedure TAndroidNativeImage.UpdateTextSettings;
var
LTextSettings: TTextSettings;
begin
LTextSettings := Model.TextSettingsInfo.TextSettings;
FTextView.setTextColor(TAndroidHelper.AlphaColorToJColor(LTextSettings.FontColor));
FTextView.setTextSize(TJTypedValue.JavaClass.COMPLEX_UNIT_DIP, LTextSettings.Font.Size);
FTextView.setGravity(TPlatformUIHelper.VertTextAlignToGravity(LTextSettings.VertAlign) or
TPlatformUIHelper.HorzTextAlignToGravity(LTextSettings.HorzAlign));
FTextView.setTypeface(TJTypeface.JavaClass.create(StringToJString(LTextSettings.Font.Family),
TPlatformUIHelper.TFontStylesToStyle(LTextSettings.Font.Style)));
end;
initialization
TPresentationProxyFactory.Current.Register(TNativeImage, TControlType.Platform, TAndroidPresentationProxy<TAndroidNativeImage>);
finalization
TPresentationProxyFactory.Current.Unregister(TNativeImage, TControlType.Platform, TAndroidPresentationProxy<TAndroidNativeImage>);
end.