fusion app网页转app教程——高级篇

姿势论坛app: https://bbs.zsxwz.com/thread-839.htm

分享几个有意思的代码。

安装腾讯x5内核: https://zsxwz.com/2019/02/03/fusion-app安装腾讯x5内核

1、清理缓存,设置一个点击事件。

os.execute("pm clear "..this.packageName)
弹出消息("清除缓存成功")

2、圆角侧滑栏,放在启动事件当中就可以了。侧滑栏宽度和高度可以自行修改。

import "java.io.File"  import "android.text.format.Formatter"--获取文件夹大小function getFolderSize(folderPath,conversion)  import "java.io.*"  local size = 0  local fileList = luajava.astable(File(folderPath).listFiles())  if(fileList == nil) then    return 0  end  --开始遍历循环获取文件夹底下所有文件的字节大小  if(fileList ~= nil) then    for count=1,#fileList do      if(File(tostring(fileList[count])).isDirectory()) then        size = size + getFolderSize(tostring(fileList[count]))      else        local singleFileSize = File(tostring(fileList[count])).length()        size = size + singleFileSize      end    end  end    --字节换算  if(conversion == true) then    local GB = 1024 * 1024 * 1024;--定义GB的计算常量    local MB = 1024 * 1024;--定义MB的计算常量    local KB = 1024;--定义KB的计算常量    local countResult = ""    if(size / GB >= 1) then      --如果当前Byte的值大于等于1GB      countResult = string.format("%.2f",size / GB).."GB"      return countResult    elseif (size / MB >= 1) then      --如果当前Byte的值大于等于1MB      countResult = string.format("%.2f",size / MB).."MB"      return countResult    elseif (size / KB >= 1) then      --如果当前Byte的值大于等于1KB      countResult = string.format("%.2f",size / KB).."KB"      return countResult    else      countResult = size.."B"      return countResult    end  elseif(conversion == nil or conversion == false) then    return size  endend--x5视频缓存大于1000m自动清除if File("/sdcard/Android/data/"..this.packageName.."/files/VideoCache/main/").exists() or File("storage/emulated/0/Android/data/"..this.packageName.."/files/VideoCache/main/").exists()then  if (getFolderSize("/sdcard/Android/data/"..this.packageName.."/files/VideoCache/main/",false)>1000000000) or (getFolderSize("/storage/emulated/0/Android/data/"..this.packageName.."/files/VideoCache/main/",false)>1000000000) then    执行Shell("rm -rf /sdcard/Android/data/"..this.packageName.."/files/VideoCache/main/")    执行Shell("rm -rf /storage/emulated/0/Android/data/"..this.packageName.."/files/VideoCache/main/")  endend

function CircleButton(view,InsideColor,radiu)
  import "android.graphics.drawable.GradientDrawable"
  drawable = GradientDrawable() 
  drawable.setShape(GradientDrawable.RECTANGLE) 
  drawable.setColor(InsideColor)
  drawable.setCornerRadii({radiu,radiu,radiu,radiu,radiu,radiu,radiu,radiu});
  view.setBackgroundDrawable(drawable)
end
角度=30--圆角弧度
控件id=sidebar
控件颜色=0xFFFFFFFF
CircleButton(控件id,控件颜色,角度)

linearParams = sidebar.getLayoutParams()
linearParams.width =360--侧滑栏宽度
linearParams.height =900--侧滑栏高度
sidebar.setLayoutParams(linearParams)

flt=sidebar.getLayoutParams( )
flt.setMargins(0, 300, 0, 0)
sidebar.setLayoutParams(flt)
--上下左右

--此处以悬浮按钮为例,id为fltBtn
--flt=fltBtn.getLayoutParams( )
--flt.setMargins(1, 3, 1, 4)
--fltBtn.setLayoutParams(flt)
--边距dp值顺序:左,上,右,下


--tz=id.getLayoutParams()
--tz.setMargins(x,y,x2,y2)
--id.setLayoutParams(tz)
--调整y和y2
--y是顶,y2是底

screenWidth=activity.getWidth()
screenHeight=activity.getHeight()
lp=sidebar.getLayoutParams()
sidebar.setOnTouchListener(View.OnTouchListener{onTouch=function(v,e)
    if e.getAction() == MotionEvent.ACTION_DOWN then
      ry=e.getRawY()
      rx=e.getRawX()
    elseif e.getAction() == MotionEvent.ACTION_MOVE then
      dx=e.getRawX()-rx
      dy=e.getRawY()-ry
      top = v.getTop() + dy
      left = v.getLeft() + dx
      if top<=0 then
        top=0
      end
      if top >= screenHeight - fltBtn.getHeight() then
        top = screenHeight - fltBtn.getHeight()
      end
      if left >= screenWidth - fltBtn.getWidth() then
        left = screenWidth - fltBtn.getWidth()
      end

      if left <= 0 then
        left = 0
      end
      lp.leftMargin = left
      lp.topMargin = top
      sidebar.setLayoutParams(lp)
      ry=e.getRawY()
      rx=e.getRawX()
    end
    return true
  end
--以上为圆角对话框

})

titleTvw.onLongClick=function()
  --输入对话框 
  InputLayout={
    LinearLayout;
    orientation="vertical";
    Focusable=true,
    FocusableInTouchMode=true,
    {
      TextView;
      id="Prompt",
      textSize="15sp",
      layout_marginTop="10dp";
      layout_marginLeft="3dp",
      layout_width="80%w";
      layout_gravity="center",
      text="将加载链接:";
    };
    {

      EditText;
      hint="请输入完整链接:";
      layout_marginTop="5dp";
      layout_width="80%w";
      layout_gravity="center",
      id="edit";
      text=webView.getUrl();
    };
  };

  AlertDialog.Builder(this)
  .setTitle(webView.title)
  .setView(loadlayout(InputLayout))
  .setPositiveButton("确定",{onClick=function(v) 
      加载网页(edit.text)
    end})
  .setNegativeButton("取消",nil)
  .show()
  import "android.view.View$OnFocusChangeListener"
  edit.setOnFocusChangeListener(OnFocusChangeListener{ 
    onFocusChange=function(v,hasFocus)
      if hasFocus then
        Prompt.setTextColor(0xFD009688)
      end
    end})

end

3、浏览器打开分享网页,设置点击事件。

分享文本(webView.getUrl())

4、去除顶部空白,有些网页删除一些空间之后顶部会有空白。scrollTop = 值,可以自己根据网页情况修改。放在启动事件当中。

javascript: if (document.getElementsByTagName('BODY')[0].scrollTop < 76) {
    document.getElementsByTagName('BODY')[0].scrollTop = 76;
} else {
    return false;
}

5、自适应手机屏幕,有些网页只有电脑版,需要自适应手机屏幕,方便浏览。放在启动文件当中。

javascript: var sc = document.createElement("meta");
sc.setAttribute("name", "viewport");
sc.setAttribute("content", "width=1000px, initial-scale=1, user-scalable=yes");
document.head.appendChild(sc);

6、检查更新。设置启动事件。

function 提示(内容)
  Toast.makeText(this, 内容,Toast.LENGTH_SHORT).show()
end

function 检测更新(Github地址)
  local dl=ProgressDialog.show(activity,nil,'更新检测中…')
  dl.show()
  local tt=Ticker()
  tt.start()
  packinfo=this.getPackageManager().getPackageInfo(this.getPackageName(),((1552294270/8/2-8392)/32/1250-25.25)/8-236)
  version=tostring(packinfo.versionName)
  versioncode=tostring(packinfo.versionCode)

  url=Github地址;
  function 过滤(content)
    版本名=content:match("【版本名】(.-)【版本名】")
    版本=content:match("【版本】(.-)【版本】")
    内容=content:match("【内容】(.-)【内容】")
    链接=content:match("【链接】(.-)【链接】")
    if(版本名==nil) then
      版本名="获取失败"
    end
    if(版本==nil) then
      版本="0"
    end
    if(内容==nil) then
      内容="获取失败"
    end
    if(链接==nil) then
      提示("服务器参数配置错误,请过段时间再次尝试")
    end

    if(版本 > versioncode) then
      dl.dismiss()
      tt.stop()
      对话框()
      .设置标题("检测到更新")
      .设置消息("版本:"..version.."→"..版本名.."\n更新内容:"..内容)
      .设置积极按钮("下载更新",function()
        下载文件(链接)
        提示("下载更新中…")
      end)
      .设置消极按钮("取消更新")
      .显示()
    else
      dl.dismiss()
      tt.stop()
      提示("当前已是最新版本!")
    end
  end
  Http.get(url,nil,"UTF-8",nil,function(code,content,cookie,header)
    if(code==200 and content)then
      过滤(content)
    else
      dl.dismiss()
      tt.stop()
      提示("本地网络或服务器异常 "..code)
    end
  end)
end

检测更新("https://远程服务器网址/app.txt")

远程服务器创建app.txt。

【版本名】1.1.1【版本名】【版本】1【版本】【内容】无 【内容】【链接】更新链接【链接】

7、分享一个ua,对于屏蔽一些网页广告还是用得上的,当然也不可能全部屏蔽。

Mozilla/5.0 (BlackBerry; U; BlackBerry 9700; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/6.0.0.448 Mobile Safari/534.8+

留言

* - 必填