• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Java
  • Created about 6 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

dev android use lua language

做移动端开发,做蛋疼的就是不能动态发版,不能像 web 那样发版立即全部用户生效,然而 lua语言 为其提供了可能性。使用 lua 来构建跨平台原生应用有许多好处,比如 lua 语言简洁高效,可移植性好, Lua虚拟机极为轻量,仅占用200到300k的内存空间,且速度极快。

演示

写一个简单的代码演示一下。新建一个 lua 文件,叫做 view.lua, 放在手机的 sdcard 上,文件目录为 /sdcard/view.lua

require "import"
import "android.widget.*"
import "android.content.*"

function getView()
    local layout = {
        LinearLayout,
        orientation = "vertical",
        layout_width = "fill",
        layout_height = "fill",
        {
            Button,
            id = "btn",
            layout_marginTop="8dp",
            layout_width = "fill",
            layout_height = "50dp",
            text = "click"
        },
    }
    local view = loadlayout(layout)
    return view
end

运行一下,

屏幕中上半部分是 Android 的 xml 布局中写好的代码,当点击运行按钮时,加载 lua 脚本,返回一个 View 对象,然后添加到布局中。一个简单的 lua 脚本编写的视图就写好了。 接下来修改一下,设置个点击事件。

require "import"
import "android.widget.*"
import "android.content.*"

function getView()
    local layout = {
        LinearLayout,
        orientation = "vertical",
        layout_width = "fill",
        layout_height = "fill",
        {
            Button,
            id = "btn",
            layout_marginTop="8dp",
            layout_width = "fill",
            layout_height = "50dp",
            text = "click"
        },
    }
    local ids = {} -- store ids to find view
    local view = loadlayout(layout, ids)
    ids.btn.onClick = function()
        Toast.makeText(activity,"2333",0).show()
    end
    return view
end

运行效果

再来个稍微复杂点的例子,写个列表,新建 list.lua 文件,放在手机的 sdcard/list.lua

require "import"
import "android.widget.*"
import "android.content.*"
import "android.view.View"
import "androlua.LuaHttp"
import "androlua.LuaAdapter"
import "androlua.LuaImageLoader"

local JSON = require("cjson")
local uihelper = require('uihelper')

-- create view table
local layout = {
    LinearLayout,
    orientation = "vertical",
    layout_width = "fill",
    layout_height = "fill",
    {
        ListView,
        id = "listview",
        dividerHeight = 0,
        layout_width = "fill",
        layout_height = "fill",
    },
}

local item_view = {
    FrameLayout,
    layout_width = "fill",
    layout_height = "240dp",
    {
        ImageView,
        id = "iv_image",
        layout_width = "fill",
        layout_height = "fill",
        scaleType = "centerCrop",
    },
    {
        TextView,
        id = "tv_title",
        background = "#66000000",
        layout_width = "fill",
        layout_height = "fill",
        padding = "32dp",
        gravity = "center",
        maxLines = "5",
        lineSpacingMultiplier = '1.2',
        textSize = "14sp",
        textColor = "#CCFFFFFF",
    },
}


local data = {
    dailyList = {}
}
local adapter

local function getData()
    -- http://baobab.kaiyanapp.com/api/v1/feed
    local url = data.nextPageUrl
    if url == nil then url = 'http://baobab.kaiyanapp.com/api/v1/feed?udid=3e7ee30c6fc0004a773dc33b0597b5732b145c04' end
    if url:find('udid=') == nil then url = url .. '&udid=3e7ee30c6fc0004a773dc33b0597b5732b145c04' end
    print(url)
    LuaHttp.request({ url = url }, function(error, code, body)
        if error or code ~= 200 then
            print('fetch data error')
            return
        end
        local str = JSON.decode(body)
        uihelper.runOnUiThread(activity, function()
            data.nextPageUrl = str.nextPageUrl
            local list = str.dailyList[1].videoList
            for i = 1, #list do
                data.dailyList[#data.dailyList + 1] = list[i]
            end
            adapter.notifyDataSetChanged()
        end)
    end)
end

local function launchDetail(item)
    Toast.makeText(activity, item.title, 0).show()
end

function getView()
    local view = loadlayout(layout)
    adapter = LuaAdapter(luajava.createProxy("androlua.LuaAdapter$AdapterCreator", {
        getCount = function() return #data.dailyList end,
        getItem = function(position) return nil end,
        getItemId = function(position) return position end,
        getView = function(position, convertView, parent)
            position = position + 1 -- lua 索引从 1开始
            if position == #data.dailyList then
                getData()
            end
            if convertView == nil then
                local views = {} -- store views
                convertView = loadlayout(item_view, views, ListView)
                if parent then
                    local params = convertView.getLayoutParams()
                    params.width = parent.getWidth()
                end
                convertView.setTag(views)
            end
            local views = convertView.getTag()
            local item = data.dailyList[position]
            if item then
                LuaImageLoader.load(views.iv_image, item.coverForFeed)
                views.tv_title.setText(item.title)
            end
            return convertView
        end
    }))
    listview.setAdapter(adapter)
    listview.setOnItemClickListener(luajava.createProxy("android.widget.AdapterView$OnItemClickListener", {
        onItemClick = function(adapter, view, position, id)
            launchDetail(data.dailyList[position + 1])
        end,
    }))
    getData()
    return view
end

创建 listView , 设置 adapter ,网络请求,刷新列表。看下效果吧。

代码放到了 github 👉 源码

原理图

写了几篇文章比较详细的介绍了原理,想了解的可以看一下

支持 iOS 吗?

Lua 是用 c 语言开发的,可移植性比较好,想支持 iOS 的话,原理时一样的,不过参考目前已有的跨平台技术。关于跨平台方面的一些个人见解,目前已有的跨平台技术每当涉及到不同平台的特性时,事情就比较蛋疼了,需要单独去适配,还有建立一堆连接库,比如选取本地图片,不同平台的数据库,平台特有 api,真是一份代码到处运行终是梦,一份儿代码到处采坑才是真

Android 开发能支持到什么程度?

看到了上面的原理图就可以知道,支持 Android SDK 几乎所有的 API。

联系我

More Repositories

1

HTextView

Animation effects to text, not really textview
Java
5,620
star
2

SmallBang

twitter like animation for any view 💓
Java
1,002
star
3

PasscodeView

Material Design PasscodeView for Android.
Java
539
star
4

AnimateCheckBox

A custom view in Android with a animation when CheckBox status changed
Java
472
star
5

Conquer

A todo list app base Material Design
Java
433
star
6

500px-guideview

500px guideview demo
Java
382
star
7

SwipeRefreshLayout

谷歌的下拉刷新,新的界面效果
Java
368
star
8

LineHeightEditText

Fix edittext lineHeight and cursor length when set lineSpacingExtra or lineSpacingMultiplier
Java
203
star
9

hydrogenApp

hydrogen is a pluggable android app
Lua
170
star
10

ScrollViewOnTouch

仿百度助手头部搜索框缩放.
Java
139
star
11

SelectPicture

仿微信选择图片拍照
Java
129
star
12

RxSerach

RxSerach
Java
117
star
13

NumberAnimation

number view with 'add' animation
Java
61
star
14

PhotoViewer

imageView transition animation
Java
46
star
15

juejin_flutter

juejin client ui power by flutter
Dart
44
star
16

ToolBar

带动画的ActionBar --------- ToolBar(兼容低版本)
Java
43
star
17

ViewPager-Listview-item-Viewpager-

Viewpager中嵌套Listview,而且Listview的item中夹杂ViewPage
Java
43
star
18

MaterialCheckBox

Custom view to implement CheckBox in Material Design to 14+
Java
30
star
19

WeixinEye

高仿微信下拉小眼睛动画
Java
27
star
20

Hrn

hanks react-native-demo
JavaScript
23
star
21

SlideLayout

控制View的OnClick OnTouch来实现LsitView的Item的侧滑出现删除
Java
23
star
22

PlayerButton

用于播放音频文件的自定义按钮,有进度条,可暂停
Java
19
star
23

SelectorButton

Custom button that can implement click effect don't need create a selector.xml
Java
19
star
24

capturedata

tools to capture network data 抓包工具总结
19
star
25

FlyWoo

A android app can use the wifi hotspot transmit data without mobile network
Java
17
star
26

OpAnimateView

Material Design View with Animation
Java
14
star
27

BottomLayout

自定义组合控件,底部多按钮切换
Java
10
star
28

DeletableEditText

Custom Edittext with a delete button
Java
8
star
29

AndroidDesignPattern

source code in <<Android 源码设计模式解析与实战>>
Java
8
star
30

233333

鬼畜表情包 react-native版
JavaScript
7
star
31

NestedScroll

Java
6
star
32

eye-video

Vue
6
star
33

RecylerView

Swipe the item of RecylerView and Drag to sort item
Java
6
star
34

SuperRecyclerView

Just like super man :octocat:
Java
5
star
35

LuajAndroid

Android use Luaj
Java
4
star
36

GenerateBanner

generate a beautiful banner by canvas
JavaScript
3
star
37

MoblieSafe

just for learn
Java
2
star
38

MaterialDesign

Design support library with material
Java
2
star
39

TextAnimation

Java
2
star
40

apkInstaller

silent to install apks
Java
2
star
41

LoadingLayout

layout with three children, normal loading selected status
Java
2
star
42

buildluajava

build luajava so files in AS 2.3.2
C
2
star
43

U

:octocat:
Java
2
star
44

RxSimple

Java
2
star
45

themedemo

themedemo
Java
2
star
46

QRcode-scanning

use ZXing to recognize QRcode
Java
2
star
47

videowallpaper

1
star
48

blog-comment

1
star
49

blog_golang

blog api base on golang
Go
1
star
50

ActivityResultManager

convert `activity.startActivityForResult` to callback
Kotlin
1
star
51

lua-5.3.4-64_32

C
1
star
52

joke

joke data form qiqu.uc.cn
Vue
1
star
53

folder-widget

1
star
54

CircleImageView

simple to implemente a circle ImageView with border
Java
1
star
55

resume

a resume app running on android
Java
1
star
56

PythonBlog

Just learn Python
CSS
1
star
57

12306

find available ticket
1
star
58

hanks-zyh.github.io

blog
HTML
1
star
59

hkalbum

a android library to pick pictures form album or camera
1
star
60

AllApplication

Java
1
star
61

lua_into_android

lua 嵌入 Android
C
1
star
62

Flask-blog

a blog base on flask
HTML
1
star
63

AutoPictureforWord

给文字自动配图片
Java
1
star