276 lines
9.3 KiB
Plaintext
276 lines
9.3 KiB
Plaintext
import LinearLayout from 'android.widget.LinearLayout';
|
|
import Context from 'android.content.Context'
|
|
import RelativeLayout from 'android.widget.RelativeLayout';
|
|
import ViewGroup from 'android.view.ViewGroup';
|
|
import View from 'android.view.View';
|
|
import { XLOADINGS_TYPE, XLOADINGS_TYPE_PRIVATE } from "../interface.uts"
|
|
import Color from 'android.graphics.Color';
|
|
import Gravity from 'android.view.Gravity';
|
|
import GradientDrawable from 'android.graphics.drawable.GradientDrawable';
|
|
import TextView from 'android.widget.TextView';
|
|
import Typeface from 'android.graphics.Typeface';
|
|
import TextUtils from 'android.text.TextUtils';
|
|
import Uri from 'android.net.Uri';
|
|
import ObjectAnimator from 'android.animation.ObjectAnimator';
|
|
import ValueAnimator from 'android.animation.ValueAnimator';
|
|
import TimeInterpolator from 'android.animation.TimeInterpolator';
|
|
import MotionEvent from 'android.view.MotionEvent';
|
|
import { hexToRgb } from "../libs/color.uts"
|
|
import { defaultConfig, configCover } from "../libs/config.uts"
|
|
import ViewPropertyAnimator from 'android.view.ViewPropertyAnimator';
|
|
import AccelerateDecelerateInterpolator from 'android.view.animation.AccelerateDecelerateInterpolator';
|
|
// #ifdef UNI-APP-X
|
|
import getCurrentPages from 'io.dcloud.uniapp.framework.getCurrentPages';
|
|
import onReady from 'io.dcloud.uniapp.framework.onReady';
|
|
import onUnload from 'io.dcloud.uniapp.framework.onUnload';
|
|
import onShow from 'io.dcloud.uniapp.framework.onShow';
|
|
// #endif
|
|
|
|
import AttributeSet from 'android.util.AttributeSet';
|
|
import R from 'android.R';
|
|
import Bundle from "android.os.Bundle";
|
|
import Dialog from "android.app.Dialog";
|
|
import WindowManager from "android.view.WindowManager";
|
|
import Window from "android.view.Window";
|
|
import ColorDrawable from "android.graphics.drawable.ColorDrawable";
|
|
let rotationAnimator:ObjectAnimator|null = null;
|
|
let maskDomId = 5894
|
|
function px2dp(n : number) : number {
|
|
|
|
const mets = UTSAndroid.getAppContext()!.resources!.getDisplayMetrics()
|
|
|
|
return mets.density * n
|
|
}
|
|
function setSpin(textView : TextView) {
|
|
try {
|
|
textView.clearAnimation();
|
|
} catch (e) {
|
|
//TODO handle the exception
|
|
}
|
|
let rotationStart = 0
|
|
let rotation = 360
|
|
let rotationSFD = 0.5
|
|
if(rotationAnimator!=null){
|
|
rotationAnimator!.cancel()
|
|
}
|
|
rotationAnimator = ObjectAnimator.ofFloat(textView, "rotation", rotationStart.toFloat(), rotation.toFloat());
|
|
if(rotationAnimator==null) return;
|
|
rotationAnimator!.setDuration(1000);
|
|
class LinearInterpolator implements TimeInterpolator {
|
|
override getInterpolation(input : Float) : Float {
|
|
return input;
|
|
}
|
|
}
|
|
rotationAnimator!.setInterpolator(new LinearInterpolator());
|
|
rotationAnimator!.setRepeatCount(ValueAnimator.INFINITE);
|
|
rotationAnimator!.start();
|
|
}
|
|
let dialogModal : FullScreenDialogFragment | null = null;
|
|
class FullScreenDialogFragment extends Dialog {
|
|
private mContext : Context;
|
|
private opts : XLOADINGS_TYPE|null = null;
|
|
private maskeDom:RelativeLayout|null = null;
|
|
constructor(context : Context, optsOwer : XLOADINGS_TYPE|null) {
|
|
super(context,R.style.Theme_Translucent_NoTitleBar_Fullscreen);
|
|
this.mContext = context;
|
|
this.opts = optsOwer
|
|
}
|
|
override onCreate(savedInstanceState ?: Bundle) {
|
|
super.onCreate(savedInstanceState)
|
|
this.requestWindowFeature(Window.FEATURE_NO_TITLE)
|
|
// 禁止按键返回及侧滑返回.
|
|
this.setCancelable(false)
|
|
this.maskeDom = _createToastView(this.getContext(),this.window!.decorView! as ViewGroup,this.opts)
|
|
// 设置窗口属性
|
|
let window = this.getWindow();
|
|
if (window != null) {
|
|
// 首先设置全屏标志
|
|
window.setFlags(
|
|
WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
|
WindowManager.LayoutParams.FLAG_FULLSCREEN
|
|
);
|
|
|
|
// 设置布局参数为全屏
|
|
window.setLayout(
|
|
WindowManager.LayoutParams.MATCH_PARENT,
|
|
WindowManager.LayoutParams.MATCH_PARENT
|
|
);
|
|
|
|
// 设置背景透明
|
|
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
|
|
|
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
|
|
window.setGravity(Gravity.CENTER);
|
|
|
|
// 完整的沉浸式设置
|
|
let decorView = window.getDecorView();
|
|
let option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|
decorView.setSystemUiVisibility(option);
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
|
|
window.setStatusBarColor(Color.TRANSPARENT)
|
|
window.setNavigationBarColor(Color.TRANSPARENT)
|
|
window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
|
|
}
|
|
|
|
}
|
|
override dismiss(){
|
|
super.dismiss()
|
|
this.maskeDom = null;
|
|
dialogModal = null;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
export function hideXloading() {
|
|
UTSAndroid.getDispatcher("main").async(function (_) {
|
|
dialogModal?.dismiss()
|
|
dialogModal = null;
|
|
rotationAnimator?.cancel()
|
|
rotationAnimator = null;
|
|
})
|
|
}
|
|
|
|
function _createToastView(context : Context, decorView : ViewGroup, opts : XLOADINGS_TYPE|null):RelativeLayout{
|
|
|
|
let realopts = opts == null ? ({} as XLOADINGS_TYPE) : (opts!)
|
|
let config = configCover(realopts)
|
|
let rmbgColor = hexToRgb(config.maskBgColor);
|
|
let textColor = hexToRgb(config.iconColor);
|
|
let wrapColor = hexToRgb(config.contentBgColor);
|
|
let titleColorRgb = hexToRgb(config.titleColor);
|
|
|
|
let mbgColor = rmbgColor == null ? Color.parseColor(config.maskBgColor) : Color.argb((rmbgColor!.a * 255).toInt(), rmbgColor!.r.toInt(), rmbgColor!.g.toInt(), rmbgColor!.b.toInt())
|
|
let fontColor = textColor == null ? Color.parseColor(config.iconColor) : Color.argb((textColor!.a * 255).toInt(), textColor!.r.toInt(), textColor!.g.toInt(), textColor!.b.toInt())
|
|
let contentColor = rmbgColor == null ? Color.parseColor(config.contentBgColor) : Color.argb((wrapColor!.a * 255).toInt(), wrapColor!.r.toInt(), wrapColor!.g.toInt(), wrapColor!.b.toInt())
|
|
let titleColor = titleColorRgb == null ? Color.parseColor(config.titleColor) : Color.argb((titleColorRgb!.a * 255).toInt(), titleColorRgb!.r.toInt(), titleColorRgb!.g.toInt(), titleColorRgb!.b.toInt())
|
|
|
|
let maskerDom = new RelativeLayout(context)
|
|
|
|
class MaskerDomClickListsner extends View.OnTouchListener {
|
|
constructor() {
|
|
super();
|
|
}
|
|
override onTouch(view : View, event : MotionEvent) : Boolean {
|
|
// 阻止事件向下传递。
|
|
return true;
|
|
}
|
|
}
|
|
maskerDom.setAlpha((0).toFloat())
|
|
maskerDom.setOnTouchListener(new MaskerDomClickListsner())
|
|
|
|
let maskerDomLayrPrams = new LinearLayout.LayoutParams(
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
ViewGroup.LayoutParams.MATCH_PARENT
|
|
)
|
|
maskerDom.setLayoutParams(maskerDomLayrPrams)
|
|
maskerDom.setBackgroundColor(mbgColor)
|
|
|
|
|
|
maskerDom.setGravity(Gravity.CENTER)
|
|
maskerDom.setId((maskDomId).toInt())
|
|
let contentWrapDiv = new LinearLayout(context);
|
|
// 设置圆角背景
|
|
let bg = new GradientDrawable()
|
|
bg.setColor(contentColor)
|
|
bg.setCornerRadius((px2dp(16)).toFloat())
|
|
contentWrapDiv.setBackground(bg)
|
|
// contentWrapDiv.setBackgroundColor(Color.parseColor(config.contentBgColor))
|
|
let wpx = px2dp(config.size).toInt();
|
|
let contentLays = new LinearLayout.LayoutParams(wpx,ViewGroup.LayoutParams.WRAP_CONTENT);
|
|
|
|
contentWrapDiv.setLayoutParams(contentLays)
|
|
contentWrapDiv.setMinimumHeight(wpx)
|
|
let wpadingx = px2dp(16).toInt();
|
|
contentWrapDiv.setPadding(wpadingx, wpadingx, wpadingx, wpadingx)
|
|
contentWrapDiv.setGravity(Gravity.CENTER)
|
|
contentWrapDiv.setOrientation(LinearLayout.VERTICAL)
|
|
|
|
// 创建图标。
|
|
let iconDiv = new TextView(context)
|
|
// iconDiv.setLineHeight(1)
|
|
iconDiv.setTextColor(fontColor)
|
|
let assetManager = context!.getAssets();
|
|
let typeface = Typeface.createFromAsset(assetManager, "remixicon.ttf")
|
|
iconDiv.setTypeface(typeface)
|
|
iconDiv.setGravity(Gravity.CENTER)
|
|
iconDiv.setTextSize(config.iconSize.toFloat())
|
|
if (!TextUtils.isEmpty(config.iconCode)) {
|
|
let codePoint = Integer.parseInt(config.iconCode, 16);
|
|
let charArray = Character.toChars(codePoint);
|
|
let text = new String(charArray);
|
|
iconDiv.setText(text);
|
|
}
|
|
setSpin(iconDiv)
|
|
|
|
|
|
|
|
|
|
contentWrapDiv.addView(iconDiv)
|
|
|
|
if (config.title != "") {
|
|
// 添加一个title
|
|
let titleDiv = new TextView(context)
|
|
titleDiv.setText(config.title)
|
|
titleDiv.setTextSize((config.titleSize).toFloat())
|
|
titleDiv.setTextColor(titleColor)
|
|
titleDiv.setGravity(Gravity.CENTER)
|
|
contentWrapDiv.addView(titleDiv)
|
|
}
|
|
|
|
maskerDom.addView(contentWrapDiv)
|
|
|
|
decorView.addView(maskerDom!)
|
|
|
|
try {
|
|
let s0 = (0).toFloat()
|
|
let s1 = (1).toFloat()
|
|
contentWrapDiv.setAlpha(s0)
|
|
contentWrapDiv.setScaleX(s0)
|
|
contentWrapDiv.setScaleY(s0)
|
|
|
|
// 遮罩渐变
|
|
const maskAni = maskerDom.animate() as ViewPropertyAnimator;
|
|
maskAni.alpha(s1)
|
|
.setDuration(300)
|
|
.setInterpolator(AccelerateDecelerateInterpolator())
|
|
.start()
|
|
|
|
let ani = contentWrapDiv.animate() as ViewPropertyAnimator;
|
|
ani.alpha(s1)
|
|
.scaleX(s1)
|
|
.scaleY(s1)
|
|
.setDuration(300)
|
|
.setInterpolator(AccelerateDecelerateInterpolator())
|
|
.start()
|
|
} catch (e) {
|
|
//TODO handle the exception
|
|
}
|
|
|
|
return maskerDom;
|
|
}
|
|
function _showLoading_(opts : XLOADINGS_TYPE | null) {
|
|
dialogModal = new FullScreenDialogFragment(UTSAndroid.getUniActivity()!,opts)
|
|
UTSAndroid.getDispatcher("main").async(function (_) {
|
|
dialogModal?.show()
|
|
})
|
|
}
|
|
|
|
export function showLoading(opts : XLOADINGS_TYPE | null) {
|
|
dialogModal?.dismiss()
|
|
const pages = getCurrentPages();
|
|
if (pages.length > 0) {
|
|
const page = pages[pages.length - 1].vm!
|
|
const instance = page.$
|
|
if (page.$isReady) {
|
|
_showLoading_(opts)
|
|
} else {
|
|
hideXloading()
|
|
}
|
|
onUnload(() => {
|
|
hideXloading()
|
|
}, instance)
|
|
}else{
|
|
_showLoading_(opts)
|
|
}
|
|
} |