.class public Lcom/example/floatingball/FloatingBallService;
.super Ljava/lang/Object;
.source "FloatingBallService.java"
# static fields
.field private static floatingBall:Landroid/view/View;
.field private static windowManager:Landroid/view/WindowManager;
.field private static initialX:I
.field private static initialY:I
.field private static initialTouchX:F
.field private static initialTouchY:F
# direct methods
.method public static constructor <clinit>()V
.registers 1
const/4 v0, 0x0
sput-object v0, Lcom/example/floatingball/FloatingBallService;->floatingBall:Landroid/view/View;
sput-object v0, Lcom/example/floatingball/FloatingBallService;->windowManager:Landroid/view/WindowManager;
sput v0, Lcom/example/floatingball/FloatingBallService;->initialX:I
sput v0, Lcom/example/floatingball/FloatingBallService;->initialY:I
return-void
.end method
.method public static constructor <init>()V
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method
.method public static start(Landroid/content/Context;)V
.registers 10
.local p0, "context":Landroid/content/Context;
# 检查是否已创建
sget-object v0, Lcom/example/floatingball/FloatingBallService;->floatingBall:Landroid/view/View;
if-nez v0, :end
# 获取 WindowManager
const-string v0, "window"
invoke-virtual {p0, v0}, Landroid/content/Context;->getSystemService(Ljava/lang/String;)Ljava/lang/Object;
move-result-object v0
check-cast v0, Landroid/view/WindowManager;
sput-object v0, Lcom/example/floatingball/FloatingBallService;->windowManager:Landroid/view/WindowManager;
# 创建 ImageView
new-instance v1, Landroid/widget/ImageView;
invoke-direct {v1, p0}, Landroid/widget/ImageView;-><init>(Landroid/content/Context;)V
sput-object v1, Lcom/example/floatingball/FloatingBallService;->floatingBall:Landroid/view/View;
# 设置图标 (系统内置)
const v2, 0x108009b # android.R.drawable.ic_dialog_info
invoke-virtual {v1, v2}, Landroid/widget/ImageView;->setImageResource(I)V
# 创建 LayoutParams
const/4 v2, 0x64 # 100px
invoke-static {}, Landroid/os/Build$VERSION;->getSdkInt()I
move-result v3
const/16 v4, 0x18 # API 24
if-lt v3, v4, :type_phone
const/16 v3, 0x1000024 # TYPE_APPLICATION_OVERLAY
goto :set_type
:type_phone
const/16 v3, 0x00000022 # TYPE_PHONE
:set_type
const/high16 v4, 0x00000040 # FLAG_NOT_FOCUSABLE | FLAG_LAYOUT_IN_SCREEN
invoke-static {v2, v2, v3, v4, -0x3}, Landroid/view/WindowManager$LayoutParams;-><init>(IIIILint;)V
move-result-object v3
# Gravity: TOP | LEFT
const/16 v4, 0x0050
invoke-virtual {v3, v4}, Landroid/view/WindowManager$LayoutParams;->setGravity(I)V
# 设置宽高
invoke-virtual {v3, v2}, Landroid/view/WindowManager$LayoutParams;->setWidth(I)V
invoke-virtual {v3, v2}, Landroid/view/WindowManager$LayoutParams;->setHeight(I)V
# 设置触摸监听
new-instance v4, Lcom/example/floatingball/FloatingBallService$TouchListener;
invoke-direct {v4}, Lcom/example/floatingball/FloatingBallService$TouchListener;-><init>()V
invoke-virtual {v1, v4}, Landroid/view/View;->setOnTouchListener(Landroid/view/View$OnTouchListener;)V
# 添加到窗口
sget-object v4, Lcom/example/floatingball/FloatingBallService;->windowManager:Landroid/view/WindowManager;
invoke-interface {v4, v1, v3}, Landroid/view/WindowManager;->addView(Landroid/view/View;Landroid/view/ViewGroup$LayoutParams;)V
:end
return-void
.end method
# 内部类 TouchListener
.class public Lcom/example/floatingball/FloatingBallService$TouchListener;
.super Ljava/lang/Object;
.implements Landroid/view/View$OnTouchListener;
.method public constructor <init>()V
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method
.method public onTouch(Landroid/view/View;Landroid/view/MotionEvent;)Z
.registers 10
.local p2, "event":Landroid/view/MotionEvent;
invoke-virtual {p2}, Landroid/view/MotionEvent;->getAction()I
move-result v0
packed-switch v0, :sw_data
const/4 v0, 0x0
return v0
:case_down
invoke-virtual {p2}, Landroid/view/MotionEvent;->getX()F
move-result v1
invoke-virtual {p2}, Landroid/view/MotionEvent;->getY()F
move-result v2
sput v1, Lcom/example/floatingball/FloatingBallService;->initialTouchX:F
sput v2, Lcom/example/floatingball/FloatingBallService;->initialTouchY:F
invoke-virtual {p1}, Landroid/view/View;->getLeft()I
move-result v1
invoke-virtual {p1}, Landroid/view/View;->getTop()I
move-result v2
sput v1, Lcom/example/floatingball/FloatingBallService;->initialX:I
sput v2, Lcom/example/floatingball/FloatingBallService;->initialY:I
const/4 v0, 0x1
return v0
:case_move
invoke-virtual {p2}, Landroid/view/MotionEvent;->getX()F
move-result v1
invoke-virtual {p2}, Landroid/view/MotionEvent;->getY()F
move-result v2
sget v3, Lcom/example/floatingball/FloatingBallService;->initialX:I
sget v4, Lcom/example/floatingball/FloatingBallService;->initialTouchX:F
sub-float/2addr v1, v4
invoke-static {v1}, Ljava/lang/Math;->round(F)I
move-result v1
add-int/2addr v1, v3
sget v3, Lcom/example/floatingball/FloatingBallService;->initialY:I
sget v4, Lcom/example/floatingball/FloatingBallService;->initialTouchY:F
sub-float/2addr v2, v4
invoke-static {v2}, Ljava/lang/Math;->round(F)I
move-result v2
add-int/2addr v2, v3
invoke-virtual {p1, v1, v2, v1, v2}, Landroid/view/View;->layout(IIII)V
const/4 v0, 0x1
return v0
:case_up
invoke-virtual {p2}, Landroid/view/MotionEvent;->getX()F
move-result v1
invoke-virtual {p2}, Landroid/view/MotionEvent;->getY()F
move-result v2
sget v3, Lcom/example/floatingball/FloatingBallService;->initialTouchX:F
sub-float/2addr v1, v3
invoke-static {v1}, Ljava/lang/Math;->abs(F)F
move-result v1
sget v3, Lcom/example/floatingball/FloatingBallService;->initialTouchY:F
sub-float/2addr v2, v3
invoke-static {v2}, Ljava/lang/Math;->abs(F)F
move-result v2
const/high16 v3, 0x3f000000 # 0.5f
cmpl-float v1, v1, v3
cmpg-float v2, v2, v3
or-int/2addr v1, v2
if-nez v1, :not_click
# 单击事件:显示 Toast
const-string v1, "悬浮球被点击!"
const/4 v2, 0x0
invoke-static {p0, v1, v2}, Landroid/widget/Toast;->makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
move-result-object v1
invoke-virtual {v1}, Landroid/widget/Toast;->show()V
:not_click
const/4 v0, 0x1
return v0
nop
:sw_data
.packed-switch 0x0
:case_down
:case_move
:case_up
.end packed-switch
.end method
完整写出修改后方案