将视图添加到窗口管理器时出现空白屏幕
A blank white screen appeared when addview to windowmanager
我正在使用 windowmanager 中的 addview 做一个简单的锁屏,虽然我成功添加了锁屏视图,但在添加视图之前,在显示视图之前添加了一个白色烦人的页面。我试图将视图背景设置为其他颜色,但它没有影响任何东西所以我认为它可能与 window 视图有关,在 activity 中更改背景颜色后,延迟添加视图,它仍然出现了。
ps: 在finish()之前调用remove view时出现白色view,ondestroyed remove view时会延迟一点但是不会出现白色view
我remove/hide如何在addView/removeView时看到这个白色视图?
public static synchronized LockUtil getInstance(Activity act) {
if (mLockLayer == null) {
mLockLayer = new LockUtil(act);
}
return mLockLayer;
}
private LockUtil(Activity act) {
mActivty = act;
init();
}
private void init() {
isLocked = false;
if (null == mWindowManager) {
mWindowManager = ((WindowManager) mActivty.getSystemService(Context.WINDOW_SERVICE));
}
mLockViewLayoutParams = new WindowManager.LayoutParams();
mLockViewLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
mLockViewLayoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mLockViewLayoutParams.windowAnimations = android.R.style.Animation_Translucent;
}
public synchronized void lock() {
if (mLockView != null && !isLocked) {
mWindowManager.addView(mLockView, mLockViewLayoutParams);
isLocked = true;
}
}
public synchronized void unlock() {
if (mWindowManager != null && isLocked) {
try{
mWindowManager.removeView(mLockView);
isLocked = false;
}catch(IllegalArgumentException e){
Logger.e("LockUtil unlock failed:"+e.getMessage());
}
}
mActivty = null;
}
经过一些研究,白色背景是 activity (window) 的背景颜色,所以为了移除它,我们只需要将颜色设置为透明主题。
<style name="Theme.Transparent" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
我正在使用 windowmanager 中的 addview 做一个简单的锁屏,虽然我成功添加了锁屏视图,但在添加视图之前,在显示视图之前添加了一个白色烦人的页面。我试图将视图背景设置为其他颜色,但它没有影响任何东西所以我认为它可能与 window 视图有关,在 activity 中更改背景颜色后,延迟添加视图,它仍然出现了。
ps: 在finish()之前调用remove view时出现白色view,ondestroyed remove view时会延迟一点但是不会出现白色view
我remove/hide如何在addView/removeView时看到这个白色视图?
public static synchronized LockUtil getInstance(Activity act) {
if (mLockLayer == null) {
mLockLayer = new LockUtil(act);
}
return mLockLayer;
}
private LockUtil(Activity act) {
mActivty = act;
init();
}
private void init() {
isLocked = false;
if (null == mWindowManager) {
mWindowManager = ((WindowManager) mActivty.getSystemService(Context.WINDOW_SERVICE));
}
mLockViewLayoutParams = new WindowManager.LayoutParams();
mLockViewLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
mLockViewLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
mLockViewLayoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
mLockViewLayoutParams.windowAnimations = android.R.style.Animation_Translucent;
}
public synchronized void lock() {
if (mLockView != null && !isLocked) {
mWindowManager.addView(mLockView, mLockViewLayoutParams);
isLocked = true;
}
}
public synchronized void unlock() {
if (mWindowManager != null && isLocked) {
try{
mWindowManager.removeView(mLockView);
isLocked = false;
}catch(IllegalArgumentException e){
Logger.e("LockUtil unlock failed:"+e.getMessage());
}
}
mActivty = null;
}
经过一些研究,白色背景是 activity (window) 的背景颜色,所以为了移除它,我们只需要将颜色设置为透明主题。
<style name="Theme.Transparent" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>