"ui";
ui.layout(
    <frame bg="#00000000" id="frame">
        <text id="action5" text="清空x" gravity="center" bg="#66000000" color="#ff00ff00" textSize="16sp" />
        <text id="action6" text="记x" gravity="center" bg="#66000000" color="#ff00ff00" textSize="16sp" />
        <text id="action7" text="清空y" gravity="center" bg="#66000000" color="#ff00ff00" textSize="16sp" />
        <text id="action8" text="记y" gravity="center" bg="#66000000" color="#ff00ff00" textSize="16sp" />
        <text id="action9" text="x=[];" gravity="left|center" bg="#66000000" color="#ff00ff00" textSize="16sp" />
        <text id="action10" text="y=[];" gravity="left|center" bg="#66000000" color="#ff00ff00" textSize="16sp" />
        <img id="action11" text="加" gravity="center" bg="#66000000" color="#ff00ff00" textSize="16sp" hint="提示" textColorHint="#ffff00ff" src="@drawable/ic_zoom_in_black_48dp" tint="#ff00bcff" />
        <img id="action12" text="减" gravity="center" bg="#66000000" color="#ff00ff00" textSize="16sp" hint="提示" textColorHint="#ffff00ff" src="@drawable/ic_zoom_out_black_48dp" tint="#ff00bcff" />
        <img id="action13" text="重" gravity="center" bg="#66000000" color="#ff00ff00" textSize="16sp" hint="提示" textColorHint="#ffff00ff" src="@drawable/ic_youtube_searched_for_black_48dp" tint="#ffe0bcff" />
        <img id="action0" bg="#66000000"/>
        <text id="action4" text="2" gravity="center" bg="#66000000" color="#ffffffff" textSize="16sp" />
        <text id="action2" text="" gravity="center" bg="#ff60a507" color="#ffff0007" textSize="16sp" />
        <text id="action3" text="选择图片" gravity="center" bg="#ff22557a" color="#ff5bce07" textSize="16sp" />
        <text id="action14" text="" gravity="top|center" bg="#21000000" color="#ff0000ff" textSize="16sp" />
    </frame>);
uix = "";
uiy = "";
path0 = "/storage/emulated/0/DCIM/Screenshots/"; //截图的路径
ui.action6.on("touch_down", function() {
    if (uix == "") {
        uix += zx;
    } else {
        uix += "," + zx;
    }
    ui.action9.text("x=[" + uix + "];");
    mog2("添加x:" + zx);
});
ui.action8.on("touch_down", function() {
    if (uiy == "") {
        uiy += zy;
    } else {
        uiy += "," + zy;
    }
    ui.action10.text("x=[" + uiy + "];");
    mog2("添加y:" + zx);
});
ui.action5.on("touch_down", function() {
    uix = "";
    ui.action9.text("x=[" + uix + "];");
    mog2("清空x");
});
ui.action7.on("touch_down", function() {
    uiy = "";
    ui.action10.text("x=[" + uiy + "];");
    mog2("清空y");
});
anjian(ui.action9);
anjian(ui.action10);

function anjian(xxx) {
    xxx.setOnTouchListener(function(view, event) {
        switch (event.getAction()) {
            case event.ACTION_DOWN:
                xxx.setBackgroundColor(colors.parseColor("#ff0000"));
                setClip(xxx.text());
                mog2("已复制:" + xxx.text());
                return true;
            case event.ACTION_MOVE:
                return true;
            case event.ACTION_UP:
                xxx.setBackgroundColor(colors.parseColor("#66000000"));
                return true;
        }
        return true;
    });
}
ys = 0;
ui.action2.setOnTouchListener(function(view, event) {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            ui.action2.setBackgroundColor(colors.parseColor("#ff0000"));
            setClip(colors.toString(ys));
            mog2("已复制:" + colors.toString(ys));
            return true;
        case event.ACTION_MOVE:
            return true;
        case event.ACTION_UP:
            ui.action2.setBackgroundColor(ys);
            return true;
    }
    return true;
});
importClass(java.io.File);
importClass(android.graphics.Bitmap);
importClass(android.graphics.Matrix);
qpbj();
zhankaiaction();

var fdbs, zx, zy, hx, hy, bitmap, zdx, zdy, img;
fdbs = 1;
zx = 0;
zy = 0;
dzx = zx * fdbs; //大中x
dzy = zy * fdbs;

function cim() {
    img0 = img;
    log(dzy);
    lsx = 0;
    lsy = 0;
    lsw = 0;
    lsh = 0;
    if (dzx > 540) { //超左
        lsx = Math.floor((dzx - 540) / fdbs);
        //img0 = images.clip(img0, ls, 0, img0.width - ls, img0.height);
        //log("1:cx:" + cx + ";" + img0.width + " " + img0.height);

        hx = 0 - (dzx - 540) % fdbs;
    } else {
        hx = 540 - dzx;
    }
    if (dzy > 540) { //超上
        lsy = Math.floor((dzy - 540) / fdbs);
        //img0 = images.clip(img0, 0, ls, img0.width, img0.height - ls);
        //log("2:cy:" + cy + ";" + img0.width + " " + img0.height);
        hy = 0 - (dzy - 540) % fdbs;
    } else {
        hy = 540 - dzy;
    }
    //log(img.height*fdbs+" "+dzx);
    if (img.width * fdbs - dzx > 540) { //超右
        lsw = Math.floor((img.width * fdbs - dzx - 540) / fdbs);
        // img0 = images.clip(img0, 0, 0, img0.width - ls, img0.height);
        //log("3:cw:" + cw + ";" + img0.width + " " + img0.height);
    }
    if (img.height * fdbs - dzy > 540) { //超下
        lsh = Math.floor((img.height * fdbs - dzy - 540) / fdbs);
        //img0 = images.clip(img0, 0, 0, img0.width, img0.height - ls);
        //log("4:ch:" + ch + ";" + img0.width + " " + img0.height);
    }
    img0 = images.clip(img0, lsx, lsy, img0.width - lsx - lsw, img0.height - lsy - lsh);
    log(hx);
    // img0=images.resize(img0, [Math.floor(img0.width*fdbs), Math.floor(img0.height*fdbs)])
    scaledBitmap = Bitmap.createScaledBitmap(img0.bitmap, Math.floor(img0.width * fdbs), Math.floor(img0.height * fdbs), false);
    //mog2("5:" + img0.width + " " + img0.height);
    return scaledBitmap;
}
ankz = 0;
ui.action11.on("touch_down", function() {
    fdbsc(fdbs*2);
});
ui.action12.on("touch_down", function() {
    fdbsc(fdbs / 2);
});
ui.action13.on("touch_down", function() {
    fdbsc(1);
});

function fdbsc(num) {
    threads.start(function() {
        dzx = Math.floor(dzx / fdbs);
        dzy = Math.floor(dzy / fdbs);
        fdbs = num;
        mog2("放大" + num + "倍");
        dzx = Math.floor(dzx * fdbs);
        dzy = Math.floor(dzy * fdbs);
        pog();
    });
}
ui.action0.setOnTouchListener(function(view, event) {
    switch (event.getAction()) {
        case event.ACTION_DOWN:
            x = event.getRawX();
            y = event.getRawY();
            X = dzx;
            Y = dzy;
            return true;
        case event.ACTION_MOVE:
            //移动手指时调整悬浮窗位置
            ydx = (event.getRawX() - x);
            ydy = (event.getRawY() - y);
            dzx = Math.floor(X - ydx); //大中x
            dzy = Math.floor(Y - ydy);

            if (dzx < 0) {
                dzx = 0;
            }
            if (dzy < 0) {
                dzy = 0;
            }
            try {
                lw = Math.floor(img.width * fdbs);
            } catch (e) {
                mog2(e);
                return false;
            }
            if (dzx > lw - 1) {
                dzx = lw - 1;
            }
            lh = Math.floor(img.height * fdbs);
            if (dzy > lh - 1) {
                dzy = lh - 1;
            }
            pog();
            return true;
        case event.ACTION_UP:
            return true;
    }
    return true;
});

function pog() {
    xb();
    zx = Math.floor(dzx / fdbs);
    zy = Math.floor(dzy / fdbs);
    bitmap0 = cim();
    ys = images.pixel(img, zx, zy);
    mog(img.width + "x" + img.height + "   x=" + zx + " y=" + zy + "  color:" + colors.toString(ys));

    canvas.drawBitmap(bitmap0, hx, hy, paint);
    hbz();
    ui.run(function() {
        ui.action0.setImageBitmap(bitmap);
        ui.action2.setBackgroundColor(ys);
    });

}
path = "/storage/emulated/0/DCIM/Screenshots/pScreenshot_2024-09-02-22-50-10-749_com.tencent.game.rhythmmaster.jpg"
mog2("图片路径:" + path);
img = images.read(path);
ui.action3.on("touch_down", function() {
    threads.start(function() {
        path = filecc()[0];
        mog2("图片路径:" + path);
        img = images.read(path);
        if (img != null) {
            pog();
        }
    });
});

function mog2(str) {
    builder = new android.text.SpannableStringBuilder();
    currentText = ui.action14.getText();
    builder.append(str + "\n").append(currentText);
    ui.run(function() {
        ui.action14.text(builder);
    });
}

function mog(str) {
    ui.run(function() {
        ui.action4.text(str);
    });
}

function xb() {
    bitmap = Bitmap.createBitmap(1080, 1080, Bitmap.Config.ARGB_8888);
    canvas = new Canvas(bitmap);
    paint = new Paint();
}

function hbz() {
    paint.setARGB(255, 255, 0, 0);
    paint.setStyle(Paint.Style.STROKE); //空心样式  
    paint.setStrokeWidth(4); //边缘宽度  
    canvas.drawCircle(540, 540, 20, paint); //绘制圆
    paint.setARGB(255, 0, 255, 0); //白色画笔
    paint.setStyle(Paint.Style.STROKE); //空心样式  
    paint.setStrokeWidth(2); //
    canvas.drawLine(460, 540, 538, 540, paint); //绘制直线
    canvas.drawLine(542, 540, 620, 540, paint); //绘制直线
    canvas.drawLine(540, 460, 540, 538, paint); //绘制直线
    canvas.drawLine(540, 542, 540, 620, paint); //绘制直线

}

function qpbj() {
    importClass(android.view.View);
    importClass(android.view.WindowManager);
    var windowManager = context.getSystemService(context.WINDOW_SERVICE);
    var layoutParams = activity.getWindow().getAttributes();
    layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
    activity.getWindow().setAttributes(layoutParams);
    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}

function zhankaiaction() {
    x = 0;
    y = 0;
    xcsz("action5", 0 + x, 0 + y, 110, 110);
    xcsz("action6", 970 + x, 0 + y, 110, 110);
    xcsz("action7", 0 + x, 113 + y, 110, 110);
    xcsz("action8", 970 + x, 113 + y, 110, 110);
    xcsz("action9", 114 + x, 0 + y, 852, 110);
    xcsz("action10", 114 + x, 113 + y, 852, 110);

    x = 0;
    y = 226;
    xcsz("action0", 0 + x, 0 + y, 1080, 1080);
    xcsz("action4", 0 + x, 0 + y, 1080, 50);

    xcsz("action2", 0 + x, 1220 + y, 375, 109);
    xcsz("action3", 433 + x, 1220 + y, 646, 115);
    xcsz("action11", 632 + x, 1090 + y, 237, 113);
    xcsz("action12", 354 + x, 1090 + y, 237, 113);
    xcsz("action13", 27 + x, 1090 + y, 237, 113);
    xcsz("action14", 0, 1570, 1080, 700);

}

function filecc() {
    path = [];
    path[0] = "/storage/emulated/0/DCIM/Screenshots/";
    filec();
    return path;

    function filec() {
        arr0 = ["..."];
        i = 1;
        var arr = files.listDir(path[0], function(name) {
            if (files.isFile(files.join(path[0], name))) {
                arr0[i] = "" + name;
            } else {
                arr0[i] = "📂" + name;
            }
            i++;
            return true;
        });

        var i = dialogs.select("请选择一个选项", arr0);
        if (i == 0) { //...
            path0 = path[0].split("/");
            if (path0.length > 5) {
                path[0] = "";
                for (i = 0; i < path0.length - 2; i++) {
                    path[0] += path0[i] + "/";
                }
            }
            filec();
        } else if (i < 0) {} else {
            if (files.isFile(files.join(path[0], arr[i - 1]))) {
                path[0] += arr[i - 1];
                path[1] = arr[i - 1];
            } else {
                path[0] += arr[i - 1] + "/";
                filec();
            }
        }
    }
}

function xcsz(k, x, y, w, h) {
    ui.run(function() {
        eval("var layoutParams = ui." + k + ".getLayoutParams();");
        layoutParams.leftMargin = x;
        layoutParams.topMargin = y;
        layoutParams.width = w;
        layoutParams.height = h;
        eval("ui." + k + ".setLayoutParams(layoutParams);");
    });
}