From 2a55400f268fba186fefecd965034f201056964e Mon Sep 17 00:00:00 2001 From: goweii Date: Fri, 28 May 2021 20:40:10 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 92 ++++++++++++++++--------------------------------------- 1 file changed, 27 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 604116d..4cd21ac 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,16 @@ Android稳定高效的浮层创建管理框架。 +# 优势 + +如果你的App用的是单Activity架构,那么这个就是系统Dialog/PopupWindow/Toast的完美替代。而且可以实现悬浮按钮和引导层等一些列功能。 + +不是单Activity也不影响,上面这些功能一样可以实现,只是 + +- 相对于Dialog更轻量,相对于PopupWindow + + + # 简介 - 同时兼容support和androidx @@ -93,14 +103,16 @@ Android稳定高效的浮层创建管理框架。 ![](https://img.shields.io/badge/Downloads%20Week-1.4k-green) ![](https://img.shields.io/badge/Downloads%20Month-7.3K-blue) -- ### 添加jitpack库 +- ### 添加仓库 ```groovy // build.gradle(Project:) allprojects { repositories { - maven { url 'https://www.jitpack.io' } + maven { url "https://www.jitpack.io" } + // 支持4.1.0及以后版本 + // 使用4.1.5及以后版本必须添加,因为高斯模糊实现用到的VisualEffect库暂时只发布在了的gitee仓库 // maven { url "https://gitee.com/goweii/maven-repository/raw/master/releases/" } } } @@ -113,82 +125,32 @@ allprojects { // build.gradle(Module:) dependencies { // 完整引入,二选一 - implementation 'com.github.goweii:AnyLayer:4.1.4-androidx' - implementation 'com.github.goweii:AnyLayer:4.1.4-support' + implementation "com.github.goweii:AnyLayer:4.1.4-androidx" + // implementation "com.github.goweii:AnyLayer:4.1.4-support" + // 按需引入 // 基础库 - // implementation 'com.github.goweii.AnyLayer:anylayer:4.1.4-androidx' - // 自动初始化(依赖基础库和Android Jetpack App Startup) - // implementation 'com.github.goweii.AnyLayer:anylayer-startup:4.1.4-androidx' + // implementation "com.github.goweii.AnyLayer:anylayer:4.1.4-androidx" + // 自动初始化(依赖基础库和Jetpack Startup) + // implementation "com.github.goweii.AnyLayer:anylayer-startup:4.1.4-androidx" // 扩展库(依赖基础库) - // implementation 'com.github.goweii.AnyLayer:anylayer-ext:4.1.4-androidx' + // implementation "com.github.goweii.AnyLayer:anylayer-ext:4.1.4-androidx" // Kotlin扩展库(依赖基础库和扩展库) - // implementation 'com.github.goweii.AnyLayer:anylayer-ktx:4.1.4-androidx' + // implementation "com.github.goweii.AnyLayer:anylayer-ktx:4.1.4-androidx" } ``` +- ### 一些问题/建议 + - 建议按需引入 -## 更新说明 - -[点击查看详细更新说明](https://github.com/goweii/AnyLayer/releases) - -### 4.1.3 - -- 优化输入法适配 -- 修复一些BUG - -### 4.1.1 - -- 修复物理返回键事件拦截问题 -- 修复带输入框弹窗点击外部关闭需要2次才能触发的问题 - -### 4.1.0 - -- 适配分屏小窗和横竖屏切换 -- 新增设置通知最大宽高 -- 新增3组对其方式 -- 新增吐司自定义布局 -- 吐司和通知默认布局适配暗色模式 -- 修复一些代码错误 + - anylayer-startup依赖于Jetpack Startup,仅支持androidx,support需要自己在application初始化 -### 4.0.0 -- 重构包结构 -- 新增通知/悬浮按钮/引导浮层 -- 支持自定义浮层层级 -### 3.6.0 - -- 新增ext库,添加常用动画创建器 -- 新增ktx库,方便kotlin调用 - -### 3.5.0 - -- 添加注解,避免kotlin调用变为可选型 - -### 3.3.0 - -- 拆分为support和androidx两个分支 - -### 3.1.0 - -- 移除对support-v7的依赖,可同时兼容support和androidx - -### 3.0.0 - -- 大型重构,修改层级和继承结构,API改变很大 -- 删除common库 - -### 2.5.0 - -- 新增popupWindow效果时可跟随targetView移动 -- 可以从ApplicationContext中弹出 -- 监听器支持添加多个 - -### 2.4.0 +## 更新说明 -- 新增通用库 +[点击查看](https://github.com/goweii/AnyLayer/releases) From 7089d777b508f87c7b13793c4d343f41635a69bf Mon Sep 17 00:00:00 2001 From: goweii Date: Wed, 2 Jun 2021 20:23:46 +0800 Subject: [PATCH 2/2] fix dismiss bug --- .../java/per/goweii/anylayer/DecorLayer.java | 4 +- .../main/java/per/goweii/anylayer/Layer.java | 105 +++++++++++------- .../android/anylayer/NormalActivity.java | 17 ++- 3 files changed, 85 insertions(+), 41 deletions(-) diff --git a/anylayer/src/main/java/per/goweii/anylayer/DecorLayer.java b/anylayer/src/main/java/per/goweii/anylayer/DecorLayer.java index f47abe9..b1edb0a 100644 --- a/anylayer/src/main/java/per/goweii/anylayer/DecorLayer.java +++ b/anylayer/src/main/java/per/goweii/anylayer/DecorLayer.java @@ -212,11 +212,11 @@ public void run() { } @Override - public void dismiss() { + public void dismiss(boolean withAnim) { if (mShowRunnable != null) { getViewHolder().getDecor().removeCallbacks(mShowRunnable); } else { - super.dismiss(); + super.dismiss(withAnim); } } diff --git a/anylayer/src/main/java/per/goweii/anylayer/Layer.java b/anylayer/src/main/java/per/goweii/anylayer/Layer.java index 4fbde09..3d72095 100644 --- a/anylayer/src/main/java/per/goweii/anylayer/Layer.java +++ b/anylayer/src/main/java/per/goweii/anylayer/Layer.java @@ -43,11 +43,27 @@ public boolean onKey(int keyCode, KeyEvent event) { } }; + private final Runnable mInAnimEndCallback = new Runnable() { + @Override + public void run() { + Layer.this.handleInAnimEnd(); + } + }; + + private final Runnable mOutAnimEndCallback = new Runnable() { + @Override + public void run() { + Layer.this.handleOutAnimEnd(); + } + }; + private final ViewManager mViewManager; private final ViewHolder mViewHolder; private final ListenerHolder mListenerHolder; private final Config mConfig; + private ViewTreeObserver.OnPreDrawListener mShowOnPreDrawListener = null; + private boolean mShowWithAnim = false; private boolean mDismissWithAnim = false; @@ -193,12 +209,7 @@ protected void onGlobalPreDraw() { private void handleShow() { if (isShown()) { if (isOutAnimRunning()) { - startAnimatorIn(new Runnable() { - @Override - public void run() { - onShow(); - } - }); + startAnimatorIn(); } return; } @@ -206,25 +217,24 @@ public void run() { mViewManager.attach(); onAttach(); getViewHolder().getChild().setVisibility(View.VISIBLE); - getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { - @Override - public boolean onPreDraw() { - if (getViewTreeObserver().isAlive()) { - getViewTreeObserver().removeOnPreDrawListener(this); - } - onAppear(); - startAnimatorIn(new Runnable() { - @Override - public void run() { - onShow(); + if (mShowOnPreDrawListener == null) { + mShowOnPreDrawListener = new ViewTreeObserver.OnPreDrawListener() { + @Override + public boolean onPreDraw() { + mShowOnPreDrawListener = null; + if (getViewTreeObserver().isAlive()) { + getViewTreeObserver().removeOnPreDrawListener(this); } - }); - return true; - } - }); + onAppear(); + startAnimatorIn(); + return true; + } + }; + } + getViewTreeObserver().addOnPreDrawListener(mShowOnPreDrawListener); } - private void startAnimatorIn(@NonNull Runnable onEnd) { + private void startAnimatorIn() { cancelAnimator(); if (mShowWithAnim) { mAnimatorIn = onCreateInAnimator(mViewHolder.getChild()); @@ -238,34 +248,41 @@ public void onAnimationEnd(Animator animation) { @Override public void onAnimationEndWithoutCancel(Animator animation) { - onEnd.run(); + super.onAnimationEndWithoutCancel(animation); + mInAnimEndCallback.run(); } }); mAnimatorIn.start(); } else { - onEnd.run(); + mInAnimEndCallback.run(); } } else { - onEnd.run(); + mInAnimEndCallback.run(); } } + private void handleInAnimEnd() { + onShow(); + } + private void handleDismiss() { if (!isShown()) return; if (isOutAnimRunning()) return; - onDismiss(); - startAnimatorOut(new Runnable() { - @Override - public void run() { - onDisappear(); - mViewManager.detach(); - onDetach(); - onDestroy(); + if (mShowOnPreDrawListener != null) { + mShowOnPreDrawListener = null; + if (getViewTreeObserver().isAlive()) { + getViewTreeObserver().removeOnPreDrawListener(mShowOnPreDrawListener); } - }); + mViewManager.detach(); + onDetach(); + onDestroy(); + return; + } + onDismiss(); + startAnimatorOut(); } - private void startAnimatorOut(@NonNull final Runnable onEnd) { + private void startAnimatorOut() { cancelAnimator(); if (mDismissWithAnim) { mAnimatorOut = onCreateOutAnimator(mViewHolder.getChild()); @@ -279,33 +296,45 @@ public void onAnimationEnd(Animator animation) { @Override public void onAnimationEndWithoutCancel(Animator animation) { + super.onAnimationEndWithoutCancel(animation); // 动画执行结束后不能直接removeView,要在下一个dispatchDraw周期移除 // 否则会崩溃,因为viewGroup的childCount没有来得及-1,获取到的view为空 getViewHolder().getChild().setVisibility(View.INVISIBLE); - getViewHolder().getParent().post(onEnd); + getViewHolder().getParent().post(mOutAnimEndCallback); } }); mAnimatorOut.start(); } else { getViewHolder().getChild().setVisibility(View.INVISIBLE); - onEnd.run(); + mOutAnimEndCallback.run(); } } else { getViewHolder().getChild().setVisibility(View.INVISIBLE); - onEnd.run(); + mOutAnimEndCallback.run(); } } + private void handleOutAnimEnd() { + onDisappear(); + mViewManager.detach(); + onDetach(); + onDestroy(); + } + private ViewTreeObserver getViewTreeObserver() { return getViewHolder().getParent().getViewTreeObserver(); } private void cancelAnimator() { + getViewHolder().getParent().removeCallbacks(mInAnimEndCallback); + getViewHolder().getParent().removeCallbacks(mOutAnimEndCallback); if (mAnimatorIn != null) { + mAnimatorIn.removeAllListeners(); mAnimatorIn.cancel(); mAnimatorIn = null; } if (mAnimatorOut != null) { + mAnimatorOut.removeAllListeners(); mAnimatorOut.cancel(); mAnimatorOut = null; } diff --git a/app/src/main/java/per/goweii/android/anylayer/NormalActivity.java b/app/src/main/java/per/goweii/android/anylayer/NormalActivity.java index 3b9c7d4..9ece2c0 100644 --- a/app/src/main/java/per/goweii/android/anylayer/NormalActivity.java +++ b/app/src/main/java/per/goweii/android/anylayer/NormalActivity.java @@ -127,7 +127,22 @@ public void onClick(@NonNull Layer layer, @NonNull View v) { .cancelableOnClickKeyBack(true) .onClickToDismiss(R.id.fl_dialog_no); dialog.show(); - dialog.dismiss(); + new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(30); + } catch (InterruptedException e) { + e.printStackTrace(); + } + runOnUiThread(new Runnable() { + @Override + public void run() { + dialog.dismiss(); + } + }); + } + }).start(); } private Random mRandom = new Random();