Java Code Examples for android.view.Gravity#apply()

The following examples show how to use android.view.Gravity#apply() . 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: PinnedStackController.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * @return the default bounds to show the PIP, if a {@param snapFraction} is provided, then it
 * will apply the default bounds to the provided snap fraction.
 */
Rect getDefaultBounds(float snapFraction) {
    synchronized (mService.mWindowMap) {
        final Rect insetBounds = new Rect();
        getInsetBounds(insetBounds);

        final Rect defaultBounds = new Rect();
        final Size size = mSnapAlgorithm.getSizeForAspectRatio(mDefaultAspectRatio,
                mDefaultMinSize, mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight);
        if (snapFraction != INVALID_SNAP_FRACTION) {
            defaultBounds.set(0, 0, size.getWidth(), size.getHeight());
            final Rect movementBounds = getMovementBounds(defaultBounds);
            mSnapAlgorithm.applySnapFraction(defaultBounds, movementBounds, snapFraction);
        } else {
            Gravity.apply(mDefaultStackGravity, size.getWidth(), size.getHeight(), insetBounds,
                    0, Math.max(mIsImeShowing ? mImeHeight : 0,
                            mIsShelfShowing ? mShelfHeight : 0),
                    defaultBounds);
        }
        return defaultBounds;
    }
}
 
Example 2
Source File: DimensionsLabel.java    From Rhythm with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas, Rect drawableBounds) {
    final int intWidth = drawableBounds.width();
    // Make the label text based on width, height, and scale factor
    String text = prettyPrintDips(intWidth, mScaleFactor) + ' ' + MULTIPLY + ' '
            + prettyPrintDips(drawableBounds.height(), mScaleFactor);

    // Use StaticLayout, which will calculate text dimensions nicely, then position the box using Gravity.apply()
    // (although that's one instantiation per draw call...)
    // This is what happens if you're obsessed with perfection like me
    StaticLayout layout = new StaticLayout(text, mTextPaint, intWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);
    Gravity.apply(mGravity, (int) (layout.getLineMax(0) + 0.5), layout.getHeight(), drawableBounds, mTemp);

    // Draw background
    canvas.drawRect(mTemp, mBackgroundPaint);

    // We have to translate the canvas ourselves, since layout can only draw itself at (0, 0)
    canvas.save();
    canvas.translate(mTemp.left, mTemp.top);
    layout.draw(canvas);
    canvas.restore();
}
 
Example 3
Source File: CardContainer.java    From giraff-android with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
	super.onLayout(changed, l, t, r, b);

	for (int i = 0; i < getChildCount(); i++) {
		boundsRect.set(0, 0, getWidth(), getHeight());

		View view = getChildAt(i);
		int w, h;
		w = view.getMeasuredWidth();
		h = view.getMeasuredHeight();

		Gravity.apply(mGravity, w, h, boundsRect, childRect);
		view.layout(childRect.left, childRect.top, childRect.right, childRect.bottom);
	}
}
 
Example 4
Source File: ScaledFrameLayout.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Position all children within this layout.
 */
@Override
protected void onLayout(boolean changed, int left, int top, int right,
                        int bottom) {
  final int count = getChildCount();

  int leftPos = getPaddingLeft();

  final int parentTop = getPaddingTop();
  final int parentBottom = bottom - top - getPaddingBottom();

  for (int i = 0; i < count; i++) {
    final View child = getChildAt(i);
    if (child.getVisibility() != GONE) {
      final int width = child.getMeasuredWidth();
      final int height = child.getMeasuredHeight();

      mTmpContainerRect.left = leftPos;
      mTmpContainerRect.right = leftPos;
      leftPos = mTmpContainerRect.right;
      mTmpContainerRect.top = parentTop;
      mTmpContainerRect.bottom = parentBottom;

      Gravity.apply(Gravity.TOP | Gravity.LEFT, width, height,
                    mTmpContainerRect, mTmpChildRect);

      child.layout(mTmpChildRect.left, mTmpChildRect.top,
                   mTmpChildRect.right, mTmpChildRect.bottom);
    }
  }
}
 
Example 5
Source File: ForegroundLinearLayout.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    if (mForeground != null) {
        final Drawable foreground = mForeground;

        if (mForegroundBoundsChanged) {
            mForegroundBoundsChanged = false;
            final Rect selfBounds = mSelfBounds;
            final Rect overlayBounds = mOverlayBounds;

            final int w = getRight() - getLeft();
            final int h = getBottom() - getTop();

            if (mForegroundInPadding) {
                selfBounds.set(0, 0, w, h);
            } else {
                selfBounds.set(getPaddingLeft(), getPaddingTop(),
                        w - getPaddingRight(), h - getPaddingBottom());
            }

            Gravity.apply(mForegroundGravity, foreground.getIntrinsicWidth(),
                    foreground.getIntrinsicHeight(), selfBounds, overlayBounds);
            foreground.setBounds(overlayBounds);
        }

        foreground.draw(canvas);
    }
}
 
Example 6
Source File: BadgedFourThreeImageView.java    From android-proguards with Apache License 2.0 5 votes vote down vote up
private void layoutBadge() {
    Rect badgeBounds = badge.getBounds();
    Gravity.apply(badgeGravity,
            badge.getIntrinsicWidth(),
            badge.getIntrinsicHeight(),
            new Rect(0, 0, getWidth(), getHeight()),
            badgePadding,
            badgePadding,
            badgeBounds);
    badge.setBounds(badgeBounds);
    badgeBoundsSet = true;
}
 
Example 7
Source File: WrapperTarget.java    From glide-support with The Unlicense 5 votes vote down vote up
/** Align drawable in wrapper in case the image is smaller than the target size. */
private Rect calcBounds(Drawable drawable, int gravity) {
	Rect bounds = new Rect();
	int w = drawable.getIntrinsicWidth();
	int h = drawable.getIntrinsicHeight();
	Rect container = wrapper.getBounds();
	if (w == -1 && h == -1) {
		w = container.width();
		h = container.height();
	}
	Gravity.apply(gravity, w, h, container, bounds);
	return bounds;
}
 
Example 8
Source File: QiscusFlowLayout.java    From qiscus-sdk-android with Apache License 2.0 5 votes vote down vote up
private void applyGravityToLines(List<LineDefinition> lines, int realControlLength, int realControlThickness) {
    final int linesCount = lines.size();
    if (linesCount <= 0) {
        return;
    }

    final int totalWeight = linesCount;
    LineDefinition lastLine = lines.get(linesCount - 1);
    int excessThickness = realControlThickness - (lastLine.getLineThickness() + lastLine.getLineStartThickness());

    if (excessThickness < 0) {
        excessThickness = 0;
    }

    int excessOffset = 0;
    for (int i = 0; i < linesCount; i++) {
        final LineDefinition child = lines.get(i);
        int weight = 1;
        int gravity = this.getGravity(null);
        int extraThickness = Math.round(excessThickness * weight / totalWeight);

        final int childLength = child.getLineLength();
        final int childThickness = child.getLineThickness();

        Rect container = new Rect();
        container.top = excessOffset;
        container.left = 0;
        container.right = realControlLength;
        container.bottom = childThickness + extraThickness + excessOffset;

        Rect result = new Rect();
        Gravity.apply(gravity, childLength, childThickness, container, result);

        excessOffset += extraThickness;
        child.setLineStartLength(child.getLineStartLength() + result.left);
        child.setLineStartThickness(child.getLineStartThickness() + result.top);
        child.setLength(result.width());
        child.setThickness(result.height());
    }
}
 
Example 9
Source File: ForegroundLinearLayout.java    From Mizuu with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    if (mForeground != null) {
        final Drawable foreground = mForeground;

        if (mForegroundBoundsChanged) {
            mForegroundBoundsChanged = false;
            final Rect selfBounds = mSelfBounds;
            final Rect overlayBounds = mOverlayBounds;

            final int w = getRight() - getLeft();
            final int h = getBottom() - getTop();

            if (mForegroundInPadding) {
                selfBounds.set(0, 0, w, h);
            } else {
                selfBounds.set(getPaddingLeft(), getPaddingTop(),
                        w - getPaddingRight(), h - getPaddingBottom());
            }

            Gravity.apply(mForegroundGravity, foreground.getIntrinsicWidth(),
                    foreground.getIntrinsicHeight(), selfBounds, overlayBounds);
            foreground.setBounds(overlayBounds);
        }

        foreground.draw(canvas);
    }
}
 
Example 10
Source File: Compat.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
static void apply(Drawable drawable, int gravity, int w, int h, Rect container, Rect outRect) {
    if (drawable == null)
        return;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        Gravity.apply(gravity, w, h, container, outRect, drawable.getLayoutDirection());
    else
        Gravity.apply(gravity, w, h, container, outRect);
}
 
Example 11
Source File: AnimatedScaleDrawable.java    From android-animatedscaledrawable with Apache License 2.0 5 votes vote down vote up
@Override
protected void onBoundsChange(Rect bounds)
{
	if(mState.mDrawable != null){
		if(mState.mUseBounds){
			mState.mDrawable.setBounds(bounds);
		}
		else{
			Gravity.apply(Gravity.CENTER, getIntrinsicWidth(), 
				getIntrinsicHeight(), bounds, mTmpRect);
			mState.mDrawable.setBounds(mTmpRect);
		}
	}
}
 
Example 12
Source File: Compat.java    From ProjectX with Apache License 2.0 5 votes vote down vote up
static void apply(int gravity, int w, int h, Rect container,
                         Rect outRect, int layoutDirection) {
    if (SDK_INT >= 17) {
        Gravity.apply(gravity, w, h, container, outRect, layoutDirection);
    } else {
        Gravity.apply(gravity, w, h, container, outRect);
    }
}
 
Example 13
Source File: GlideBitmapDrawable.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    if (applyGravity) {
        Gravity.apply(BitmapState.GRAVITY, width, height, getBounds(), destRect);
        applyGravity = false;
    }
    canvas.drawBitmap(state.bitmap, null, destRect, state.paint);
}
 
Example 14
Source File: GravityCompatJellybeanMr1.java    From guideshow with MIT License 4 votes vote down vote up
public static void apply(int gravity, int w, int h, Rect container, Rect outRect,
        int layoutDirection) {
    Gravity.apply(gravity, w, h, container, outRect, layoutDirection);
}
 
Example 15
Source File: GravityCompat.java    From letv with Apache License 2.0 4 votes vote down vote up
public void apply(int gravity, int w, int h, Rect container, int xAdj, int yAdj, Rect outRect, int layoutDirection) {
    Gravity.apply(gravity, w, h, container, xAdj, yAdj, outRect);
}
 
Example 16
Source File: GravityCompatJellybeanMr1.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public static void apply(int gravity, int w, int h, Rect container, Rect outRect,
        int layoutDirection) {
    Gravity.apply(gravity, w, h, container, outRect, layoutDirection);
}
 
Example 17
Source File: GravityCompat.java    From adt-leanback-support with Apache License 2.0 4 votes vote down vote up
@Override
public void apply(int gravity, int w, int h, Rect container, Rect outRect,
        int layoutDirection) {
    Gravity.apply(gravity, w, h, container, outRect);
}
 
Example 18
Source File: QiscusFlowLayout.java    From qiscus-sdk-android with Apache License 2.0 4 votes vote down vote up
private void applyGravityToLine(LineDefinition line) {
    final List<View> views = line.getViews();
    final int viewCount = views.size();
    if (viewCount <= 0) {
        return;
    }

    float totalWeight = 0;
    for (int i = 0; i < viewCount; i++) {
        final View prev = views.get(i);
        LayoutParams plp = (LayoutParams) prev.getLayoutParams();
        totalWeight += this.getWeight(plp);
    }

    View lastChild = views.get(viewCount - 1);
    LayoutParams lastChildLayoutParams = (LayoutParams) lastChild.getLayoutParams();
    int excessLength = line.getLineLength() - (lastChildLayoutParams.getLength() + lastChildLayoutParams.getSpacingLength() +
            lastChildLayoutParams.getInlineStartLength());
    int excessOffset = 0;
    for (int i = 0; i < viewCount; i++) {
        final View child = views.get(i);
        LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();

        float weight = this.getWeight(layoutParams);
        int gravity = this.getGravity(layoutParams);
        int extraLength;
        if (totalWeight == 0) {
            extraLength = excessLength / viewCount;
        } else {
            extraLength = Math.round(excessLength * weight / totalWeight);
        }

        final int childLength = layoutParams.getLength() + layoutParams.getSpacingLength();
        final int childThickness = layoutParams.getThickness() + layoutParams.getSpacingThickness();

        Rect container = new Rect();
        container.top = 0;
        container.left = excessOffset;
        container.right = childLength + extraLength + excessOffset;
        container.bottom = line.getLineThickness();

        Rect result = new Rect();
        Gravity.apply(gravity, childLength, childThickness, container, result);

        excessOffset += extraLength;
        layoutParams.setInlineStartLength(result.left + layoutParams.getInlineStartLength());
        layoutParams.setInlineStartThickness(result.top);
        layoutParams.setLength(result.width() - layoutParams.getSpacingLength());
        layoutParams.setThickness(result.height() - layoutParams.getSpacingThickness());
    }
}
 
Example 19
Source File: CommonLogic.java    From Last-Launcher with GNU General Public License v3.0 4 votes vote down vote up
public static void applyGravityToLines(List<LineDefinition> lines, int realControlLength, int realControlThickness, ConfigDefinition config) {
    final int linesCount = lines.size();
    if (linesCount <= 0) {
        return;
    }

    int remainingWeight = linesCount;
    LineDefinition lastLine = lines.get(linesCount - 1);
    int excessThickness = realControlThickness - (lastLine.getLineThickness() + lastLine.getLineStartThickness());

    if (excessThickness < 0) {
        excessThickness = 0;
    }

    int excessOffset = 0;
    for (int i = 0; i < linesCount; i++) {
        final LineDefinition child = lines.get(i);
        int weight = 1;
        int gravity = getGravity(null, config);
        int extraThickness = Math.round(excessThickness * weight / remainingWeight);

        excessThickness -= extraThickness;
        remainingWeight -= weight;

        final int childLength = child.getLineLength();
        final int childThickness = child.getLineThickness();

        Rect container = new Rect();
        container.top = excessOffset;
        container.left = 0;
        container.right = realControlLength;
        container.bottom = childThickness + extraThickness + excessOffset;

        Rect result = new Rect();
        Gravity.apply(gravity, childLength, childThickness, container, result);

        excessOffset += extraThickness;
        child.setLineStartLength(child.getLineStartLength() + result.left);
        child.setLineStartThickness(child.getLineStartThickness() + result.top);
        child.setLength(result.width());
        child.setThickness(result.height());

        applyGravityToLine(child, config);
    }
}
 
Example 20
Source File: GravityUtils.java    From GestureViews with Apache License 2.0 2 votes vote down vote up
/**
 * Calculates movement area position within viewport area with gravity applied.
 *
 * @param settings Image settings
 * @param out Output rectangle
 */
public static void getMovementAreaPosition(Settings settings, Rect out) {
    tmpRect1.set(0, 0, settings.getViewportW(), settings.getViewportH());
    Gravity.apply(settings.getGravity(),
            settings.getMovementAreaW(), settings.getMovementAreaH(), tmpRect1, out);
}