GithubHelp home page GithubHelp logo

hsllany / htmlnative Goto Github PK

View Code? Open in Web Editor NEW
460.0 9.0 95.0 70.66 MB

:page_with_curl:Use HTML/CSS to render Android View, Lua to control its logic (Not Webview)

License: Apache License 2.0

Java 99.15% HTML 0.85% Lua 0.01%
android html web mobile dynamic android-library android-sdk

htmlnative's Introduction

HtmlNative

English

使用Html来渲染Android界面的动态化界面方案。

不同于Webview, HtmlNative直接将Html和Css转换成原生控件,这样做的好处是:

  • 满足部分定制化的场景需求,客户端无需发版,通过服务器渲染HTML+CSS,客户端展现即可
  • 不同于WebView,原生控件让界面可以能达到更好的体验效果,且用户无感知
  • 客户端可以自定义HTML标签和对应控件,灵活开发
  • 简易部署,使用最容易的html + css来控制界面
  • 此外,还可以通过内嵌在html<script>标签中的 Lua 脚本,来控制整个动态化控件的部分行为。其中集成了部分API,构成HNLua API.

远景目标是把JavaScript通过V8引擎集成,这样就完成了对整个H5技术栈的模拟

Html支持情况

<a>, <p>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <input>, <img>, <div>, <br>, <iframe>

属性:style, onclick(需要再lua脚本中定义onclick事件,后续完善相关文档), id, class, href(for <a>), src(for <img>)

css支持情况

支持内联style属性,及<head><style>标签的内嵌css。不支持链接形式的css样式文件。

支持的css样式

width, height, background, background-color, background-position, background-size, padding, padding-left, padding-right, padding-top, padding-bottom, margin, margin-top, margin-left, margin-right, margin-bottom, visibility

font-size, color, line-height, font-style, font-weight, text-align, word-spacing, text-overflow, text-transform

此外,支持从html中获取颜色和drawable,使用@开头即可,例如:

background: url(@pic);
color: @colorPrimary;

支持的选择器

type选择器

p{}
div{}

id选择器
#id1{}

class选择器
.class1{}

组
#id1, #id2{}

子孙选择器
div p{}

子选择器
div > p{}

全部选择器
*{}
p *{}

例子

通过HtmlNative渲染后的结果:

Download Video

Demo Demo Demo

Html:

<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <meta name="viewport"
          content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1"/>
    <style>
	html{
		font-size: 62.5%;

	}

	body{
		margin: 0;
		padding: 0;
		color: #3f484f;
	}

	.header{
	    color:#3f484f;
		padding: 2em;
		background: #db3541;
		-hn-background-color-size: 1.2em 100%;

	}

	.article{
	    padding: 3em
	 }

	 p{
	    padding: 5em 0;
	    font-size: 4em;
	    line-height: 120%
	  }
    </style>
    <title>article</title>
</head>
<body>
<div class="header">
    <span>头条 | </span>
    <a href="http://www.sina.com.cn/">NewsWebSite</a>
    <h1>滴滴在硅谷建了研发中心做无人驾驶,还从 Uber 挖了人</h1>
    <span>来源:新闻网 2017-05-09</span>
</div>

<div class="article">
    <h2>这是滴滴在海外的第一个实验室</h2>
    <p>周四,滴滴正式在美国加州山景城宣布了滴滴美国研究院的成立,一个由数十位工程师和研究人员组成的团队,已经入驻这里,从大数据安全和无人驾驶两个方向展开研究。</p>

    <div>
        <img src="http://www.abc.com/pic.jpg" alt="新闻说明"/>
        <span>滴滴美国研究所</span>
    </div>

    <img src="http://www.abc.com/pic.jpg"/>

    <p>美国研究院由滴滴研究院副院长弓峰敏带领。弓峰敏是去年 9 月加入滴滴,担任滴滴信息安全战略副总裁和滴滴研究院副院长,并开始组建美国研究院。加盟滴滴之前,他是安全公司 Palo Alto
        Networks 的联合创始人。</p>
        
    <p>这一次,滴滴还从Uber挖来了查理·米勒(Charlie Miller)。米勒被誉为全球顶级安全专家,曾是一名黑客,从事苹果越狱和安全研究,在加入 Uber
        之前,米勒受雇于苹果。2015 年,米勒完成了对一辆吉普切诺基的远程控制实验。2015 年 9 月,他加入Uber担任汽车安全工程师。</p>

    <p>在Twitter上,米勒表示他的工作是帮助滴滴研发和使用的自动驾驶系统抵抗来自外部的攻击和威胁。在宣布加入滴滴美国研究院的消息之后,他又发布推文表示实验室正在招人。</p>
</div>

<div>
    <img src="coop/banner.png"/>
</div>
</body>
</html>

使用方法:

InputStream htmlSource = ...;

// 在需要加载view的时候加载
HNativeEngine.getInstance().loadView(mActivity, htmlSource, new
                    HNativeEngine.OnHNViewLoaded() {

    @Override
    public void onViewLoaded(View v) {
        if (mActivity != null && !mActivity.isDestroyed()) {
            mActivity.setContentView(v);
        }
    }

    @Override
    public void onError(Exception e) {

    }

    @Override
    public void onHead(HNHead head) {
        
    }
});

HNLua API

HNLua API是一套定义在HtmlNative框架下的精简API,通过该API,可以灵活的控制界面、调整样式、跳转url、以及有限地访问部分Android的基础能力。

例子:

<html>
    <head>
        <title>iframe</title>
        <meta name="nihao" content="world"/>
        <style>
            #text1{
                background:red;
                color:green;
                margin: 3em 3em;
                padding: 2em;
            }

            button{
                color:yellow
            }

            #parent{
                color: red;
             }

        </style>
    </head>

    <body>
        <text id="text1">This is an demo of iframe<br/>nihao
        </text>
        <button onclick="changeText1">click me</button>
    <div id="parent" class="claz1 claz2 claz3"></div>
    </body>

    <script type="text/lua">
        toast("hello world")

        b = false

        local vv = document.createView("p", "")
        vv.setAttribute("text:helloworld");

        local parent = getElementById("parent")

        parent.appendChild(vv)

        console.log(vv.toString())
        console.log(vv.getAttribute("background"));

        function changeText1()
            local v = getElementById("text1")
            console.log(v.id())
            if(b) then
                v.setAttribute("background:red;color:#fff")
            else
                v.setAttribute("background:blue;color:red")
            end
            b = not(b)

            document.jump("http://www.baidu.com")

        end
    </script>
</html>

以上示例,使用HNLuaAPI, 动态添加了一个生成了一个<p>,并设置了其text属性,最终添加到id=parent<div>中;

此外,在html中有一段<button onclick="changeText1">click me</button>,为<button>控件设置了一个点击事件,事件的行为,在下面由函数changeText1()定义:通过变量b的值,动态改变其背景色和文字颜色;并跳转链接。

目前支持的HNLuaApi(不断完善中):

--log相关,会以HNConsole的标签出现在Android Logcat中
console.log(msg)
console.error(msg)
console.info(msg)
console.warn(msg)

--全局操作
document.version()	--获取当前HNLuaApi版本号
document.jump(url) 	--跳转至url,具体处理方式,客户端可复写HrefLinkHandler,并在HNativeEngine中注册
doucment.createView(tag, style) --以tag方式创建一个标签,并附加上style属性。例如local v = document.createElement("p", "color:red"),该函数返回一个lview对象

--查找view
local lview = document.getElementById(id) --通过id查找view

--lview对象
lview.toString()	--转换成stirng
lview.setAttribute(style)	--设置样式,例如lview.setAttribute("color:red")
lview.id()	--获取该标签的id
lview.className()	--获取该标签的class,返回为一个LuaTable类型对象
lview.appendChild(lviewChild)	--往该lview中添加一个子view到末尾
lview.insertBefore(lviewChild)	--往该lview中添加一个子view到最前面
lview.removeChild(lviewChild)	--删除view
lview.childNodes()	--返回该view的所有子view
lview.getAttribute(stylename)	--返回某个属性名为stylename的属性值
lview.tagName()	--返回该view的tag标签名称
lview.hasChildNode() --检验该lview是否含有子view
lview.setText(textString) --设置文本

--toast
toast("helloworld")	--显示一个toast提示(android only)

--网络操作

local callback = {}        --定义一个回调函数
function l.onResponse(res) --定义其onResponse方法
    toast(res.body())      --在其中发起一个toast,并显示response的body中的内容。res有body(), header()及status()方法,可分别访问其HTTP BODY,RESPONSE HEADER及状态码
end

http.get("http://www.abc.com", callback)
http.post("http://www.abc.com", postParams, callback)

License

Copyright 2016 Hsllany. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

htmlnative's People

Contributors

hsllany avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

htmlnative's Issues

lua script crash cause main app crash

06-01 11:40:40.249 13598-13615/com.mozz.htmlnativedemo E/AndroidRuntime: FATAL EXCEPTION: HNScriptRunner
                                                                         Process: com.mozz.htmlnativedemo, PID: 13598
                                                                         org.luaj.vm2.LuaError:     local idTxt = document.getElementById("result")
                                                                         
                                                                             function sendGet()
                                                                                 local l = {};
                                                                         
                                                                                 function l.onResponse(res)
                                                                                     idTxt.setAttrbute("text:"..res.body())
                                                                                 end
                                                                         
                                                                                 http.get("http://www.w3school.com.cn/", l)
                                                                             end
                                                                         
                                                                             function sendPost()
                                                                         
                                                                             end
                                                                         
                                                                         :7 attempt to call nil
                                                                             at org.luaj.vm2.LuaValue.checkmetatag(LuaValue.java:3365)
                                                                             at org.luaj.vm2.LuaValue.callmt(LuaValue.java:1997)
                                                                             at org.luaj.vm2.LuaValue.call(LuaValue.java:1450)
                                                                             at org.luaj.vm2.LuaClosure.execute(LuaClosure.java:357)
                                                                             at org.luaj.vm2.LuaClosure.call(LuaClosure.java:142)
                                                                             at com.mozz.htmlnative.script.lua.LHttp$1$1$1.run(LHttp.java:56)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.os.HandlerThread.run(HandlerThread.java:61)

大佬厉害!问下还更新嘛?

大佬您好,想问下这个项目后续还更新嘛?
我是一个RSS APP的作者,做了两年了,苦于技术无能一直在用Web View,一直想找一个原生渲染HTML的方案,今天终于找到了。我特别想用大佬的 Html Native 方案替代 Web view。但是看到 2017 年就不更新了感觉十分遗憾呀!
想问下大佬这个项目是怎么处理大量图片时的OOM问题呢,
另外想问问大佬未来有没有想法继续更新这个项目呢,期待回复

aide编译

我用aide编译,但是没有通过。报了各种错误。然后还有一次编译过了,软件打不开,估计编译错了。。。能给一个预编译版本吗。。。

Crash: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference

08-28 20:04:58.524 12413-12413/com.mozz.htmlnativedemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mozz.htmlnativedemo, PID: 12413
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equalsIgnoreCase(java.lang.String)' on a null object reference
at com.mozz.htmlnative.HtmlTag.isGroupingElement(HtmlTag.java:86)
at com.mozz.htmlnative.HNRenderer.createView(HNRenderer.java:212)
at com.mozz.htmlnative.HNRenderer.renderInternal(HNRenderer.java:153)
at com.mozz.htmlnative.HNRenderer.render(HNRenderer.java:116)
at com.mozz.htmlnative.HNRenderThread$RenderTask$1.run(HNRenderThread.java:65)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.