Android App运行核心,Handler,Looper,Message_android app looper message(2)

  1. 读取图片
let imgFilepath = files.path("./chess.png");
let img = images.read(imgFilepath); 

  1. 设置绘制区域
let rect = {
  left: 52,
  top: 20,
  right: 138,
  bottom: 96,
}; 

  1. 设置画笔属性: 绿色, 空心
let paint = new Paint();
paint.setStrokeWidth(9);
paint.setColor(colors.parseColor("#00ff00"));
paint.setStyle(Paint.Style.STROKE); 

  1. 在图片上画矩形
var canvas = new Canvas(img);
canvas.drawRect(left, top, right, bottom, paint); 

  1. 把canvas的数据转成图片
var image = canvas.toImage();
let filePath = "/sdcard/脚本/showRect.png";
files.createWithDirs(filePath);
images.save(image, filePath); 

  1. 回收资源
image.recycle();
img.recycle(); 

  1. 查看图片
app.viewFile(filePath); 

在全屏透明悬浮窗上直接画矩形, 圆形, 文字

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4IplbkEj-1653470466691)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/97542b35b48c4f8bae83178b5e2a29ce~tplv-k3u1fbpfcp-zoom-in-crop-mark:1956:0:0:0.image)]

使用示例

let Board = require("./board");
let board = new Board();
setTimeout(() => {
  board.drawText({
    x: 100,
    y: 200,
    text: "Hello World",
  });
  sleep(1000);
  board.setPaintColor("#00ff00");
  board.drawCircle({
    x: 200,
    y: 400,
    radius: 100,
  });
  sleep(1000);
  board.setPaintColor("#0000ff");
  board.drawRect({
    left: 400,
    top: 300,
    right: 600,
    bottom: 500,
  });
}, 1000); 

把绘制方法封装成了一个模块Board

  1. 检查悬浮窗权限
function ensureFloatyPermission() {
  if (floaty.checkPermission()) {
    return true;
  }
  toastLog("请授予悬浮窗权限");
  app.startActivity({
    action: "android.settings.action.MANAGE_OVERLAY_PERMISSION",
    data: "package:" + context.packageName,
  });
  throw new Error("请授予悬浮窗权限");
} 


  1. 创建悬浮窗
function createFloatyWindow() {
  let floatyWindow = floaty.rawWindow(
    <frame bg="#88ff0000">
      <canvas id="board" w="*" h="*" />
    </frame>
  );
  ui.run(function () {
    floatyWindow.setSize(-1, -1);
    floatyWindow.setPosition(0, 0);
    let touchable = false;
    floatyWindow.setTouchable(touchable);
  });
  return floatyWindow;
} 


  1. 创建画笔
function createPaint() {
  paint = new Paint();
  paint.setStyle(Paint.Style.FILL);
  paint.setTextSize(100);
  return paint;
} 


  1. 设置画板draw事件
board.on("draw", (canvas) => {
  canvas.drawColor(android.graphics.Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);


## 最后

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

当然我也为你们整理好了百度、阿里、腾讯、字节跳动等等互联网超级大厂的历年面试真题集锦。这也是我这些年来养成的习惯,一定要学会把好的东西,归纳整理,然后系统的消化吸收,这样才能极大的提高学习效率和成长进阶。碎片、零散化的东西,我觉得最没有价值的。就好比你给我一张扑克牌,我只会觉得它是一张废纸,但如果你给我一副扑克牌,它便有了它的价值。这和我们收集资料就要收集那些系统化的,是一个道理。

![](https://img-blog.csdnimg.cn/img_convert/7552034f7490095d581b418d1ae9f286.webp?x-oss-process=image/format,png)

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 ALoop、AHandler 和 AMessage 的 C++ 代码示例: ```c++ #include <android/looper.h> #include <android/native_activity.h> #include <android/log.h> #define APP_NAME "ALoopExample" enum { MSG_HELLO = 1, MSG_BYE, }; class ALooperExample { public: ALooperExample(ANativeActivity* activity) : m_activity(activity) { // 创建一个 ALooper 对象,并且运行在当前线程中 m_looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS); // 创建一个 AHandler 对象,并且关联到当前线程的 ALooper 对象中 m_handler = new AHandler(m_looper, this); } ~ALooperExample() { delete m_handler; } void run() { // 发送一个 MSG_HELLO 消息 m_handler->sendMessage(m_handler->obtainMessage(MSG_HELLO)); // 开始循环处理消息 ALooper_pollAll(-1, NULL, NULL, NULL); } void handleMessage(const AMessage& msg) { switch (msg.what()) { case MSG_HELLO: __android_log_write(ANDROID_LOG_INFO, APP_NAME, "Hello"); // 发送一个 MSG_BYE 消息 m_handler->sendMessage(m_handler->obtainMessage(MSG_BYE)); break; case MSG_BYE: __android_log_write(ANDROID_LOG_INFO, APP_NAME, "Bye"); // 结束消息循环 ALooper_wake(m_looper); break; default: break; } } private: ANativeActivity* m_activity; ALooper* m_looper; AHandler* m_handler; }; void android_main(ANativeActivity* activity) { ALooperExample example(activity); example.run(); } ``` 在这个示例代码中,我们首先创建了一个 `ALooperExample` 类,它包含了一个 `ALooper` 对象、一个 `AHandler` 对象和一个消息处理函数 `handleMessage`。在 `ALooperExample` 的构造函数中,我们创建了一个 `ALooper` 对象,并且将其准备为当前线程的消息循环。然后,我们创建了一个 `AHandler` 对象,并且将其关联到 `ALooper` 对象中。在 `run` 函数中,我们发送了一个 `MSG_HELLO` 消息,然后开始循环处理消息,直到收到 `MSG_BYE` 消息为止。在 `handleMessage` 函数中,我们根据不同的消息类型进行不同的处理,然后再次发送一个消息或者结束消息循环。 在 `android_main` 函数中,我们创建了一个 `ALooperExample` 对象,并且调用了它的 `run` 函数。这样,我们就创建了一个包含消息循环的 Android 应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值