下面列出了android.view.WindowManager#updateViewLayout ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void applyOrientationPaddings(boolean isLandscape, int height) {
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) avatarImage.getLayoutParams();
params.width = height;
params.height = height;
avatarImage.setLayoutParams(params);
FrameLayout.LayoutParams params1 = (FrameLayout.LayoutParams) textLayout.getLayoutParams();
if (isLandscape) {
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
messageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
nameTextView.setPadding(0, OSUtilities.dp(2), 0, 0);
messageTextView.setPadding(0, OSUtilities.dp(18), 0, 0);
if (LocaleController.isRTL) {
params1.setMargins(OSUtilities.dp(40), 0, height + OSUtilities.dp(6), 0);
} else {
params1.setMargins(height + OSUtilities.dp(6), 0, OSUtilities.dp(40), 0);
}
} else {
nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
messageTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
nameTextView.setPadding(0, OSUtilities.dp(4), 0, 0);
messageTextView.setPadding(0, OSUtilities.dp(24), 0, 0);
if (LocaleController.isRTL) {
params1.setMargins(OSUtilities.dp(40), 0, height + OSUtilities.dp(8), 0);
} else {
params1.setMargins(height + OSUtilities.dp(8), 0, OSUtilities.dp(40), 0);
}
}
textLayout.setLayoutParams(params1);
if (notificationParentView != null) {
notificationLayoutParams.height = height;
if (notificationParentView.getParent() != null) {
WindowManager wm = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Context.WINDOW_SERVICE);
wm.updateViewLayout(notificationParentView, notificationLayoutParams);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mLastX = (int) event.getRawX();
mLastY = (int) event.getRawY();
mFirstX = mLastX;
mFirstY = mLastY;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int) event.getRawX() - mLastX;
int deltaY = (int) event.getRawY() - mLastY;
mLastX = (int) event.getRawX();
mLastY = (int) event.getRawY();
int totalDeltaX = mLastX - mFirstX;
int totalDeltaY = mLastY - mFirstY;
if (mIsMoving
|| Math.abs(totalDeltaX) >= MOVE_THRESHOLD
|| Math.abs(totalDeltaY) >= MOVE_THRESHOLD) {
mIsMoving = true;
WindowManager windowMgr = mWeakRef.get().mWindowManager;
WindowManager.LayoutParams parm = mWeakRef.get().mWindowParam;
if (event.getPointerCount() == 1 && windowMgr != null) {
parm.x -= deltaX;
parm.y -= deltaY;
windowMgr.updateViewLayout(this, parm);
}
}
break;
case MotionEvent.ACTION_UP:
mIsMoving = false;
break;
}
return true;
}
@Override public void onUpdateWindowParams(@NonNull WindowManager windowManager,
@NonNull WindowManager.LayoutParams originalParams,
@NonNull FloatingView floatingView,
int x, int y) {
originalParams.x = x;
originalParams.y = y;
windowManager.updateViewLayout(floatingView, originalParams);
}
public void dimBehind() {
View container = getContentView().getRootView();
Context context = getContentView().getContext();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams p = (WindowManager.LayoutParams) container.getLayoutParams();
p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
p.dimAmount = 0.2f;
wm.updateViewLayout(container, p);
}
private void drag(int x, int y) {
if (mDragView != null) {
WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mDragView
.getLayoutParams();
layoutParams.x = x;
layoutParams.y = y - mDragPointOffset;
WindowManager mWindowManager = (WindowManager) getContext()
.getSystemService(Context.WINDOW_SERVICE);
mWindowManager.updateViewLayout(mDragView, layoutParams);
if (mDragListener != null)
mDragListener.onDrag(x, y, null);// change null to "this" when
// ready to use
}
}
public void dimBehind() {
View container = getContentView().getRootView();
Context context = getContentView().getContext();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams p = (WindowManager.LayoutParams) container.getLayoutParams();
p.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
p.dimAmount = 0.2f;
wm.updateViewLayout(container, p);
}
/**
* 要显示在窗口最前面的方法
*
* @param context
* 调用对象Context getApplicationContext()
* @param window
* 调用对象 Window getWindow()
* @param floatingViewObj
* 要显示的浮动对象 View
*/
public static void show(Context context, View floatingViewObj) {
mFloatingViewObj = floatingViewObj;
view_obj = mFloatingViewObj;
Rect frame = new Rect();
// 这一句是关键,让其在top 层显示
// getWindow()
// window.getDecorView().getWindowVisibleDisplayFrame(frame);
TOOL_BAR_HIGH = frame.top;
wm = (WindowManager) context// getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
params.type = WindowManager.LayoutParams.TYPE_APPLICATION;// 2;
// params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
// params.type = 2003;//WindowManager.LayoutParams.TYPE_PHONE;
params.format = -2;
params.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
// 设置悬浮窗口长宽数据
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = HWUtils.dip2px(context, 60);
// 设定透明度
params.alpha = 80;
// 设定内部文字对齐方式
params.gravity = Gravity.LEFT | Gravity.TOP;
screenWidth = wm.getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px)
screenHeight = wm.getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800px)
// 以屏幕左上角为原点,设置x、y初始值ֵ
// params.x = (int) (screenWidth - params.width);
String postions = HWPreferences.getData(context, "HW_postions");
String mY = HWPreferences.getData(context, "HW_currentY");
if (null != postions && null != mY && 0 < mY.length()) {
if (Boolean.valueOf(postions)) {
params.x = params.width;
} else {
params.x = (int) (screenWidth - params.width);
}
isLeftPosition = Boolean.valueOf(postions);
params.y = Integer.valueOf(mY) + 60;
} else {
params.x = params.width;
params.y = ((screenHeight - params.height) / 2) + 60;
}
// params.y = (int) y;
// tv = new MyTextView(TopFrame.this);
wm.addView(floatingViewObj, params);
wm.updateViewLayout(mFloatingViewObj, params);
isShow = true;
mIsShow = true; // 定时器检测浮标是否已经显示出来的标志
}
public static void updateViewLayoutInWindow(View v, WindowManager.LayoutParams params) {
try {
WindowManager windowManager = (WindowManager) Utils.getContext().getSystemService(Context.WINDOW_SERVICE);
windowManager.updateViewLayout(v, params);
} catch (Throwable ignore){}
}
private void updateLayout(WindowManager.LayoutParams params){
WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
if(windowManager != null) {
windowManager.updateViewLayout(this, params);
}
}
/**
* 更新悬浮窗
*/
public void updateWindowView(View view, WindowManager.LayoutParams windowParams) {
WindowManager windowManager = getWindowManager(view.getContext());
if (windowManager != null)
windowManager.updateViewLayout(view, windowParams);
}
@Override
public void show(int timeout) {
super.show(timeout);
// fix pre Android 4.3 strange positioning when used in Fragments
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < Build.VERSION_CODES.JELLY_BEAN_MR2) {
try {
Field field1 = MediaController.class.getDeclaredField("mAnchor");
field1.setAccessible(true);
View mAnchor = (View)field1.get(this);
Field field2 = MediaController.class.getDeclaredField("mDecor");
field2.setAccessible(true);
View mDecor = (View)field2.get(this);
Field field3 = MediaController.class.getDeclaredField("mDecorLayoutParams");
field3.setAccessible(true);
WindowManager.LayoutParams mDecorLayoutParams = (WindowManager.LayoutParams)field3.get(this);
Field field4 = MediaController.class.getDeclaredField("mWindowManager");
field4.setAccessible(true);
WindowManager mWindowManager = (WindowManager)field4.get(this);
// NOTE: this appears in its own Window so co-ordinates are screen co-ordinates
int [] anchorPos = new int[2];
mAnchor.getLocationOnScreen(anchorPos);
// we need to know the size of the controller so we can properly position it
// within its space
mDecor.measure(MeasureSpec.makeMeasureSpec(mAnchor.getWidth(), MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(mAnchor.getHeight(), MeasureSpec.AT_MOST));
mDecor.setPadding(0,0,0,0);
mDecorLayoutParams.verticalMargin = 0;
mDecorLayoutParams.horizontalMargin = 0;
mDecorLayoutParams.width = mAnchor.getWidth();
mDecorLayoutParams.gravity = Gravity.LEFT|Gravity.TOP;
mDecorLayoutParams.x = anchorPos[0];// + (mAnchor.getWidth() - p.width) / 2;
mDecorLayoutParams.y = anchorPos[1] + mAnchor.getHeight() - mDecor.getMeasuredHeight();
mWindowManager.updateViewLayout(mDecor, mDecorLayoutParams);
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onSizeChanged(int height) {
Rect localRect = new Rect();
parentActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
WindowManager manager = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Activity.WINDOW_SERVICE);
if (manager == null || manager.getDefaultDisplay() == null) {
return;
}
int rotation = manager.getDefaultDisplay().getRotation();
if (height > OSUtilities.dp(50)) {
if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
keyboardHeightLand = height;
ApplicationLoader.applicationContext.getSharedPreferences("emoji", 0).edit().putInt("kbd_height_land3", keyboardHeightLand).commit();
} else {
keyboardHeight = height;
ApplicationLoader.applicationContext.getSharedPreferences("emoji", 0).edit().putInt("kbd_height", keyboardHeight).commit();
}
}
if (emojiPopup != null && emojiPopup.isShowing()) {
WindowManager wm = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Context.WINDOW_SERVICE);
final WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) emojiPopup.getContentView().getLayoutParams();
layoutParams.width = sizeNotifierRelativeLayout.getWidth();
if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
layoutParams.height = keyboardHeightLand;
} else {
layoutParams.height = keyboardHeight;
}
wm.updateViewLayout(emojiPopup.getContentView(), layoutParams);
if (!keyboardVisible) {
sizeNotifierRelativeLayout.post(new Runnable() {
@Override
public void run() {
if (sizeNotifierRelativeLayout != null) {
sizeNotifierRelativeLayout.setPadding(0, 0, 0, layoutParams.height);
sizeNotifierRelativeLayout.requestLayout();
}
}
});
}
}
boolean oldValue = keyboardVisible;
keyboardVisible = height > 0;
if (keyboardVisible && sizeNotifierRelativeLayout.getPaddingBottom() > 0) {
showEmojiPopup(false);
} else if (!keyboardVisible && keyboardVisible != oldValue && emojiPopup != null && emojiPopup.isShowing()) {
showEmojiPopup(false);
}
}