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