• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.support.v17.leanback.widget;
15 
16 import static android.support.v17.leanback.widget.ItemAlignmentFacet.ITEM_ALIGN_OFFSET_PERCENT_DISABLED;
17 import static android.support.v7.widget.RecyclerView.HORIZONTAL;
18 
19 import android.graphics.Paint;
20 import android.graphics.Rect;
21 import android.support.v17.leanback.widget.GridLayoutManager.LayoutParams;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.TextView;
25 
26 /**
27  * Helper class to handle ItemAlignmentFacet in a grid view.
28  */
29 class ItemAlignmentFacetHelper {
30 
31     private static Rect sRect = new Rect();
32 
33     /**
34      * get alignment position relative to optical left/top of itemView.
35      */
getAlignmentPosition(View itemView, ItemAlignmentFacet.ItemAlignmentDef facet, int orientation)36     static int getAlignmentPosition(View itemView, ItemAlignmentFacet.ItemAlignmentDef facet,
37             int orientation) {
38         LayoutParams p = (LayoutParams) itemView.getLayoutParams();
39         View view = itemView;
40         if (facet.mViewId != 0) {
41             view = itemView.findViewById(facet.mViewId);
42             if (view == null) {
43                 view = itemView;
44             }
45         }
46         int alignPos = facet.mOffset;
47         if (orientation == HORIZONTAL) {
48             if (facet.mOffset >= 0) {
49                 if (facet.mOffsetWithPadding) {
50                     alignPos += view.getPaddingLeft();
51                 }
52             } else {
53                 if (facet.mOffsetWithPadding) {
54                     alignPos -= view.getPaddingRight();
55                 }
56             }
57             if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
58                 alignPos += ((view == itemView ? p.getOpticalWidth(view) : view.getWidth())
59                         * facet.mOffsetPercent) / 100f;
60             }
61             if (itemView != view) {
62                 sRect.left = alignPos;
63                 ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect);
64                 alignPos = sRect.left - p.getOpticalLeftInset();
65             }
66         } else {
67             if (facet.mOffset >= 0) {
68                 if (facet.mOffsetWithPadding) {
69                     alignPos += view.getPaddingTop();
70                 }
71             } else {
72                 if (facet.mOffsetWithPadding) {
73                     alignPos -= view.getPaddingBottom();
74                 }
75             }
76             if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
77                 alignPos += ((view == itemView ? p.getOpticalHeight(view) : view.getHeight())
78                         * facet.mOffsetPercent) / 100f;
79             }
80             if (itemView != view) {
81                 sRect.top = alignPos;
82                 ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect);
83                 alignPos = sRect.top - p.getOpticalTopInset();
84             }
85             if (view instanceof TextView && facet.isAlignedToTextViewBaseLine()) {
86                 Paint textPaint = ((TextView)view).getPaint();
87                 int titleViewTextHeight = -textPaint.getFontMetricsInt().top;
88                 alignPos += titleViewTextHeight;
89             }
90         }
91         return alignPos;
92     }
93 
94 }
95