下面列出了android.view.WindowManager#removeView ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Show the view in front of screen at position x,y until call close()
* @param x the position x of view
* @param y the position y of view
*/
public void show(int x, int y) {
mParams.x = x;
mParams.y = y;
if (mView != mNextView) {
// remove the old view if necessary
close();
mView = mNextView;
Context context = mView.getContext().getApplicationContext();
if (context == null) {
context = mView.getContext();
}
mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
if (mView.getParent() != null) {
mWM.removeView(mView);
}
mWM.addView(mView, mParams);
}
}
/**
* Show the view in front of screen at last position until call close()
* If the first show, it will show in center of screen.
* You also can call show(x, y) to specific position.
*/
public void show() {
if (mView != mNextView) {
// remove the old view if necessary
close();
mView = mNextView;
Context context = mView.getContext().getApplicationContext();
if (context == null) {
context = mView.getContext();
}
mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
if (mView.getParent() != null) {
mWM.removeView(mView);
}
// get device width and height
Point point = new Point();
mWM.getDefaultDisplay().getSize(point);
// get config x,y
mParams.x = ConfigUtils.getInt(context, Constant.KEY_FLOAT_TOAST_X, (point.x - mView.getWidth()) / 2);
mParams.y = ConfigUtils.getInt(context, Constant.KEY_FLOAT_TOAST_Y, (point.y - mView.getHeight()) / 2);
// System.out.println("config (x, y) = " + mParams.x + "," + mParams.y);
mWM.addView(mView, mParams);
}
}
@Override
public void remove() {
if (DEBUG_SPLASH_SCREEN) Slog.v(TAG, "Removing splash screen window for " + mAppToken + ": "
+ this + " Callers=" + Debug.getCallers(4));
final WindowManager wm = mView.getContext().getSystemService(WindowManager.class);
wm.removeView(mView);
}
public static void removeViewFromWindow(View v) {
try {
WindowManager windowManager = (WindowManager) Utils.getContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.removeView(v);
} catch (Throwable t){
t.printStackTrace();
}
}
public static void removeView(View view, WindowManager manager) {
try {
manager.removeView(view);
} catch (Exception e) {
VenvyLog.e(TAG, e);
}
}
private void createFloatBarView() {
displayHeight = getResources().getDisplayMetrics().heightPixels;
displayWidth = getResources().getDisplayMetrics().widthPixels;
defaultX = displayWidth * 0.9f;
defaultY = displayHeight / 2;
thresholdX = (int)(getResources().getDisplayMetrics().widthPixels * 0.1f);
thresholdY = (int)(getResources().getDisplayMetrics().heightPixels * 0.1f);
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
params = new WindowManager.LayoutParams();
if(Build.VERSION.SDK_INT>=26){
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else{
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.gravity = Gravity.LEFT | Gravity.TOP;
params.x = (int) (displayHeight * 0.9f);
params.y = displayWidth / 2;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.format = PixelFormat.RGBA_8888;
//for get showmenuview width & height
wm.addView(showMenuView, params);
wm.removeView(showMenuView);
wm.addView(hideMenuView, params);
}
@MainThread public void show() {
assertMainThread();
View view = toast.getView();
if (view == null) {
throw new IllegalStateException("Can't show a Toast with no View.");
}
Context context = toast.getView().getContext();
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
// We can resolve the Gravity here by using the Locale for getting
// the layout direction
Configuration config = view.getContext().getResources().getConfiguration();
int gravity = toast.getGravity();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
gravity = Gravity.getAbsoluteGravity(gravity, config.getLayoutDirection());
}
params.gravity = gravity;
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
params.horizontalWeight = 1.0f;
}
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
params.verticalWeight = 1.0f;
}
params.x = toast.getXOffset();
params.y = toast.getYOffset();
params.verticalMargin = toast.getVerticalMargin();
params.horizontalMargin = toast.getHorizontalMargin();
params.packageName = context.getPackageName();
if (view.getParent() != null) {
windowManager.removeView(view);
}
windowManager.addView(view, params);
trySendAccessibilityEvent(view);
}
/**
* 将小悬浮窗从屏幕上移除
*
* @param context
*/
public void removeSmallWindow() {
if (smallWindow != null) {
WindowManager windowManager = getWindowManager();
windowManager.removeView(smallWindow);
smallWindow = null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mTextView != null) {
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.removeView(mTextView);
}
}
public void removeFloatingWindow(Context context) {
if (mFloatingView != null) {
WindowManager windowManager = getWindowManager(context);
windowManager.removeView(mFloatingView);
mFloatingView.removeFloatingWindow(FloatingPlayer.getInstance().getKSYTextureView());
mFloatingView = null;
}
}
private void stopDrag(int itemIndex) {
if (mDragView != null) {
if (mDragListener != null)
mDragListener.onStopDrag(getChildAt(itemIndex));
mDragView.setVisibility(GONE);
WindowManager wm = (WindowManager) getContext().getSystemService(
Context.WINDOW_SERVICE);
wm.removeView(mDragView);
mDragView.setImageDrawable(null);
mDragView = null;
}
}
private void stopDragging() {
if (mDragView != null) {
mDragView.setVisibility(GONE);
WindowManager wm = (WindowManager) getContext().getSystemService("window");
wm.removeView(mDragView);
mDragView.setImageDrawable(null);
mDragView = null;
}
if (mDragBitmap != null) {
mDragBitmap.recycle();
mDragBitmap = null;
}
}
public void removeFloatingWindow(Context context) {
if (mFloatingView != null) {
WindowManager windowManager = getWindowManager(context);
windowManager.removeView(mFloatingView);
mFloatingView = null;
}
}
public void detachFromWindow(WindowManager windowManager) {
this.windowManager = null;
if (isAdded) {
removeSleepRunnable();
if (getContext() instanceof Activity) {
windowManager.removeViewImmediate(this);
} else {
windowManager.removeView(this);
}
isAdded = false;
sleep = false;
}
}
public void detachFromWindow(WindowManager windowManager) {
if (!isAdded) return;
isAdded = false;
removeOnLayoutChangeListener(layoutChangeListener);
if (getContext() instanceof Activity) {
windowManager.removeViewImmediate(this);
} else {
windowManager.removeView(this);
}
}
public void hide() {
if(!mShown)
return;
mShown = false;
mService.unregisterObserver(mObserver);
mOverlayList.setAdapter(null);
try {
WindowManager windowManager = (WindowManager) mService.getSystemService(Context.WINDOW_SERVICE);
windowManager.removeView(mOverlayView);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
public void removeFloatingWindow(Context context) {
if (mFloatingView != null) {
WindowManager windowManager = getWindowManager(context);
windowManager.removeView(mFloatingView);
mFloatingView.removeFloatingWindow(FloatingPlayer.getInstance().getKSYTextureView());
mFloatingView = null;
}
}
public void handleShow(IBinder windowToken) {
if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
+ " mNextView=" + mNextView);
// If a cancel/hide is pending - no need to show - at this point
// the window token is already invalid and no need to do any work.
if (mHandler.hasMessages(CANCEL) || mHandler.hasMessages(HIDE)) {
return;
}
if (mView != mNextView) {
// remove the old view if necessary
handleHide();
mView = mNextView;
Context context = mView.getContext().getApplicationContext();
String packageName = mView.getContext().getOpPackageName();
if (context == null) {
context = mView.getContext();
}
mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
// We can resolve the Gravity here by using the Locale for getting
// the layout direction
final Configuration config = mView.getContext().getResources().getConfiguration();
final int gravity = Gravity.getAbsoluteGravity(mGravity, config.getLayoutDirection());
mParams.gravity = gravity;
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
mParams.horizontalWeight = 1.0f;
}
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
mParams.verticalWeight = 1.0f;
}
mParams.x = mX;
mParams.y = mY;
mParams.verticalMargin = mVerticalMargin;
mParams.horizontalMargin = mHorizontalMargin;
mParams.packageName = packageName;
mParams.hideTimeoutMilliseconds = mDuration ==
Toast.LENGTH_LONG ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT;
mParams.token = windowToken;
if (mView.getParent() != null) {
if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
mWM.removeView(mView);
}
if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this);
// Since the notification manager service cancels the token right
// after it notifies us to cancel the toast there is an inherent
// race and we may attempt to add a window after the token has been
// invalidated. Let us hedge against that.
try {
mWM.addView(mView, mParams);
trySendAccessibilityEvent();
} catch (WindowManager.BadTokenException e) {
/* ignore */
}
}
}
public void handleShow() {
if (mView != mNextView) {
// remove the old view if necessary
mView = mNextView;
Context context = mView.getContext().getApplicationContext();
String packageName = mView.getContext().getOpPackageName();
System.out.println("pkg:" + packageName);
if (context == null) {
context = mView.getContext();
}
mWM = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
// We can resolve the Gravity here by using the Locale for
// getting
// the layout direction
final Configuration config = mView.getContext().getResources()
.getConfiguration();
final int gravity = Gravity.getAbsoluteGravity(mGravity,
config.getLayoutDirection());
mParams.gravity = gravity;
if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
mParams.horizontalWeight = 1.0f;
}
if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
mParams.verticalWeight = 1.0f;
}
mParams.x = mX;
mParams.y = mY;
mParams.verticalMargin = mVerticalMargin;
mParams.horizontalMargin = mHorizontalMargin;
mParams.packageName = packageName;
if (mView.getParent() != null) {
mWM.removeView(mView);
}
System.out.println("add view start");
try {
mWM.addView(mView, mParams);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("add view end");
trySendAccessibilityEvent();
}
}
protected void removeSuperToast(SuperToast superToast) {
final WindowManager windowManager = superToast
.getWindowManager();
final View toastView = superToast.getView();
if (windowManager != null) {
mQueue.poll();
windowManager.removeView(toastView);
sendMessageDelayed(superToast,
Messages.DISPLAY_SUPERTOAST, 500);
if(superToast.getOnDismissListener() != null) {
superToast.getOnDismissListener().onDismiss(superToast.getView());
}
}
}