-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
265 lines (239 loc) · 7.35 KB
/
main.lua
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
-- 请使用AndroLuaX打开
-- 开源地址:
--- Gitee: https://gitee.com/AideLua/AndroLuaResGetter
--- GitHub: https://github.com/AideLua/AndroLuaResGetter
-- 欢迎 Star!`(*∩_∩*)′
require "import"
local startTime=System.currentTimeMillis()
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
import "android.text.style.TypefaceSpan"
import "android.text.Spannable"
import "android.text.SpannableString"
import "android.text.style.ForegroundColorSpan"
import "android.graphics.Typeface"
import "res"
--以下是封装好的各种各样的代码。如需使用请查看 README.md 以及 layout.aly 最后的demo
isAndroidX=false
xpcall(function()
import "androidx.appcompat.widget.Toolbar"
import "androidx.appcompat.view.ContextThemeWrapper"
activity.setTheme(R.style.Theme_Material3_DynamicColors_DayNight)
isAndroidX=true
end,
function()
activity.setTheme(android.R.style.Theme_Material_Light)
end)
pcall(function()
androidhwext={R=luajava.bindClass("androidhwext.R")}
isEmui=true
end)
--EMUI系统会有额外的 R 类
if isEmui then
androidhwext.res=res.getOrNewResWithRClass(androidhwext.R)
end
local SDK_INT=Build.VERSION.SDK_INT
isSdk31AndUp=SDK_INT>=31
local codeTypeface=Typeface.createFromFile(activity.getLuaDir().."/JetBrainsMono-Regular.ttf")
--分割线布局
divider={
View;
layout_width="fill",
layout_height="1dp",
backgroundColor=android.res.color.attr.textColorPrimary;
alpha=0.12;
layout_marginLeft="4dp";
layout_marginRight="4dp";
}
--当系统不支持时占位
placeholderView={
View;
layout_width=0,
layout_height=0,
}
---运行代码,并pcall,防止代码报错
---@param code string
---@param code showError
---@return all 代码返回结果。如果代码报错,则返回 "您的设备无此资源资源:\n..."
function pcallLoadCode(code,showError)
local success,value=pcall(assert(load("return "..code)))
if success then
return value
else
--只有编辑器问题才允许继续执行
local isEditorError=value:match("is not a field or mothod")
or value:match("Class not found: .-%.R")
or value:match("attempt to index a nil value %(global 'androidhwext'%)")
if not isEditorError then
error(value,2)
end
if showError then
return "您的设备或编译器无此资源资源:\n"..value
end
end
end
---生成标题布局
---@param text string
---@return table layout 生成的布局
function buildTitle(text)
return {
TextView,
text=text;
textColor=android.res.color.attr.colorAccent;
textSize="14sp";
padding="4dp";
paddingTop="8dp";
paddingBottom="4dp";
}
end
---生成颜色展示布局
---@param zhName string
---@param enName string
---@param callName string
---@param enableSwitchView boolean
---@return table layout 生成的布局
function buildColorShow(zhName,enName,callName,enableSwitchView)
local title=zhName.."\n"..enName
local text=title.."\n "..callName
local spannableText=SpannableString(text)
return {
HorizontalScrollView;
layout_width="fill";
fillViewport="true";
{
enableSwitchView and Switch or TextView,
text=spannableText,
textColor=pcallLoadCode(callName,false);
layout_width="fill",
padding="8dp";
paddingTop="4dp";
paddingBottom="4dp";
checked=enableSwitchView and "true";
textIsSelectable="true";
}
};
end
---生成背景展示布局
---@param zhName string
---@param enName string
---@param callName string
---@param enableSwitchView boolean
---@return table layout 生成的布局
function buildBgShow(zhName,enName,callName,enableSwitchView)
local title=zhName.."\n"..enName
local text=title.."\n "..callName
local spannableText=SpannableString(text)
return {
HorizontalScrollView;
layout_width="fill";
fillViewport="true";
{
enableSwitchView and Switch or TextView,
text=spannableText,
background=pcallLoadCode(callName,false);
layout_width="fill",
padding="8dp";
paddingTop="4dp";
paddingBottom="4dp";
checked=enableSwitchView and "true";
textIsSelectable="true";
}
};
end
---生成值展示布局
---@param resIdCallName string
---@param callNames string[]
---@return table layout 生成的布局
function buildValueShow(resIdCallName,callNames)
local text=resIdCallName
local colorList={}
for key,value in ipairs(callNames) do
local reallyValue=pcallLoadCode(value,true)
local valueType=type(reallyValue)
local dumpValue=dump(reallyValue):match("(.+) ;")
text=text.."\n "..value..": "..dumpValue
local textLength=utf8.len(text)
local valueLength=utf8.len(dumpValue)
local color=0xff9e9e9e
if valueType=="string" then
color=0xffe91e63
elseif valueType=="number" then
color=0xff2196f3
elseif valueType=="boolean" or valueType=="nil" then
color=0xff9c27b0
end
table.insert(colorList,{color,textLength-valueLength,textLength})
end
local spannableText=SpannableString(text)
for index,content in ipairs(colorList) do
if content[1] then
spannableText.setSpan(ForegroundColorSpan(content[1]),content[2],content[3],Spannable.SPAN_INCLUSIVE_INCLUSIVE)
spannableText.setSpan(TypefaceSpan(codeTypeface),content[2],content[3],Spannable.SPAN_INCLUSIVE_INCLUSIVE)
end
end
return {
HorizontalScrollView;
layout_width="fill";
fillViewport="true";
{
TextView,
text=spannableText;
layout_width="fill",
padding="8dp";
paddingTop="4dp";
paddingBottom="4dp";
textIsSelectable="true";
}
};
end
local afterLoadModuleTime=System.currentTimeMillis()
local layout=require "layout"
local afterLoadLayTime=System.currentTimeMillis()
table.insert(layout[2],{
TextView,
text="获取资源耗时:"..afterLoadLayTime-afterLoadModuleTime,
textColor=android.res.color.attr.textColorPrimary;
layout_width="fill",
padding="4dp";
})
for i=1,100000 do
local colorAccent=android.res.color.attr.colorAccent
end
local after100000GetColorTestTime=System.currentTimeMillis()
table.insert(layout[2],{
TextView,
text="获取100000次颜色耗时:"..after100000GetColorTestTime-afterLoadLayTime,
textColor=android.res.color.attr.textColorPrimary;
layout_width="fill",
padding="4dp";
})
for i=1,1000 do
local sym_def_app_icon=android.res.drawable.sym_def_app_icon
end
local after1000GetDrawableTestTime=System.currentTimeMillis()
table.insert(layout[2],{
TextView,
text="获取1000次图片耗时:"..after1000GetDrawableTestTime-after100000GetColorTestTime,
textColor=android.res.color.attr.textColorPrimary;
layout_width="fill",
padding="4dp";
})
--设置缓存图片,请谨慎使用,因为这种方式可能导致状态,还会造成内存泄露
res.setSupportCacheGetterMap("getDrawable",true)
for i=1,100000 do
local sym_def_app_icon=android.res.drawable.sym_def_app_icon
end
local after100000GetCacheDrawableTestTime=System.currentTimeMillis()
table.insert(layout[2],{
TextView,
text="获取100000次缓存后图片耗时:"..after100000GetCacheDrawableTestTime-after1000GetDrawableTestTime,
textColor=android.res.color.attr.textColorPrimary;
layout_width="fill",
padding="4dp";
})
activity.setContentView(loadlayout(layout))--显示版本信息
actionBar=isAndroidX and activity.getSupportActionBar() or activity.getActionBar()
actionBar.setSubtitle("v"..res._VERSION)
local endTime=System.currentTimeMillis()