autojs脚本引擎中scroll的使用方法

说明

本文提供的代码仅供参考。不建议用于生产环境。
可能有些地方在最新版本的Auto.js上面需要做修改,才能运行。

Auto.js简介

Auto.js是利用安卓系统的“辅助功能”实现类似于按键精灵一样,可以通过代码模拟一系列界面动作的辅助工作。
与“按键精灵”不同的是,它的模拟动作并不是简单的使用在界面定坐标点来实现,而是类似与win一般,找窗口句柄来实现的。

Auto.js使用JavaScript作为脚本语言,目前使用Rhino 1.7.7.2作为脚本引擎,支持ES5与部分ES6特性。

推荐教程

Auto.js Pro安卓全分辨率免ROOT引流脚本开发视频教程(HD超清1080p)

开发文档

Auto.js Pro开发文档
文档尚在完善中,可能有文档描述和代码实际行为有出入的情况。

为什么要使用Auto.js Pro开发脚本,有什么特点?

吸引我使用Auto.js Pro的原因有很多。最主要的几个原因是:

  • Auto.js Pro能开发免ROOT的安卓脚本
  • Auto.js Pro基于节点操作,能开发全分辨率的脚本,自动适配各种安卓机型
  • Auto.js Pro丰富的UI组件,能自定义各种样式的安卓界面
  • Auto.js Pro使用的javascript的语法比较优雅,代码可读性强
  • Auto.js Pro的命令库非常的丰富,接口比较多
  • Auto.js Pro脚本文件体积比较小。1000行的代码,打包后的apk文件只有3-5M,还没有广告

示例代码

//此代码由飞云脚本圈整理提供(www.feiyunjs.com)
/**
 * 作者: 家
 * 功能:  scroll的使用
 */
"ui";
// scrollView.scrollTo(0, scrollView.getChildAt[0].getMeasuredHeight() - scrollView.getHeight());
importClass(android.widget.TextView)
importClass(android.widget.Button)
ui.layout(
  <vertical id="帮主"  layout_width="wrap_content" layout_gravity="fill_horizontal">
  <vertical>
  <horizontal>
    <button id="管家" textSize="16sp" margin="8" gravity="center" >管家加人</button>
    <button id="白夫人" textSize="16sp" margin="8" gravity="center" >白夫人减人</button>
    <button id="乔峰" textSize="16sp" margin="8" gravity="center" >乔峰加布局</button>
  </horizontal>
  <horizontal>
    <button id="邓紫棋" textSize="16sp" margin="8" gravity="center" >增加泡沫按钮</button>
    <button id="邓紫棋的泡沫" textSize="16sp" margin="8" gravity="center" >遍历显示泡沫按钮</button>
  </horizontal>
  </vertical>
  <scroll id='scroll'  fillViewport="true">
  <vertical id="scrollVertical"  focusable="true"  focusableInTouchMode="true">
  </vertical>
  </scroll>
  </vertical>
)
var num=function(){
  var current=0
  return function(){
    return current++
  }
}()
var 泡沫列表=[]
function 遍历显示所有泡沫按钮(泡沫列表){
  var scrollView=ui.scroll
  threads.start(
    function(){
      泡沫列表.map(
        (泡沫)=>{
          ui.run(
            function(){
              泡沫.attr('bg',getRndColor())
              scrollView.scrollTo(0,  泡沫.getTop());
            }
          )
          sleep(800)
        }
      )
    }
  )
}
function 到底(){
  var scrollView=ui.scroll
  scrollView.scrollTo(0, ui.scrollVertical.getMeasuredHeight() - scrollView.getHeight());
}
function 到顶(){
  var scrollView=ui.scroll
  scrollView.scrollTo(0, 0);
}
var 帮主 = ui.scrollVertical
// var 帮主 = ui.帮主
ui.邓紫棋的泡沫.click(
  () => {
    遍历显示所有泡沫按钮(泡沫列表)
  }
)
ui.邓紫棋.click(
  () => {
    addButtonView(帮主)
    到底()
  }
)
ui.管家.click(
  () => {
    addTextView(帮主)
    到底()
  }
)
ui.白夫人.click(
  () => {
    delView(帮主)
    到顶()
  }
)
ui.乔峰.click(
  () => {
    log('加布局开始')
    addLayout(帮主)
    log('加布局结束')
    到底()
  }
)
function delView(parent) {
  log('得不到的就毁掉它')
  parent.removeView(parent.getChildAt(1))
}
function addTextView(parent) {
  // var child = view
  var child = new TextView(context);
  child.setTextSize(20);
  child.setTextColor(colors.parseColor("#ff00f0"))
  child.setText("左护法");
  child.setGravity(0); //左护法
  parent.addView(child);
  log(child)
  var child = new TextView(context);
  child.setTextSize(20);
  child.setTextColor(colors.parseColor("#ff00f0"))
  child.setText("大长老"); //中间的是大长老
  child.setGravity(1);
  parent.addView(child);
  log(child)
  var child = new TextView(context);
  child.setTextSize(20);
  child.setTextColor(colors.parseColor("#ff00f0"))
  child.setText("右护法");
  child.setGravity(5); //右护法
  parent.addView(child);
  log(child)
}
function addButtonView(parent) {
  // var child = view
  var child = new Button(context);
  child.setTextSize(20);
  child.setTextColor(colors.parseColor("#ff00f0"))
  child.setText("泡沫"+num()); //中间的是大长老
  child.setGravity(1);
  parent.addView(child);
  log(child)
  泡沫列表.push(child)
}
function addLayout(parent) {
  var layo = new android.widget.LinearLayout(context)
  // var layo=android.widget.FrameLayout.LinearLayout(context)
  layo.setOrientation(android.widget.LinearLayout.HORIZONTAL);
  layo.setId(android.view.View.generateViewId())
  // layo.setOrientation( android.widget.FrameLayout.LinearLayout.VERTICAL );
  var child1 = new TextView(context);
  child1.setTextSize(20);
  child1.setTextColor(colors.parseColor("#ff00f0"))
  child1.setText("左护法");
  child1.setGravity(0); //左护法
  child1.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, 1));
  // child1.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, 0, 1));
  var child2 = new TextView(context);
  child2.setTextSize(20);
  child2.setTextColor(colors.parseColor("#ff00f0"))
  child2.setText("大长老"); //中间的是大长老
  child2.setGravity(1);
  child2.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, 1));
  var child3 = new TextView(context);
  child3.setTextSize(20);
  child3.setTextColor(colors.parseColor("#ff00f0"))
  child3.setText("右护法");
  child3.setGravity(5); //右护法
  child3.setLayoutParams(new android.widget.LinearLayout.LayoutParams(0, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, 1));
  layo.addView(child1)
  layo.addView(child2)
  layo.addView(child3)
  parent.addView(layo)
}
// mView.setLayoutParams(new android.widget.LinearLayout.LayoutParams(
//   android.widget.LinearLayout.LayoutParams.MATCH_PARENT, 0, 1));
// 第一个参数是width,第二个参数是height,第三个参数是weight
// 如果orientation是vertical,width就是0,如果orientation是horizontal,height就是0
//===============公用函数============================
function getRndColor() {
  var a, r, g, b;
  a = Math.floor(0), r = Math.floor(随机0_255()), g = Math.floor(随机0_255()), b = Math.floor(随机0_255());
  // var 反色 = -1 - colors.argb(0, r, g, b);
  var color = colors.argb(0, r, g, b);
  color = colors.toString(color)
  log(color)
  return color
}
function 随机0_255() {
  var r = parseInt(255 * Math.random())
  return r
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值