Java Code Examples for android.view.Gravity#FILL_HORIZONTAL

The following examples show how to use android.view.Gravity#FILL_HORIZONTAL . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EqualizerVolumePanel.java    From Noyze with Apache License 2.0 6 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE		     |
                 WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH     |
                 WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL         |
                 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED	     );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, 0,
            getResources().getDimensionPixelSize(R.dimen.volume_panel_top),
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
    WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.setTitle(TAG);
    WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 2
Source File: StatusBarVolumePanel.java    From Noyze with Apache License 2.0 6 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
	int flags = (LayoutParams.FLAG_NOT_FOCUSABLE		|
                    LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH  |
                    LayoutParams.FLAG_NOT_TOUCH_MODAL      |
                    LayoutParams.FLAG_LAYOUT_INSET_DECOR   |
			     LayoutParams.FLAG_LAYOUT_IN_SCREEN		|
			     LayoutParams.FLAG_SHOW_WHEN_LOCKED	    );
	LayoutParams WPARAMS = new WindowManager.LayoutParams(
		LayoutParams.MATCH_PARENT, mStatusBarHeight, 0, 0,
		LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
	WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
	WPARAMS.packageName = getContext().getPackageName();
	WPARAMS.setTitle(TAG);
       WPARAMS.rotationAnimation = LayoutParams.ROTATION_ANIMATION_JUMPCUT;
	WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
	WPARAMS.screenBrightness = WPARAMS.buttonBrightness = LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
	return WPARAMS;
}
 
Example 3
Source File: ToastUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
private void setToastParams() {
    mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    mParams.format = PixelFormat.TRANSLUCENT;
    mParams.windowAnimations = android.R.style.Animation_Toast;
    mParams.setTitle("ToastWithoutNotification");
    mParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    mParams.packageName = Utils.getApp().getPackageName();

    mParams.gravity = mToast.getGravity();
    if ((mParams.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
        mParams.horizontalWeight = 1.0f;
    }
    if ((mParams.gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
        mParams.verticalWeight = 1.0f;
    }

    mParams.x = mToast.getXOffset();
    mParams.y = mToast.getYOffset();
    mParams.horizontalMargin = mToast.getHorizontalMargin();
    mParams.verticalMargin = mToast.getVerticalMargin();
}
 
Example 4
Source File: VolumeBarPanel.java    From Noyze with Apache License 2.0 6 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE		|
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH  |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL      |
            WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR   |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN   |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN		|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED	    );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, 0, 0,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
    WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.setTitle(TAG);
    WPARAMS.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
    WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 5
Source File: VolumeBarPanel.java    From Noyze with Apache License 2.0 6 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE		|
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH  |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL      |
            WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR   |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_OVERSCAN   |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN		|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED	    );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, 0, 0,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
    WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.setTitle(TAG);
    WPARAMS.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
    WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 6
Source File: BlackberryVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE		|
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH     |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL         |
            WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR      |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN		|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED	    );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,
            0, 0, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
    WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.setTitle(TAG);
    WPARAMS.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
    Resources res = getResources();
    final int maxWidth = res.getDimensionPixelSize(R.dimen.notification_panel_width);
    final int menuWidth = res.getDimensionPixelSize(R.dimen.max_menu_width);
    final int screenWidth = getWindowWidth();
    if (maxWidth <= 0 && (!res.getBoolean(R.bool.isTablet) && screenWidth < menuWidth)) {
        WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.CENTER_VERTICAL);
    } else {
        WPARAMS.gravity = (Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        WPARAMS.width = (maxWidth <= 0) ? menuWidth : maxWidth;
    }
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 7
Source File: WPVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH     |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL         |
            WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR      |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN        |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED        );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.RGB_565);
    WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
    WPARAMS.setTitle(TAG);
    Resources res = getResources();
    final int panelWidth = getNotificationPanelWidth();
    final int maxWidth = ((panelWidth > 0) ? panelWidth : res.getDimensionPixelSize(R.dimen.notification_panel_width));
    final int menuWidth = res.getDimensionPixelSize(R.dimen.max_menu_width);
    final int screenWidth = getWindowWidth();
    if (stretch || (maxWidth <= 0) || (!res.getBoolean(R.bool.isTablet) && screenWidth < menuWidth)) {
        WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
    } else {
        WPARAMS.gravity = (Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        WPARAMS.width = (maxWidth <= 0) ? menuWidth : maxWidth;
    }
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 8
Source File: WPVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH     |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL         |
            WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR      |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN        |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED        );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.RGB_565);
    WPARAMS.windowAnimations = android.R.style.Animation_Dialog;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
    WPARAMS.setTitle(TAG);
    Resources res = getResources();
    final int panelWidth = getNotificationPanelWidth();
    final int maxWidth = ((panelWidth > 0) ? panelWidth : res.getDimensionPixelSize(R.dimen.notification_panel_width));
    final int menuWidth = res.getDimensionPixelSize(R.dimen.max_menu_width);
    final int screenWidth = getWindowWidth();
    if (stretch || (maxWidth <= 0) || (!res.getBoolean(R.bool.isTablet) && screenWidth < menuWidth)) {
        WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
    } else {
        WPARAMS.gravity = (Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        WPARAMS.width = (maxWidth <= 0) ? menuWidth : maxWidth;
    }
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 9
Source File: HeadsUpVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE		|
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH     |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL         |
            WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR      |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN		|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED	    );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,
            0, 0, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
    WPARAMS.windowAnimations = android.R.style.Animation_Translucent;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.setTitle(TAG);
    WPARAMS.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
    Resources res = getResources();
    final int panelWidth = getNotificationPanelWidth();
    final int maxWidth = ((panelWidth > 0) ? panelWidth : res.getDimensionPixelSize(R.dimen.notification_panel_width));
    final int menuWidth = res.getDimensionPixelSize(R.dimen.max_menu_width);
    final int screenWidth = getWindowWidth();
    if (stretch || (maxWidth <= 0 && (!res.getBoolean(R.bool.isTablet) && screenWidth < menuWidth))) {
        WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
    } else {
        WPARAMS.gravity = (Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        WPARAMS.width = (maxWidth <= 0) ? menuWidth : maxWidth;
    }
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 10
Source File: ViewHelper.java    From tns-core-modules-widgets with Apache License 2.0 5 votes vote down vote up
public static void setHorizontalAlignment(android.view.View view, String value) {
    ViewGroup.LayoutParams params = view.getLayoutParams();
    // Initialize if empty.
    if (params == null) {
        params = new CommonLayoutParams();
    }

    // Set margins only if params are of the correct type.
    if (params instanceof FrameLayout.LayoutParams) {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) params;
        switch (value) {
            case "left":
                lp.gravity = Gravity.LEFT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
            case "center":
            case "middle":
                lp.gravity = Gravity.CENTER_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
            case "right":
                lp.gravity = Gravity.RIGHT | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
            case "stretch":
                lp.gravity = Gravity.FILL_HORIZONTAL | (lp.gravity & Gravity.VERTICAL_GRAVITY_MASK);
                break;
        }
        view.setLayoutParams(params);
    }
}
 
Example 11
Source File: UberVolumePanel.java    From Noyze with Apache License 2.0 5 votes vote down vote up
@Override
public WindowManager.LayoutParams getWindowLayoutParams() {
    int flags = (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE		|
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH     |
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL         |
            WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR      |
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN		|
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED	    );
    WindowManager.LayoutParams WPARAMS = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT,
            0, 0, WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, flags, PixelFormat.TRANSLUCENT);
    WPARAMS.windowAnimations = android.R.style.Animation_Translucent;
    WPARAMS.packageName = getContext().getPackageName();
    WPARAMS.setTitle(TAG);
    WPARAMS.rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
    Resources res = getResources();
    final int panelWidth = getNotificationPanelWidth();
    final int maxWidth = ((panelWidth > 0) ? panelWidth : res.getDimensionPixelSize(R.dimen.notification_panel_width));
    final int menuWidth = res.getDimensionPixelSize(R.dimen.max_menu_width);
    final int screenWidth = getWindowWidth();
    if (stretch || (maxWidth <= 0 && (!res.getBoolean(R.bool.isTablet) && screenWidth < menuWidth))) {
        WPARAMS.gravity = (Gravity.FILL_HORIZONTAL | Gravity.TOP);
    } else {
        WPARAMS.gravity = (Gravity.CENTER_HORIZONTAL | Gravity.TOP);
        WPARAMS.width = (maxWidth <= 0) ? menuWidth : maxWidth;
    }
    WPARAMS.screenBrightness = WPARAMS.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
    return WPARAMS;
}
 
Example 12
Source File: LayerDrawable.java    From Carbon with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves layer gravity given explicit gravity and dimensions.
 * <p/>
 * If the client hasn't specified a gravity but has specified an explicit dimension, defaults to
 * START or TOP. Otherwise, defaults to FILL to preserve legacy behavior.
 *
 * @param gravity layer gravity
 * @param width   width of the layer if set, -1 otherwise
 * @param height  height of the layer if set, -1 otherwise
 * @return the default gravity for the layer
 */
private static int resolveGravity(int gravity, int width, int height,
                                  int intrinsicWidth, int intrinsicHeight) {
    if (!Gravity.isHorizontal(gravity)) {
        if (width < 0) {
            gravity |= Gravity.FILL_HORIZONTAL;
        } else {
            gravity |= Gravity.START;
        }
    }

    if (!Gravity.isVertical(gravity)) {
        if (height < 0) {
            gravity |= Gravity.FILL_VERTICAL;
        } else {
            gravity |= Gravity.TOP;
        }
    }

    // If a dimension if not specified, either implicitly or explicitly,
    // force FILL for that dimension's gravity. This ensures that colors
    // are handled correctly and ensures backward compatibility.
    if (width < 0 && intrinsicWidth < 0) {
        gravity |= Gravity.FILL_HORIZONTAL;
    }

    if (height < 0 && intrinsicHeight < 0) {
        gravity |= Gravity.FILL_VERTICAL;
    }

    return gravity;
}
 
Example 13
Source File: Mixture.java    From frenchtoast with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 14
Source File: TagTabStrip.java    From ProjectX with Apache License 2.0 4 votes vote down vote up
@SuppressLint("RtlHardcoded")
private void applyGravity(int itemWidth, int itemHeight) {
    final int count = mCount;
    if (count == 0) {
        mFirstCenterX = 0;
        mFirstCenterY = 0;
        mItemCenterOffset = 0;
        return;
    }
    final int padding = mPadding;
    final float scale = mScale > 1 ? mScale : 1;
    final int paddingStart = ViewCompat.getPaddingStart(this);
    final int paddingEnd = ViewCompat.getPaddingEnd(this);
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();
    final int width = getMeasuredWidth();
    final int height = getMeasuredHeight();
    final int contentWidth = width - paddingStart - paddingEnd;
    final int contentHeight = height - paddingTop - paddingBottom;
    mItemCenterOffset = itemWidth + padding;
    switch (GravityCompat.getAbsoluteGravity(mGravity,
            ViewCompat.getLayoutDirection(this))) {
        case Gravity.LEFT:
        case Gravity.TOP:
        case Gravity.LEFT | Gravity.TOP:
            mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
            mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
            break;
        case Gravity.CENTER_HORIZONTAL:
        case Gravity.CENTER_HORIZONTAL | Gravity.TOP:
            mFirstCenterX = paddingStart + contentWidth * 0.5f -
                    mItemCenterOffset * (count - 1) * 0.5f;
            mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
            break;
        case Gravity.RIGHT:
        case Gravity.RIGHT | Gravity.TOP:
            mFirstCenterX = width - paddingEnd - itemWidth * scale * 0.5f -
                    mItemCenterOffset * (count - 1);
            mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
            break;
        case Gravity.CENTER_VERTICAL:
        case Gravity.CENTER_VERTICAL | Gravity.LEFT:
            mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
            mFirstCenterY = paddingTop + contentHeight * 0.5f;
            break;
        default:
        case Gravity.CENTER:
            mFirstCenterX = paddingStart + contentWidth * 0.5f -
                    mItemCenterOffset * (count - 1) * 0.5f;
            mFirstCenterY = paddingTop + contentHeight * 0.5f;
            break;
        case Gravity.CENTER_VERTICAL | Gravity.RIGHT:
            mFirstCenterX = width - paddingEnd - itemWidth * scale * 0.5f -
                    mItemCenterOffset * (count - 1);
            mFirstCenterY = paddingTop + contentHeight * 0.5f;
            break;
        case Gravity.BOTTOM:
        case Gravity.BOTTOM | Gravity.LEFT:
            mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
            mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
            break;
        case Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM:
            mFirstCenterX = paddingStart + contentWidth * 0.5f -
                    mItemCenterOffset * (count - 1) * 0.5f;
            mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
            break;
        case Gravity.RIGHT | Gravity.BOTTOM:
            mFirstCenterX = width - paddingEnd - itemWidth * scale * 0.5f -
                    mItemCenterOffset * (count - 1);
            mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
            break;
        // 水平方向上沾满
        case Gravity.FILL:
            mItemCenterOffset = count == 1 ? 0 :
                    (contentWidth - itemWidth * scale) / (count - 1);
            mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
            mFirstCenterY = paddingTop + contentHeight * 0.5f;
            break;
        case Gravity.FILL_HORIZONTAL:
        case Gravity.FILL_HORIZONTAL | Gravity.TOP:
            mItemCenterOffset = count == 1 ? 0 :
                    (contentWidth - itemWidth * scale) / (count - 1);
            mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
            mFirstCenterY = paddingTop + itemHeight * scale * 0.5f;
            break;
        case Gravity.FILL_HORIZONTAL | Gravity.BOTTOM:
            mItemCenterOffset = count == 1 ? 0 :
                    (contentWidth - itemWidth * scale) / (count - 1);
            mFirstCenterX = paddingStart + itemWidth * scale * 0.5f;
            mFirstCenterY = height - paddingBottom - itemHeight * scale * 0.5f;
            break;
    }
}
 
Example 15
Source File: SimpleArgumentsBundle.java    From Rhythm with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc} Does a quick and rough parsing of the raw string for containing constant words like
 * <code>top</code> or <code>center_vertical</code>
 */
@Override
@SuppressLint("RtlHardcoded")
public int getGravity(String key, int defaultValue) {
    String gravityArg = resolveArgument(key);
    if (gravityArg == null) {
        return defaultValue;
    } else if (gravityArg.equals("center")) {
        return Gravity.CENTER;
    } else if (gravityArg.equals("fill")) {
        return Gravity.FILL;
    } else {
        // supported options
        int gravity = 0;
        if (gravityArg.contains("top")) {
            gravity |= Gravity.TOP;
        }
        if (gravityArg.contains("bottom")) {
            gravity |= Gravity.BOTTOM;
        }
        if (gravityArg.contains("center_vertical")) {
            gravity |= Gravity.CENTER_VERTICAL;
        }
        if (gravityArg.contains("fill_vertical")) {
            gravity |= Gravity.FILL_VERTICAL;
        }
        if (gravityArg.contains("left")) {
            gravity |= Gravity.LEFT;
        }
        if (gravityArg.contains("right")) {
            gravity |= Gravity.RIGHT;
        }
        if (gravityArg.contains("center_horizontal")) {
            gravity |= Gravity.CENTER_HORIZONTAL;
        }
        if (gravityArg.contains("fill_horizontal")) {
            gravity |= Gravity.FILL_HORIZONTAL;
        }
        return gravity;
    }
}
 
Example 16
Source File: SweetToast.java    From SweetTips with Apache License 2.0 4 votes vote down vote up
/**
     * 初始化
     * @param sweetToast
     * @param duration  自定义显示时间
     */
    private static void initConfiguration(SweetToast sweetToast,int duration){
        try {
            if(duration < 0){
                throw new RuntimeException("显示时长必须>=0!");
            }
            //1:初始化mWindowManager
            sweetToast.mWindowManager = (WindowManager) sweetToast.getContentView().getContext().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            //2:初始化mConfiguration
            SweetToastConfiguration mConfiguration = new SweetToastConfiguration();
            mConfiguration.setDuration(duration);
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            final Configuration config = sweetToast.getContentView().getContext().getResources().getConfiguration();
            final int gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
            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 = 0;
            params.y = sweetToast.getContentView().getContext().getResources().getDimensionPixelSize(R.dimen.toast_y_offset);
            params.verticalMargin = 0.0f;
            params.horizontalMargin = 0.0f;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.format = PixelFormat.TRANSLUCENT;
//            params.windowAnimations = R.style.Anim_SweetToast;
//            params.type = WindowManager.LayoutParams.TYPE_TOAST;
//            params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            params.type = WindowManager.LayoutParams.TYPE_PHONE;
            params.setTitle("Toast");
            params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
            mConfiguration.setParams(params);
            sweetToast.setConfiguration(mConfiguration);
        }catch (Exception e){
            Log.e("幻海流心","e:"+e.getLocalizedMessage());
        }
    }
 
Example 17
Source File: CommonLayoutParams.java    From tns-core-modules-widgets with Apache License 2.0 4 votes vote down vote up
private static int getMeasureSpec(View view, int parentMeasureSpec, boolean horizontal) {

        int parentLength = MeasureSpec.getSize(parentMeasureSpec);
        int parentSpecMode = MeasureSpec.getMode(parentMeasureSpec);

        CommonLayoutParams lp = (CommonLayoutParams) view.getLayoutParams();
        final int margins = horizontal ? lp.leftMargin + lp.rightMargin : lp.topMargin + lp.bottomMargin;

        int resultSize = 0;
        int resultMode = MeasureSpec.UNSPECIFIED;

        int measureLength = Math.max(0, parentLength - margins);
        int childLength = horizontal ? lp.width : lp.height;

        // We want a specific size... let be it.
        if (childLength >= 0) {
            if (parentSpecMode != MeasureSpec.UNSPECIFIED) {
                resultSize = Math.min(parentLength, childLength);
            } else {
                resultSize = childLength;
            }

            resultMode = MeasureSpec.EXACTLY;
        } else {
            switch (parentSpecMode) {
                // Parent has imposed an exact size on us
                case MeasureSpec.EXACTLY:
                    resultSize = measureLength;
                    int gravity = LayoutBase.getGravity(view);
                    boolean stretched;
                    if (horizontal) {
                        final int horizontalGravity = Gravity.getAbsoluteGravity(gravity, view.getLayoutDirection()) & Gravity.HORIZONTAL_GRAVITY_MASK;
                        stretched = horizontalGravity == Gravity.FILL_HORIZONTAL;
                    } else {
                        final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
                        stretched = verticalGravity == Gravity.FILL_VERTICAL;
                    }

                    // if stretched - view wants to be our size. So be it.
                    // else - view wants to determine its own size. It can't be bigger than us.
                    resultMode = stretched ? MeasureSpec.EXACTLY : MeasureSpec.AT_MOST;
                    break;

                // Parent has imposed a maximum size on us
                case MeasureSpec.AT_MOST:
                    resultSize = measureLength;
                    resultMode = MeasureSpec.AT_MOST;
                    break;

                case MeasureSpec.UNSPECIFIED:
                    resultSize = 0;
                    resultMode = MeasureSpec.UNSPECIFIED;
                    break;
            }
        }

        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }
 
Example 18
Source File: JsWindow.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
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();
	}
}
 
Example 19
Source File: ToastFactory.java    From DevUtils with Apache License 2.0 4 votes vote down vote up
/***
 * 显示 Toast 弹窗
 */
void show() {
    if (!mShow) {
        try {
            if (mToast == null) {
                return;
            }
            // 获取 View
            View view = mToast.getView();
            // 防止 View 为 null
            if (view == null) {
                return;
            }
            // 获取 Context
            Context context = view.getContext().getApplicationContext();
            if (context == null) {
                context = view.getContext();
            }
            // 获取包名
            String packageName = context.getPackageName();

            // =

            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            // 设置参数
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
                params.type = WindowManager.LayoutParams.TYPE_TOAST;
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
                params.type = WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
            } else {
                params.type = WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW + 37;
            }

            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.format = PixelFormat.TRANSLUCENT;
            params.windowAnimations = android.R.style.Animation_Toast;
            params.setTitle(Toast.class.getSimpleName());
            params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;

            // Toast 的重心
            int gravity = mToast.getGravity();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Configuration config = context.getResources().getConfiguration();
                gravity = Gravity.getAbsoluteGravity(gravity, config.getLayoutDirection());
            }
            if (gravity != 0) {
                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;
                }
            }
            // 设置 X、Y 轴偏移
            params.x = mToast.getXOffset();
            params.y = mToast.getYOffset();
            // 设置水平边距、垂直边距
            params.verticalMargin = mToast.getVerticalMargin();
            params.horizontalMargin = mToast.getHorizontalMargin();
            // 设置包名
            params.packageName = packageName;

            // View 对象不能重复添加, 否则会抛出异常
            getWindowManager(DevUtils.getTopActivity()).addView(mToast.getView(), params);
            // 当前已经显示
            mShow = true;
            // 添加一个移除 Toast 的任务
            sendEmptyMessageDelayed(0, mToast.getDuration() == Toast.LENGTH_LONG ? 3500 : 2000);
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "ToastHelper - show");
        }
    }
}
 
Example 20
Source File: Toast.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
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 */
        }
    }
}