2014-07-09

一、Android类调用lua并回调

Android调用Lua函数,同一时候把类作为參数传递过去。然后再Lua中回调类的函数

调用lua

		mLuaState = LuaStateFactory.newLuaState();
mLuaState.openLibs();
mLuaState.LdoString(KKLua.loadAssetsString(mContext, "lua/swallow.lua"));//将lua转换为字符串
mLuaState.getField(LuaState.LUA_GLOBALSINDEX, "luaUpdate");//获取lua中的function
mLuaState.pushJavaObject(this);//传递參数
mLuaState.call(1, 0);//一个參数, 0个返回值

回调的接口

	public void luaDraw(int imageId, int x, int y, int scale) {//注意也必需要是public, lua才干调用到
System.out.println("imageId, x, y, scale: " + imageId + ", " + x + ", " + y + ", " + scale);
}

lua代码

function luaUpdate(luaCanvas)
luaCanvas:luaDraw(0, 1, 2, 1)
end

二、luajava介绍(在LuaJava库里面包括的)

lua中是没有类的。luajava库中有个luajava能够创建java中的类。并调用。

以下介绍几个方法

①newInstance(className, ...)

说明:能够依据类名创建一个Java类。同一时候返回一个lua变量与Java类相应。这样在lua中就能够直接调用Java中的方法了。

obj = luajava.newInstance("java.lang.Object")
-- obj is now a reference to the new object
-- created and any of its methods can be accessed. -- this creates a string tokenizer to the "a,b,c,d"
-- string using "," as the token separator.
strTk = luajava.newInstance("java.util.StringTokenizer",
"a,b,c,d", ",")
while strTk:hasMoreTokens() do
print(strTk:nextToken())
end

②bindClass(className)

说明:能够让lua中的变量相应一个Java的类(是类。不是实例),这样就能够用lua的这个变量创建实例以及调用静态类

sys = luajava.bindClass("java.lang.System")
print ( sys:currentTimeMillis() ) -- this prints the time returned by the function.

③new(javaClass)

说明:这个就是在②的基础上创建的

str = luajava.bindClass("java.lang.String")
strInstance = luajava.new(str)



④createProxy(interfaceNames, luaObject)

⑤loadLib(className, methodName)

这两个方法眼下还没用到。以后会用了再介绍



以上。谢谢!

最新文章

  1. oracle根据某个字段去重实例
  2. 在Ubuntu下安装ISE并给Atlys板子编程
  3. IDC机房线路质量测试方案
  4. python 函数的文档字符串 docstrings
  5. 【Python】python 普通继承方式和super继承方式
  6. C语言输出格式总结
  7. [转载] 跳表SkipList
  8. 如何让Asp.net Web Api全局预防Xss攻击
  9. SGU 149 Computer Network 树DP/求每个节点最远端长度
  10. GIS:揭开你神秘的面纱
  11. Python开源异步并发框架
  12. spring+mybatis之声明式事务管理初识(小实例)
  13. CDN,你了解多少?
  14. Winform控件根据文字内容自动调整最合适大小
  15. Dubbo介绍和服务架构分析
  16. 根据不同访问设备跳转到PC页面或手机页面
  17. laravel5.8笔记六:公共函数和常量设置
  18. 我发起了一个 网格计算 协议 开源项目 GridP
  19. [20180627]truncate table的另类恢复.txt
  20. Chrome浏览器被hao123劫持,浏览器主页会被篡改为 hao123等

热门文章

  1. 编译pano13的一些注意事项
  2. thinkphp3.2 图片平均颜色值
  3. HTTPS和SSL/TLS协议
  4. [Javascript] Case insensitive sorting for string arrays
  5. [Now] Configure secrets and environment variables with Zeit’s Now
  6. [Git] Use git add --patch for better commit history and mitigating bugs
  7. [Angular Form] ngModel and ngModelChange
  8. ITFriend创业败局(二):初创公司应该怎样分配股权
  9. matplotlib tricks(一)—— 多类别数据的 scatter(cmap)
  10. iOS 利用FZEasyFile本地保存 和 常规保存