RatioViewWrapper

public class RatioViewWrapper


A wrapper for a view, which enforces that its dimensions adhere to a set ratio if possible. Note that while multiple children can be added, only the first child will be measured, laid out, and drawn.

This will measure the child as normal, given the width/height MeasureSpecs assigned to this object. If either (or both) the width and the height for the child are inexact (i.e. WRAP_CONTENT), this wrapper will size those dimensions to be proportional to any known dimension.

As an example, say we add this wrapper to a FrameView, with width = MATCH_PARENT and height = WRAP_CONTENT, with a ratio of 2 (i.e. width is double height). In this case, it will measure its first child in the parent's bounds, as normal, then enforce that the height must be parentWidth / 2.

Note that if both axes are exact, this container does nothing; it will simply size the child and itself according to the exact MeasureSpecs.

Summary

Constants

static final @NonNull float

An undefined aspect ratio.

Public constructors

RatioViewWrapper(
    @NonNull Context context,
    @Nullable AttributeSet attributeSet
)
RatioViewWrapper(
    @NonNull Context context,
    @Nullable AttributeSet attributeSet,
    @AttrRes @NonNull int defStyleAttr
)
RatioViewWrapper(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    @AttrRes @NonNull int defStyleAttr,
    @StyleRes @NonNull int defStyleRes
)

Public methods

@NonNull float
@NonNull void
setAspectRatio(@NonNull float aspectRatio)

Sets the aspect ratio that this RatioViewWrapper should conform to.

@Override @NonNull void
setPadding(
    @NonNull int left,
    @NonNull int top,
    @NonNull int right,
    @NonNull int bottom
)
@Override @NonNull void
setPaddingRelative(
    @NonNull int start,
    @NonNull int top,
    @NonNull int end,
    @NonNull int bottom
)

Inherited methods

From class View
@NonNull void
@NonNull void
@NonNull void
@NonNull ViewPropertyAnimator
@NonNull void
@NonNull void
@NonNull void
@NonNull void

This method is deprecated.

@NonNull void
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
canScrollVertically(@NonNull int direction)
final @NonNull void
@NonNull void
final @NonNull void
@NonNull boolean
@NonNull void
static @NonNull int
combineMeasuredStates(@NonNull int curState, @NonNull int newState)
@NonNull void
@NonNull WindowInsets
computeSystemWindowInsets(
    @NonNull WindowInsets in,
    @NonNull Rect outLocalInsets
)
@NonNull AccessibilityNodeInfo
@NonNull void
@NonNull void

This method is deprecated.

@NonNull boolean
@NonNull boolean
dispatchNestedFling(
    @NonNull float velocityX,
    @NonNull float velocityY,
    @NonNull boolean consumed
)
@NonNull boolean
dispatchNestedPreFling(@NonNull float velocityX, @NonNull float velocityY)
@NonNull boolean
dispatchNestedPrePerformAccessibilityAction(
    @NonNull int action,
    @NonNull Bundle arguments
)
@NonNull boolean
dispatchNestedPreScroll(
    @NonNull int dx,
    @NonNull int dy,
    @NonNull Array<@NonNull int> consumed,
    @NonNull Array<@NonNull int> offsetInWindow
)
@NonNull boolean
dispatchNestedScroll(
    @NonNull int dxConsumed,
    @NonNull int dyConsumed,
    @NonNull int dxUnconsumed,
    @NonNull int dyUnconsumed,
    @NonNull Array<@NonNull int> offsetInWindow
)
@NonNull boolean
@NonNull void
draw(@NonNull Canvas canvas)
@NonNull void
drawableHotspotChanged(@NonNull float x, @NonNull float y)
final @NonNull T
<T extends View> findViewById(@NonNull int id)
final @NonNull T
<T extends View> findViewWithTag(@NonNull Object tag)
@NonNull void
forceHasOverlappingRendering(@NonNull boolean hasOverlappingRendering)
@NonNull void
static @NonNull int
@NonNull View.AccessibilityDelegate
@NonNull int
@NonNull AccessibilityNodeProvider
@NonNull CharSequence
@NonNull int
@NonNull int
@NonNull float
@NonNull Animation
@NonNull Matrix
@NonNull IBinder
@NonNull Array<@NonNull int>
@NonNull Map<@NonNull Integer, @NonNull Integer>
@NonNull Array<@NonNull String>
final @NonNull AutofillId
@NonNull int
@NonNull AutofillValue
@NonNull Drawable
@NonNull BlendMode
@NonNull ColorStateList
@NonNull PorterDuff.Mode
@NonNull int
final @NonNull int
@NonNull float
@NonNull Rect