Java Code Examples for androidx.core.math.MathUtils#clamp()
The following examples show how to use
androidx.core.math.MathUtils#clamp() .
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: HeaderBehavior.java From material-components-android with Apache License 2.0 | 6 votes |
int setHeaderTopBottomOffset( CoordinatorLayout parent, V header, int newOffset, int minOffset, int maxOffset) { final int curOffset = getTopAndBottomOffset(); int consumed = 0; if (minOffset != 0 && curOffset >= minOffset && curOffset <= maxOffset) { // If we have some scrolling range, and we're currently within the min and max // offsets, calculate a new offset newOffset = MathUtils.clamp(newOffset, minOffset, maxOffset); if (curOffset != newOffset) { setTopAndBottomOffset(newOffset); // Update how much dy we have consumed consumed = curOffset - newOffset; } } return consumed; }
Example 2
Source File: SubtitleCollapsingTextHelper.java From collapsingtoolbarlayout-subtitle with Apache License 2.0 | 5 votes |
/** * Set the value indicating the current scroll value. This decides how much of the background will * be displayed, as well as the title metrics/positioning. * * <p>A value of {@code 0.0} indicates that the layout is fully expanded. A value of {@code 1.0} * indicates that the layout is fully collapsed. */ public void setExpansionFraction(float fraction) { fraction = MathUtils.clamp(fraction, 0f, 1f); if (fraction != expandedFraction) { expandedFraction = fraction; calculateCurrentOffsets(); } }
Example 3
Source File: RadialTimePickerView.java From DateTimePicker with Apache License 2.0 | 5 votes |
private void adjustPicker(int step) { final int stepSize; final int initialStep; final int maxValue; final int minValue; if (mShowHours) { stepSize = 1; final int currentHour24 = getCurrentHour(); if (mIs24HourMode) { initialStep = currentHour24; minValue = 0; maxValue = 23; } else { initialStep = hour24To12(currentHour24); minValue = 1; maxValue = 12; } } else { stepSize = 5; initialStep = getCurrentMinute() / stepSize; minValue = 0; maxValue = 55; } final int nextValue = (initialStep + step) * stepSize; final int clampedValue = MathUtils.clamp(nextValue, minValue, maxValue);//MathUtils.constrain(nextValue, minValue, maxValue); if (mShowHours) { setCurrentHour(clampedValue); } else { setCurrentMinute(clampedValue); } }
Example 4
Source File: TocFragment.java From material-components-android with Apache License 2.0 | 5 votes |
private int calculateGridSpanCount() { DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); int displayWidth = displayMetrics.widthPixels; int itemSize = getResources().getDimensionPixelSize(R.dimen.cat_toc_item_size); int gridSpanCount = displayWidth / itemSize; return MathUtils.clamp(gridSpanCount, GRID_SPAN_COUNT_MIN, GRID_SPAN_COUNT_MAX); }
Example 5
Source File: AppBarLayout.java From material-components-android with Apache License 2.0 | 5 votes |
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // If we're set to handle system windows but our first child is not, we need to add some // height to ourselves to pad the first child down below the status bar final int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode != MeasureSpec.EXACTLY && ViewCompat.getFitsSystemWindows(this) && shouldOffsetFirstChild()) { int newHeight = getMeasuredHeight(); switch (heightMode) { case MeasureSpec.AT_MOST: // For AT_MOST, we need to clamp our desired height with the max height newHeight = MathUtils.clamp( getMeasuredHeight() + getTopInset(), 0, MeasureSpec.getSize(heightMeasureSpec)); break; case MeasureSpec.UNSPECIFIED: // For UNSPECIFIED we can use any height so just add the top inset newHeight += getTopInset(); break; default: // fall out } setMeasuredDimension(getMeasuredWidth(), newHeight); } invalidateScrollRanges(); }
Example 6
Source File: CollapsingTextHelper.java From material-components-android with Apache License 2.0 | 5 votes |
/** * Set the value indicating the current scroll value. This decides how much of the background will * be displayed, as well as the title metrics/positioning. * * <p>A value of {@code 0.0} indicates that the layout is fully expanded. A value of {@code 1.0} * indicates that the layout is fully collapsed. */ public void setExpansionFraction(float fraction) { fraction = MathUtils.clamp(fraction, 0f, 1f); if (fraction != expandedFraction) { expandedFraction = fraction; calculateCurrentOffsets(); } }
Example 7
Source File: DragDropHelper.java From RecyclerViewExtensions with MIT License | 5 votes |
/** * Offsets the dragged view based on the touch position, clamping its location to the parent. */ private void handleTranslation(@NonNull RecyclerView.ViewHolder viewHolder, int startLeft, int startTop) { int width = viewHolder.itemView.getWidth(); int height = viewHolder.itemView.getHeight(); int maxLeft = mRecyclerView.getWidth() - width; int maxTop = mRecyclerView.getHeight() - height; int left = MathUtils.clamp(startLeft + (mTouchCurrentX - mTouchStartX), 0, maxLeft); int top = MathUtils.clamp(startTop + (mTouchCurrentY - mTouchStartY), 0, maxTop); setTranslation(viewHolder, left - viewHolder.itemView.getLeft(), top - viewHolder.itemView.getTop()); }
Example 8
Source File: FastScroller.java From AndroidFastScroll with Apache License 2.0 | 4 votes |
private void onPreDraw() { updateScrollbarState(); mTrackView.setVisibility(mScrollbarEnabled ? View.VISIBLE : View.INVISIBLE); mThumbView.setVisibility(mScrollbarEnabled ? View.VISIBLE : View.INVISIBLE); if (!mScrollbarEnabled) { mPopupView.setVisibility(View.INVISIBLE); return; } int layoutDirection = mView.getLayoutDirection(); mTrackView.setLayoutDirection(layoutDirection); mThumbView.setLayoutDirection(layoutDirection); mPopupView.setLayoutDirection(layoutDirection); boolean isLayoutRtl = layoutDirection == View.LAYOUT_DIRECTION_RTL; int viewWidth = mView.getWidth(); int viewHeight = mView.getHeight(); Rect padding = getPadding(); int trackLeft = isLayoutRtl ? padding.left : viewWidth - padding.right - mTrackWidth; layoutView(mTrackView, trackLeft, padding.top, trackLeft + mTrackWidth, viewHeight - padding.bottom); int thumbLeft = isLayoutRtl ? padding.left : viewWidth - padding.right - mThumbWidth; int thumbTop = padding.top + mThumbOffset; layoutView(mThumbView, thumbLeft, thumbTop, thumbLeft + mThumbWidth, thumbTop + mThumbHeight); String popupText = mViewHelper.getPopupText(); boolean hasPopup = !TextUtils.isEmpty(popupText); mPopupView.setVisibility(hasPopup ? View.VISIBLE : View.INVISIBLE); if (hasPopup) { FrameLayout.LayoutParams popupLayoutParams = (FrameLayout.LayoutParams) mPopupView.getLayoutParams(); if (!Objects.equals(mPopupView.getText(), popupText)) { mPopupView.setText(popupText); int widthMeasureSpec = ViewGroup.getChildMeasureSpec( View.MeasureSpec.makeMeasureSpec(viewWidth, View.MeasureSpec.EXACTLY), padding.left + padding.right + mThumbWidth + popupLayoutParams.leftMargin + popupLayoutParams.rightMargin, popupLayoutParams.width); int heightMeasureSpec = ViewGroup.getChildMeasureSpec( View.MeasureSpec.makeMeasureSpec(viewHeight, View.MeasureSpec.EXACTLY), padding.top + padding.bottom + popupLayoutParams.topMargin + popupLayoutParams.bottomMargin, popupLayoutParams.height); mPopupView.measure(widthMeasureSpec, heightMeasureSpec); } int popupWidth = mPopupView.getMeasuredWidth(); int popupHeight = mPopupView.getMeasuredHeight(); int popupLeft = isLayoutRtl ? padding.left + mThumbWidth + popupLayoutParams.leftMargin : viewWidth - padding.right - mThumbWidth - popupLayoutParams.rightMargin - popupWidth; int popupAnchorY; switch (popupLayoutParams.gravity & Gravity.HORIZONTAL_GRAVITY_MASK) { case Gravity.LEFT: default: popupAnchorY = 0; break; case Gravity.CENTER_HORIZONTAL: popupAnchorY = popupHeight / 2; break; case Gravity.RIGHT: popupAnchorY = popupHeight; break; } int thumbAnchorY; switch (popupLayoutParams.gravity & Gravity.VERTICAL_GRAVITY_MASK) { case Gravity.TOP: default: thumbAnchorY = mThumbView.getPaddingTop(); break; case Gravity.CENTER_VERTICAL: { int thumbPaddingTop = mThumbView.getPaddingTop(); thumbAnchorY = thumbPaddingTop + (mThumbHeight - thumbPaddingTop - mThumbView.getPaddingBottom()) / 2; break; } case Gravity.BOTTOM: thumbAnchorY = mThumbHeight - mThumbView.getPaddingBottom(); break; } int popupTop = MathUtils.clamp(thumbTop + thumbAnchorY - popupAnchorY, padding.top + popupLayoutParams.topMargin, viewHeight - padding.bottom - popupLayoutParams.bottomMargin - popupHeight); layoutView(mPopupView, popupLeft, popupTop, popupLeft + popupWidth, popupTop + popupHeight); } }
Example 9
Source File: FastScroller.java From AndroidFastScroll with Apache License 2.0 | 4 votes |
private void scrollToThumbOffset(int thumbOffset) { int thumbOffsetRange = getThumbOffsetRange(); thumbOffset = MathUtils.clamp(thumbOffset, 0, thumbOffsetRange); int scrollOffset = (int) ((long) getScrollOffsetRange() * thumbOffset / thumbOffsetRange); mViewHelper.scrollTo(scrollOffset); }
Example 10
Source File: BottomSheetBehavior.java From material-components-android with Apache License 2.0 | 4 votes |
@Override public int clampViewPositionVertical(@NonNull View child, int top, int dy) { return MathUtils.clamp( top, getExpandedOffset(), hideable ? parentHeight : collapsedOffset); }
Example 11
Source File: HeaderScrollingViewBehavior.java From material-components-android with Apache License 2.0 | 4 votes |
final int getOverlapPixelsForOffset(final View header) { return overlayTop == 0 ? 0 : MathUtils.clamp((int) (getOverlapRatioForOffset(header) * overlayTop), 0, overlayTop); }
Example 12
Source File: AppBarLayout.java From material-components-android with Apache License 2.0 | 4 votes |
@Override int setHeaderTopBottomOffset( @NonNull CoordinatorLayout coordinatorLayout, @NonNull T appBarLayout, int newOffset, int minOffset, int maxOffset) { final int curOffset = getTopBottomOffsetForScrollingSibling(); int consumed = 0; if (minOffset != 0 && curOffset >= minOffset && curOffset <= maxOffset) { // If we have some scrolling range, and we're currently within the min and max // offsets, calculate a new offset newOffset = MathUtils.clamp(newOffset, minOffset, maxOffset); if (curOffset != newOffset) { final int interpolatedOffset = appBarLayout.hasChildWithInterpolator() ? interpolateOffset(appBarLayout, newOffset) : newOffset; final boolean offsetChanged = setTopAndBottomOffset(interpolatedOffset); // Update how much dy we have consumed consumed = curOffset - newOffset; // Update the stored sibling offset offsetDelta = newOffset - interpolatedOffset; if (!offsetChanged && appBarLayout.hasChildWithInterpolator()) { // If the offset hasn't changed and we're using an interpolated scroll // then we need to keep any dependent views updated. CoL will do this for // us when we move, but we need to do it manually when we don't (as an // interpolated scroll may finish early). coordinatorLayout.dispatchDependentViewsChanged(appBarLayout); } // Dispatch the updates to any listeners appBarLayout.onOffsetChanged(getTopAndBottomOffset()); // Update the AppBarLayout's drawable state (for any elevation changes) updateAppBarLayoutDrawableState( coordinatorLayout, appBarLayout, newOffset, newOffset < curOffset ? -1 : 1, false /* forceJump */); } } else { // Reset the offset delta offsetDelta = 0; } updateAccessibilityActions(coordinatorLayout, appBarLayout); return consumed; }
Example 13
Source File: BottomSheetBehavior.java From Mysplash with GNU Lesser General Public License v3.0 | 4 votes |
@Override public int clampViewPositionVertical(@NonNull View child, int top, int dy) { return MathUtils.clamp( top, getExpandedOffset(), hideable ? parentHeight : collapsedOffset); }
Example 14
Source File: VolumeInfo.java From ARVI with Apache License 2.0 | 2 votes |
/** * Sets the Audio Volume. * * @param audioVolume the audio volume ratio (a value between 0.0 and 1.0) * @return the same instance of the {@link VolumeInfo} for chaining purposes. */ @NonNull public final VolumeInfo setVolume(@FloatRange(from = 0.0, to = 1.0) float audioVolume) { this.audioVolume = MathUtils.clamp(audioVolume, 0f, 1f); return this; }