autojs放大镜

2021-08-30  本文已影响0人  牙叔教程

牙叔教程 简单易懂

效果展示

效果.gif

缘起

群里有人问, 放大镜怎么写

环境

Autojs版本: 9.0.5

Android版本: 8.0.0

软件可能还有些许bug, 介意就别看了, 百度找别的教程去

思路

思路.png

你将学到以下知识点

难点

代码讲解

1. 主逻辑
let init = require("./init");
let showFloatingWindows = require("./showFloatingWindows");

// 判断悬浮窗和截图权限, 没有就退出
init();

showFloatingWindows();

let intervalId = setInterval(() => {}, 1000);

events.broadcast.on("退出悬浮窗", function (value) {
  if (value) {
    clearInterval(intervalId);
    setTimeout(function () {}, 1000);
  }
});
2. 权限判断
module.exports = function () {
  engines.all().map((ScriptEngine) => {
    if (engines.myEngine().toString() !== ScriptEngine.toString()) {
      ScriptEngine.forceStop();
    }
  });

  if (!$floaty.checkPermission()) {
    // 没有悬浮窗权限,提示用户并跳转请求
    toastLog("本脚本需要悬浮窗权限来显示悬浮窗,请在随后的界面中允许并重新运行本脚本。");
    $floaty.requestPermission();
    exit();
  } else {
    console.log("已有悬浮窗权限");
  }

  // 请求截图
  if (!requestScreenCapture()) {
    toastLog("请求截图失败");
    exit();
  } else {
    console.log("已有截图权限");
  }
};
3. 悬浮窗布局
let window = $floaty.rawWindow(
  <frame id="parentF" alpha="0.0">
    <custom-view
      ...
    ></custom-view>
    <horizontal gravity="bottom">
      <card cardCornerRadius="{{buttonViewRadiusWithUnit}}" cardElevation="0px">
        <img
          ...
        ></img>
      </card>

      <View w="{{blankViewSizeWithUnit}}"></View>
      <card cardCornerRadius="{{buttonViewRadiusWithUnit}}" cardElevation="0px">
        <img
          ...
        ></img>
      </card>
    </horizontal>
  </frame>
);
4. 悬浮窗点击事件
window.record.click(function () {
  let data = window.customView.updateData();
  toastLog("颜色已经记录\n" + data);
  setClip(data);
});
window.quit.click(function () {
  toastLog("退出拾色器");
  window.close();
  events.broadcast.emit("退出悬浮窗", true);
});
5. 显示悬浮窗
ui.post(function () {
  let windowWidth = window.getWidth();
  let windowHeight = window.getHeight();
  let x = dw / 2 - windowWidth / 2;
  let y = dh / 2 - windowHeight / 2;
  window.setPosition(x, y);
  setTimeout(function () {
    window.parentF.setAlpha(1);
    window.customView.updateRingColor("#ff0000");
    window.customView.refresh();
    setOnTouchListener(window, dw, dh, windowWidth, windowHeight);
  }, 300);
});
6. 放大镜onDraw
onDraw: function (canvas) {
  this.ringColor = getColor(this);
  paintOfTheOuterCircle.setShader(radialGradientOfTheOuterCircle);
  drawCircle(canvas, this);
  if (this.img) {
    drawImg(canvas, this);
    drawInfo(canvas, this);
  }
  drawCross(canvas, this);
},
7. 绘制十字架
canvas.drawRect(strokeRectFLeft, strokePaint);
canvas.drawRect(fillRectFLeft, fillPaint);

canvas.drawRect(strokeRectFRight, strokePaint);
canvas.drawRect(fillRectFRight, fillPaint);

canvas.drawRect(strokeRectFTop, strokePaint);
canvas.drawRect(fillRectFTop, fillPaint);

canvas.drawRect(strokeRectFBottom, strokePaint);
canvas.drawRect(fillRectFBottom, fillPaint);
8. 绘制圆环
canvas.drawCircle(view.centerX, view.centerY, view.radiusOfTheOuterCircle, view.paintOfTheOuterCircle);
9. 绘制图片
canvas.drawARGB(255, 0, 0, 255);
canvas.drawBitmap(bitmap, srcRect, destRect, paintOfImg);
10. 及时释放图片资源
updateImg: function (img) {
  let oldImg = this.img;
  let oldBitmap = this.bitmap;
  ui.post(function () {
    oldBitmap && oldBitmap.recycle();
    oldImg && oldImg.recycle();
  }, 100);
  this.img = img.clone();
  this.bitmap = this.img.getBitmap();
},

名人名言

思路是最重要的, 其他的百度, bing, stackoverflow, 安卓文档, autojs文档, 最后才是群里问问
--- 牙叔教程

声明

部分内容来自网络
本教程仅用于学习, 禁止用于其他用途

上一篇下一篇

猜你喜欢

热点阅读