Skip to content

Commit 3aeb76f

Browse files
committed
更新canvas,plugins 文档
1 parent 78bd595 commit 3aeb76f

File tree

4 files changed

+215
-23
lines changed

4 files changed

+215
-23
lines changed

docs/plugins.md

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# 插件
2+
3+
auto.js 提供了加载插件的机制,允许用户编写带有 Activity, Service, C/C++库等的 apk,安装到 Android 设备上,并用 Auto.js 加载和调用。
4+
5+
一个插件是一个可独立安装的 apk 文件,用户安装后,再通过 `plugins` 模块加载插件和调用其中的 API。
6+
7+
安装后可以使用以下代码来加载插件
8+
9+
```js
10+
// packageName 为插件的包名,如果没有安装该插件则会抛出错误
11+
var plugin = plugins.load(packageName);
12+
```
13+
14+
:::warning
15+
由于插件系统的设计缺陷,插件 APP 的目标 SDK 与 Autojs 不一致则可能出现兼容性异常
16+
:::
17+
18+
## 开发一个插件
19+
20+
:::info
21+
为了获得最佳兼容性,可将插件 APP 的目标 SDK 与最小 SDK 设置与 Autojs 一致
22+
:::
23+
24+
### 插件 SDK 集成
25+
26+
1. 新建一个 Android 项目,在项目的 build.gradle 文件中添加:
27+
28+
```groovy
29+
allprojects {
30+
repositories {
31+
// ...
32+
maven { url 'https://jitpack.io' }
33+
}
34+
}
35+
```
36+
37+
2. 在具体模块(比如 app)的 build.gradle 文件中添加:
38+
39+
```groovy
40+
dependencies {
41+
// ...
42+
implementation 'com.github.hyb1996:Auto.js-Plugin-SDK:0.2'
43+
}
44+
```
45+
46+
### 插件配置
47+
48+
1. 新建 PluginHelloWorld 文件,继承于 Plugin.
49+
50+
```java
51+
public class PluginHelloWorld extends Plugin {
52+
53+
public PluginHelloWorld(Context context, Context selfContext, Object runtime, Object topLevelScope) {
54+
super(context, selfContext, runtime, topLevelScope);
55+
}
56+
57+
// 返回插件的JavaScript胶水层代码的assets目录路径
58+
@Override
59+
public String getAssetsScriptDir() {
60+
return "plugin-helloworld";
61+
}
62+
63+
// 插件public API,可被JavaScript代码调用
64+
public String getStringFromJava() {
65+
return "Hello, Auto.js!";
66+
}
67+
68+
// 插件public API,可被JavaScript代码调用
69+
public void say(String message) {
70+
getSelfContext().startActivity(new Intent(getSelfContext(), HelloWorldActivity.class)
71+
.putExtra("message", message)
72+
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
73+
}
74+
}
75+
```
76+
77+
2. 新增 MyPluginRegistry 文件,继承于 PluginRegistry:
78+
79+
```java
80+
public class MyPluginRegistry extends PluginRegistry {
81+
82+
static {
83+
// 注册默认插件
84+
registerDefaultPlugin(new PluginLoader() {
85+
@Override
86+
public Plugin load(Context context, Context selfContext, Object runtime, Object topLevelScope) {
87+
return new PluginHelloWorld(context, selfContext, runtime, topLevelScope);
88+
}
89+
});
90+
}
91+
}
92+
```
93+
94+
在 AndroidManifest.xml 中配置以下 meta-data, name 为"org.autojs.plugin.sdk.registry",value 为 MyPluginRegistry 的包名。
95+
96+
```xml {4-6}
97+
<application
98+
...>
99+
100+
<meta-data
101+
android:name="org.autojs.plugin.sdk.registry"
102+
android:value="org.autojs.plugin.sdk.demo.MyPluginRegistry" />
103+
104+
<activity
105+
android:name=".HelloWorldActivity"
106+
android:exported="true" />
107+
108+
<service
109+
android:name=".HelloworldPluginService"
110+
android:exported="true" />
111+
</application>
112+
```
113+
114+
3. 编写 JavaScript 胶水层
115+
116+
在 assets 的相应目录(由 Plugin.getAssetsScriptDir 返回)中添加 index.js 文件,用于作为胶水层导出插件 API。
117+
118+
```js
119+
module.exports = function (plugin) {
120+
let runtime = plugin.runtime;
121+
let scope = plugin.topLevelScope;
122+
123+
function helloworld() {}
124+
125+
helloworld.stringFromJava = plugin.getStringFromJava();
126+
127+
helloworld.say = function (message) {
128+
plugin.say(message);
129+
};
130+
131+
return helloworld;
132+
};
133+
```
134+
135+
4. 在 Auto.js 中调用
136+
编译插件为 apk(assembleDebug/assembleRelease),安装到设备上。在 Auto.js 中使用以下代码调用:
137+
138+
```js
139+
let helloworld = $plugins.load("org.autojs.plugin.sdk.demo");
140+
console.log(helloworld.stringFromJava);
141+
helloworld.say("Hello, Auto.js Pro Plugin");
142+
```

docs/rhino/advanced/canvas.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,49 @@ canvas.drawRect(0, 0, 100, 100, paint);
3636

3737
## canvas.drawARGB(a, r, g, b)
3838

39-
## canvas.draw
39+
绘制 argb 颜色到画布,每个参数为`0~255`的整数,例如
40+
41+
```js
42+
canvas.drawARGB(255, 255, 0, 0);
43+
```
44+
45+
## 在 ui 中使用画布
46+
47+
我们可以使用如下代码来创建一个带有 canvas 的界面
48+
49+
```js
50+
"ui";
51+
ui.layout(
52+
<frame>
53+
<canvas id="board" w="*" h="*" />
54+
</frame>
55+
);
56+
ui.board.on("draw", function (canvas) {
57+
canvas.drawARGB(0xff, 0xff, 0xff, 0xff);
58+
});
59+
```
60+
61+
然后在`draw`事件中可以进行绘制图像
62+
需要注意的是`ui.board`并不是`canvas`,而是一个`TextureView`,由于历史设计此名称为 canvas 很容易造成混浊,在 v7.1.7 版本后可使用别名`texture`,为兼容性考虑`canvas`名称任然保留。
63+
**`draw`事件会在 ui 创建后会有后台线程频繁触发以更新 canvas**,请勿在该含回调函数中做任何与绘制 canvas 无关的事,**在 v7.1.6 版本取消了这一行为**`draw`事件默认仅会触发一次,如果需要更新`canvas`则使用`ui.board.updateCanvas()`来手动触发`draw`事件,如果需要回到以前的行为只需添加以下代码
64+
65+
```js
66+
"ui";
67+
ui.layout(
68+
<frame>
69+
<canvas id="board" w="*" h="*" />
70+
</frame>
71+
);
72+
ui.board.on("draw", function (canvas) {
73+
canvas.drawARGB(0xff, 0xff, 0xff, 0xff);
74+
});
75+
76+
threads.start(() => {
77+
let board = ui.board;
78+
setInterval(() => {
79+
board.updateCanvas();
80+
}, 1000 / 60);
81+
});
82+
```
83+
84+
在 'app 示例/画布' 中可查看更多例子

docs/rhino/advanced/engines.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ toast(engines.myEngine().cwd());
2222
- `interval` \{number} 循环运行时两次运行之间的时间间隔,默认为 0
2323
- `path` \{Array} | \{string} 指定脚本运行的目录。这些路径会用于 require 时寻找模块文件。
2424

25-
在新的脚本环境中运行脚本 script。返回一个[ScriptExectuion](#ScriptExecution)对象。
25+
在新的脚本环境中运行脚本 script。返回一个[ScriptExectuion](#scriptexecution)对象。
2626

2727
所谓新的脚本环境,指定是,脚本中的变量和原脚本的变量是不共享的,并且,脚本会在新的线程中运行。
2828

@@ -81,7 +81,7 @@ exec(add, { a: 1, b: 2 });
8181
- `interval` \{number} 循环运行时两次运行之间的时间间隔,默认为 0
8282
- `path` \{Array} | \{string} 指定脚本运行的目录。这些路径会用于 require 时寻找模块文件。
8383

84-
在新的脚本环境中运行脚本文件 path。返回一个[ScriptExecution](#ScriptExecution)对象。
84+
在新的脚本环境中运行脚本文件 path。返回一个[ScriptExecution](#scriptexecution)对象。
8585

8686
```js
8787
engines.execScriptFile("/sdcard/脚本/1.js");
@@ -96,7 +96,7 @@ engines.execScriptFile("/sdcard/脚本/1.js");
9696
- `interval` \{number} 循环运行时两次运行之间的时间间隔,默认为 0
9797
- `path` \{Array} | \{string} 指定脚本运行的目录。这些路径会用于 require 时寻找模块文件。
9898

99-
在新的脚本环境中运行录制文件 path。返回一个[ScriptExecution](#ScriptExecution)对象。
99+
在新的脚本环境中运行录制文件 path。返回一个[ScriptExecution](#scriptexecution)对象。
100100

101101
```js
102102
engines.execAutoFile("/sdcard/脚本/1.auto");
@@ -112,7 +112,7 @@ engines.execAutoFile("/sdcard/脚本/1.auto");
112112

113113
## engines.myEngine()
114114

115-
返回当前脚本的脚本引擎对象([ScriptEngine](#ScriptEngine))
115+
返回当前脚本的脚本引擎对象([ScriptEngine](#scriptengine))
116116

117117
**[v4.1.0 新增]**
118118
特别的,该对象可以通过`execArgv`来获取他的运行参数,包括外部参数、intent 等。例如:
@@ -127,31 +127,31 @@ log(engines.myEngine().execArgv);
127127

128128
- 返回 \{Array}
129129

130-
返回当前所有正在运行的脚本的脚本引擎[ScriptEngine](#ScriptEngine)的数组。
130+
返回当前所有正在运行的脚本的脚本引擎[ScriptEngine](#scriptengine)的数组。
131131

132132
```js
133133
log(engines.all());
134134
```
135135

136-
# ScriptExecution
136+
## ScriptExecution
137137

138138
执行脚本时返回的对象,可以通过他获取执行的引擎、配置等,也可以停止这个执行。
139139

140140
要停止这个脚本的执行,使用`exectuion.getEngine().forceStop()`.
141141

142-
## ScriptExecution.getEngine()
142+
### ScriptExecution.getEngine()
143143

144-
返回执行该脚本的脚本引擎对象([ScriptEngine](#ScriptEngine))
144+
返回执行该脚本的脚本引擎对象([ScriptEngine](#scriptengine))
145145

146-
## ScriptExecution.getConfig()
146+
### ScriptExecution.getConfig()
147147

148-
返回该脚本的运行配置([ScriptConfig](#ScriptConfig))
148+
返回该脚本的运行配置([ScriptConfig](#scriptconfig))
149149

150-
# ScriptEngine
150+
## ScriptEngine
151151

152152
脚本引擎对象。
153153

154-
## ScriptEngine.isDestroyed()
154+
### ScriptEngine.isDestroyed()
155155

156156
- 返回 \{Boolean}
157157

@@ -163,17 +163,17 @@ sleep(2000);
163163
log(e.getEngine().isDestroyed())
164164
```
165165

166-
## ScriptEngine.forceStop()
166+
### ScriptEngine.forceStop()
167167

168168
停止脚本引擎的执行。
169169

170-
## ScriptEngine.cwd()
170+
### ScriptEngine.cwd()
171171

172172
- 返回 \{string}
173173

174174
返回脚本执行的路径。对于一个脚本文件而言为这个脚本所在的文件夹;对于其他脚本,例如字符串脚本,则为`null`或者执行时的设置值。
175175

176-
## ScriptEngine.getSource()
176+
### ScriptEngine.getSource()
177177

178178
- 返回 [ScriptSource](#scriptsource)
179179

@@ -183,7 +183,7 @@ log(e.getEngine().isDestroyed())
183183
log(engines.myEngine().getSource());
184184
```
185185

186-
## ScriptEngine.emit(eventName[, ...args])
186+
### ScriptEngine.emit(eventName[, ...args])
187187

188188
- `eventName` \{string} 事件名称
189189
- `...args` \{any} 事件参数
@@ -212,29 +212,29 @@ sleep(2000);
212212
e.getEngine().emit("say", "你好");
213213
```
214214

215-
# ScriptConfig
215+
## ScriptConfig
216216

217217
脚本执行时的配置。
218218

219-
## delay
219+
### delay
220220

221221
- \{number}
222222

223223
延迟执行的毫秒数
224224

225-
## interval
225+
### interval
226226

227227
- \{number}
228228

229229
循环运行时两次运行之间的时间间隔
230230

231-
## loopTimes
231+
### loopTimes
232232

233233
- \{number}
234234

235235
循环运行次数
236236

237-
## getPath()
237+
### getPath()
238238

239239
- 返回 \{Array}
240240

docusaurus.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,15 @@ const config: Config = {
6969
docId: 'overview',
7070
label: '综述',
7171
},
72+
{
73+
type: 'doc',
74+
docId: 'plugins',
75+
label: '插件',
76+
},
7277
{
7378
type: 'doc',
7479
docId: 'external_support',
75-
label: '从外部运行 Autox 脚本',
80+
label: '从外部运行脚本',
7681
},
7782
{
7883
type: 'docSidebar',

0 commit comments

Comments
 (0)