index.uts 2.4 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

import { getUniActivity } from "io.dcloud.uts.android";
import Rect from "android.graphics.Rect";
import Color from "android.graphics.Color";
import TextView from "android.widget.TextView";
import FrameLayout from "android.widget.FrameLayout";
import ViewGroup from "android.view.ViewGroup";
import Gravity from "android.view.Gravity";

type TimerOptions = {
  start: (res: string) => void;
  work: (res: string) => void;
};



export function doTimerTask(opts:TimerOptions) {
	opts.start('doTimerTask start');
	setTimeout(function() {
		opts.work("doTimerTask work");
	}, 2000);
  
  return { name: "doTimerTask" };
}


export function doIntervalTask(opts:TimerOptions) {
	
	var taskRet = setInterval(function() {
		opts.work("doIntervalTask work");
	}, 2000);
	opts.start('doIntervalTask start');
  
  return { name: "doIntervalTask",taskId:taskRet};
}

export function clearIntervalTask(taskId:number) {
	
	clearInterval(taskId);
  
  return { name: "clearIntervalTask"};
}



class AddUIRunnable extends Runnable {

    override run():void {
		
        var textView = TextView(getUniActivity())
        textView.text = "HELLO WORLD"
        textView.textSize =  30.0.toFloat();
        textView.setBackgroundColor(Color.RED)
        textView.tag = "helloText"
        textView.gravity = Gravity.CENTER

        var decorView = getUniActivity()!.window.decorView;


        var frameContent = decorView.findViewById(android.R.id.content) as FrameLayout
        var layoutParam = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParam.topMargin = 200;

         frameContent.addView(textView,layoutParam)


    }
};

class RemoveUIRunnable extends Runnable {

    override run():void {

        var decorView = getUniActivity()!.window.decorView;
        var frameContent = decorView.findViewById(android.R.id.content) as FrameLayout

		var targetTV = frameContent.findViewWithTag("helloText") as TextView
杜庆泉's avatar
杜庆泉 已提交
78
        frameContent.removeView(targetTV)
杜庆泉's avatar
杜庆泉 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98


    }
};


export function addViewToDecorView() {
    var uiRunable = AddUIRunnable();
    // 获取android环境下 宿主decorview的基本信息
    getUniActivity()!.runOnUiThread(uiRunable)

}

export function removeViewToDecorView() {
    var uiRunable = RemoveUIRunnable();
    // 获取android环境下 宿主decorview的基本信息
    getUniActivity()!.runOnUiThread(uiRunable)
}