1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package androidx.leanback.widget; 15 16 import static androidx.leanback.widget.ItemAlignmentFacet.ITEM_ALIGN_OFFSET_PERCENT_DISABLED; 17 import static androidx.recyclerview.widget.RecyclerView.HORIZONTAL; 18 19 import android.graphics.Rect; 20 import android.view.View; 21 import android.view.ViewGroup; 22 23 import androidx.leanback.widget.GridLayoutManager.LayoutParams; 24 25 /** 26 * Helper class to handle ItemAlignmentFacet in a grid view. 27 */ 28 class ItemAlignmentFacetHelper { 29 30 private static Rect sRect = new Rect(); 31 32 /** 33 * get alignment position relative to optical left/top of itemView. 34 */ 35 static int getAlignmentPosition(View itemView, ItemAlignmentFacet.ItemAlignmentDef facet, 36 int orientation) { 37 LayoutParams p = (LayoutParams) itemView.getLayoutParams(); 38 View view = itemView; 39 if (facet.mViewId != 0) { 40 view = itemView.findViewById(facet.mViewId); 41 if (view == null) { 42 view = itemView; 43 } 44 } 45 int alignPos = facet.mOffset; 46 if (orientation == HORIZONTAL) { 47 if (itemView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) { 48 alignPos = (view == itemView ? p.getOpticalWidth(view) 49 : view.getWidth()) - alignPos; 50 if (facet.mOffsetWithPadding) { 51 if (facet.mOffsetPercent == 0f) { 52 alignPos -= view.getPaddingRight(); 53 } else if (facet.mOffsetPercent == 100f) { 54 alignPos += view.getPaddingLeft(); 55 } 56 } 57 if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) { 58 alignPos -= (int) (((view == itemView ? p.getOpticalWidth(view) 59 : view.getWidth()) * facet.mOffsetPercent) / 100f); 60 } 61 if (itemView != view) { 62 sRect.right = alignPos; 63 ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect); 64 alignPos = sRect.right + p.getOpticalRightInset(); 65 } 66 } else { 67 if (facet.mOffsetWithPadding) { 68 if (facet.mOffsetPercent == 0f) { 69 alignPos += view.getPaddingLeft(); 70 } else if (facet.mOffsetPercent == 100f) { 71 alignPos -= view.getPaddingRight(); 72 } 73 } 74 if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) { 75 alignPos += (int) (((view == itemView ? p.getOpticalWidth(view) 76 : view.getWidth()) * facet.mOffsetPercent) / 100f); 77 } 78 if (itemView != view) { 79 sRect.left = alignPos; 80 ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect); 81 alignPos = sRect.left - p.getOpticalLeftInset(); 82 } 83 } 84 } else { 85 if (facet.mOffsetWithPadding) { 86 if (facet.mOffsetPercent == 0f) { 87 alignPos += view.getPaddingTop(); 88 } else if (facet.mOffsetPercent == 100f) { 89 alignPos -= view.getPaddingBottom(); 90 } 91 } 92 if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) { 93 alignPos += (int) (((view == itemView ? p.getOpticalHeight(view) : view.getHeight()) 94 * facet.mOffsetPercent) / 100f); 95 } 96 if (itemView != view) { 97 sRect.top = alignPos; 98 ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect); 99 alignPos = sRect.top - p.getOpticalTopInset(); 100 } 101 if (facet.isAlignedToTextViewBaseLine()) { 102 alignPos += view.getBaseline(); 103 } 104 } 105 return alignPos; 106 } 107 108 private ItemAlignmentFacetHelper() { 109 } 110 } 111