Skip to content

Commit 6fbf629

Browse files
committed
lua add android view
1 parent 9f72254 commit 6fbf629

31 files changed

+768
-61
lines changed

app/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
targetSdkVersion 26
99
versionCode 1
1010
versionName "1.0"
11-
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
11+
testInstrumentationRunner "android.support.create_view_animation_activity.lua.runner.AndroidJUnitRunner"
1212
}
1313
buildTypes {
1414
release {
@@ -30,6 +30,5 @@ dependencies {
3030
implementation 'com.android.support:appcompat-v7:26.1.0'
3131
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
3232
testImplementation 'junit:junit:4.12'
33-
androidTestImplementation 'com.android.support.test:runner:1.0.1'
34-
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
33+
compile 'com.android.support:recyclerview-v7:26.1.0'
3534
}

app/src/androidTest/java/com/example/zhangpeng/androidlua/ExampleInstrumentedTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
import static org.junit.Assert.*;
1111

1212
/**
13-
* Instrumented test, which will execute on an Android device.
13+
* Instrumented create_view_animation_activity.lua, which will execute on an Android device.
1414
*
1515
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
1616
*/
1717
@RunWith(AndroidJUnit4.class)
1818
public class ExampleInstrumentedTest {
1919
@Test
2020
public void useAppContext() throws Exception {
21-
// Context of the app under test.
21+
// Context of the app under create_view_animation_activity.lua.
2222
Context appContext = InstrumentationRegistry.getTargetContext();
2323

2424
assertEquals("com.example.zhangpeng.androidlua", appContext.getPackageName());

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package="com.example.zhangpeng.androidlua">
44

55
<application
6+
android:name=".MainApplication"
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"
@@ -16,6 +17,8 @@
1617
<category android:name="android.intent.category.LAUNCHER" />
1718
</intent-filter>
1819
</activity>
20+
<activity android:name=".UI.CreateViewAndAnimationActivity" />
21+
<activity android:name=".UI.recyclerview.RecyclerViewActivity"/>
1922
</application>
2023

2124
</manifest>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
function onCreate(activity, rootView)
3+
activity:setTitle("由网络Lua脚本完成布局")
4+
context = activity
5+
6+
contentView = luajava.newInstance("android.widget.LinearLayout",activity)
7+
rootView:addView(contentView)
8+
contentView:setOrientation(1)
9+
contentView:setPadding(50,50,50,50)
10+
11+
local button1 = luajava.newInstance("android.widget.Button",activity)
12+
button1:setText("根据网络Lua脚本动态生成的button.....一可赛艇")
13+
contentView:addView(button1)
14+
15+
local button2 = luajava.newInstance("android.widget.Button",activity)
16+
button2:setText("又追加了个button")
17+
contentView:addView(button2)
18+
19+
local bt1ClickListener = luajava.createProxy("android.view.View$OnClickListener", button1_cb)
20+
button1:setOnClickListener(bt1ClickListener)
21+
22+
return contentView
23+
end
24+
25+
26+
27+
button1_cb={}
28+
function button1_cb.onClick(view)
29+
local button1 = luajava.newInstance("android.widget.Button",view:getContext())
30+
button1:setText("请求网络顺便生成一个button")
31+
contentView:addView(button1)
32+
33+
local HttpCall = luajava.bindClass("com.nightfarmer.luabridge.sample.net.HttpCall")
34+
local downLoadCallBack = luajava.createProxy("com.nightfarmer.luabridge.sample.net.NetLuaCallback", dl_cb)
35+
36+
local path = view:getContext():getFilesDir()
37+
local file = luajava.newInstance("java.io.File", path, "hehehe")
38+
39+
HttpCall:downLoad("https://github.com/NightFarmer/LuaBridge/raw/master/sample/src/main/assets/NetLuaActivity.lua", file, downLoadCallBack)
40+
41+
end
42+
43+
dl_cb={}
44+
function dl_cb.onSuccess(file)
45+
Toast = luajava.bindClass("android.widget.Toast")
46+
Toast:makeText(context,"网络请求成功", 0):show()
47+
end
48+
49+
function dl_cb.onFailure(msg, errcode)
50+
Toast = luajava.bindClass("android.widget.Toast")
51+
Toast:makeText(context,""..errcode..":"..msg,0):show()
52+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function onCreate(activity, rootView)
2+
activity:setTitle("使用Lua脚本构建RecyclerView")
3+
context = activity
4+
local recyclerView = luajava.newInstance("com.example.zhangpeng.androidlua.UI.recyclerview.LuaRecyclerView", activity)
5+
-- local recyclerView = luajava.newInstance("android.support.v7.widget.RecyclerView", activity)
6+
local linearManager = luajava.newInstance("android.support.v7.widget.LinearLayoutManager", activity)
7+
recyclerView:setLayoutManager(linearManager)
8+
rootView:addView(recyclerView)
9+
10+
-- 这里之所以是自己写一个接口去代理,是因为RecyclerView.Adapter 是一个抽象类,不是接口,无法直接用代理类
11+
--类去创建,所以自己封装一层接口去实现
12+
local adapter = luajava.createProxy("com.example.zhangpeng.androidlua.UI.recyclerview.LuaRecyclerAdapter", rec_adapter)
13+
--local adapter = luajava.createProxy("android.support.v7.widget.RecyclerView$Adapter", rec_adapter)
14+
recyclerView:setAdapter(adapter)
15+
end
16+
17+
rec_adapter = {}
18+
function rec_adapter.onCreateViewHolder(parent, viewType)
19+
local button1 = luajava.newInstance("android.widget.TextView", context)
20+
button1:setPadding(50, 50, 50, 50)
21+
button1:setText("根据网络Lua脚本动态生成的button.....一可赛艇")
22+
--parent:addView(button1)
23+
local holder = luajava.newInstance("com.example.zhangpeng.androidlua.UI.recyclerview.LuaViewHolder", button1)
24+
holder:putView("btn1", button1)
25+
return holder
26+
end
27+
28+
function rec_adapter.onBindViewHolder(holder, position)
29+
holder:getView("btn1"):setText("yoo" .. position)
30+
end
31+
32+
function rec_adapter.getItemCount()
33+
return 100
34+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
4+
function createTextViewByLua(context,layout,des,color)
5+
--创建 TextView 对象
6+
tv = luajava.newInstance("android.widget.TextView",context)
7+
--调用方法
8+
tv:setText(des.."..追加中文")
9+
layout:addView(tv)
10+
--负数的负数,这个值为绿色,也就是说二进制取反+1再加上负号
11+
tv:setTextColor(-16711936)
12+
return tv
13+
end
14+
15+
function startAnimation(animationView)
16+
ObjectAnimator = luajava.bindClass("com.example.zhangpeng.androidlua.ObjectAnimator")
17+
local animator = ObjectAnimator:ofFloat(animationView, "rotation",0,360)
18+
animator:setDuration(1000)
19+
animator:start()
20+
end

app/src/main/assets/main_activity.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
--启动 CreateViewActivity
4+
function startCreateViewActivity(context)
5+
--创建一个新java对象,同时返回一个真正的java对象的引用
6+
intent = luajava.newInstance("android.content.Intent")
7+
--第一個參數是類名字,其他參數是這個類的構造函數需要的參數
8+
componentName = luajava.newInstance("android.content.ComponentName","com.example.zhangpeng.androidlua","com.example.zhangpeng.androidlua.UI.CreateViewAndAnimationActivity")
9+
intent:setFlags(intent.FLAG_ACTIVITY_NEW_TASK)
10+
intent:setComponent(componentName)
11+
context:startActivity(intent)
12+
end
13+
14+
15+
16+

app/src/main/assets/test_activity

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
function onCreate(activity)
2+
-- TODO 初始化
3+
activity:setTitle("这个界面完全由Lua编写")
4+
initUI(activity)
5+
addListener(activity)
6+
end
7+
8+
function onResume(activity)
9+
-- TODO
10+
end
11+
12+
function onActivityResult(activity, requestCode, resultCode, data)
13+
-- TODO
14+
end
15+
16+
function initUI(activity)
17+
local id = luajava.bindClass("com.nightfarmer.luabridge.sample.R$id")
18+
local layout = activity:findViewById(id.layout)
19+
20+
button2 = luajava.newInstance("android.widget.Button", activity)
21+
button2:setText("Lua初始化的button")
22+
layout:addView(button2)
23+
24+
local button3 = luajava.newInstance("android.widget.Button", activity)
25+
button3:setText("Lua创建动画")
26+
layout:addView(button3)
27+
--view 内部类 OnClickListener 用 $ 符号
28+
local button3Listener = luajava.createProxy("android.view.View$OnClickListener", button3_cb)
29+
button3:setOnClickListener(button3Listener)
30+
31+
button4 = luajava.newInstance("android.widget.Button", activity)
32+
button4:setText("Lua创建线程")
33+
layout:addView(button4)
34+
local button4Listener = luajava.createProxy("android.view.View$OnClickListener", button4_cb)
35+
button4:setOnClickListener(button4Listener)
36+
37+
end
38+
39+
button4_cb={}
40+
function button4_cb.onClick(eventView)
41+
local looper = luajava.bindClass("android.os.Looper")
42+
handler = luajava.newInstance("android.os.Handler", looper:getMainLooper())
43+
a = 1
44+
local thread_cb={run=function()
45+
--while(a<3) do
46+
local main_cb = {run=function()
47+
button4:setText("sss"..a)
48+
a=a+1
49+
end
50+
}
51+
local main_run = luajava.createProxy("java.lang.Runnable", main_cb)
52+
handler:post(main_run)
53+
--handler:postDelayed(main_run,1000)
54+
--local Thread = luajava.bindClass("java.lang.Thread")
55+
--Thread:sleep(1000)
56+
--end
57+
end
58+
}
59+
local runnable = luajava.createProxy("java.lang.Runnable", thread_cb)
60+
local thread = luajava.newInstance("java.lang.Thread", runnable)
61+
thread:start()
62+
end
63+
64+
65+
button3_cb={}
66+
function button3_cb.onClick(eventView)
67+
local Toast = luajava.bindClass("android.widget.Toast");
68+
Toast:makeText(eventView:getContext(),"按钮被点击,通过lua发起toast",0):show()
69+
70+
ObjectAnimator = luajava.bindClass("com.nightfarmer.luabridge.sample.ObjectAnimator")
71+
local animator = ObjectAnimator:ofFloat(button2, "rotation",0,360)
72+
animator:setDuration(1000)
73+
animator:start()
74+
end
75+
76+
function addListener(activity)
77+
local id = luajava.bindClass("com.nightfarmer.luabridge.sample.R$id")
78+
local launchBt = activity:findViewById(id.myLaunchButton)
79+
local buttonProxy = luajava.createProxy("android.view.View$OnClickListener", button_cb)
80+
launchBt:setOnClickListener(buttonProxy)
81+
end
82+
83+
button_cb = {}
84+
function button_cb.onClick(eventView)
85+
local Toast = luajava.bindClass("android.widget.Toast");
86+
Toast:makeText(eventView:getContext(),"按钮被点击,通过lua发起toast",0):show()
87+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.example.zhangpeng.androidlua;
2+
3+
import android.app.Application;
4+
5+
/**
6+
* good
7+
* Created by zhangpeng on 2018/3/25.
8+
*/
9+
10+
public class MainApplication extends Application {
11+
private static MainApplication mainApplication;
12+
@Override
13+
public void onCreate() {
14+
super.onCreate();
15+
mainApplication=this;
16+
}
17+
18+
public static MainApplication getMainApplication(){
19+
return mainApplication;
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.zhangpeng.androidlua;
2+
3+
/**
4+
* good
5+
* Created by zhangpeng on 2018/3/25.
6+
*/
7+
8+
public class ObjectAnimator {
9+
// 这里简单封装一层,因为 android.animation.ObjectAnimator ofFloat 方法传入参数为不定个数, 而 lua 调用的只需要传入固定两个参数
10+
// 否则直接调用会找不到方法
11+
public static android.animation.ObjectAnimator ofFloat(Object target, String propertyName,float v1,float v2) {
12+
return android.animation.ObjectAnimator.ofFloat(target, propertyName, v1, v2);
13+
}
14+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.zhangpeng.androidlua.UI;
2+
3+
import android.support.v7.app.AppCompatActivity;
4+
import android.os.Bundle;
5+
6+
import com.example.zhangpeng.androidlua.R;
7+
8+
public abstract class BaseActivity extends AppCompatActivity {
9+
10+
@Override
11+
protected void onCreate(Bundle savedInstanceState) {
12+
super.onCreate(savedInstanceState);
13+
setContentView(getContentViewId());
14+
initView();
15+
initListener();
16+
}
17+
18+
19+
abstract int getContentViewId();
20+
21+
protected abstract void initView();
22+
23+
protected abstract void initListener();
24+
}

0 commit comments

Comments
 (0)