ItemAlignment.java revision 8b068ddbbf22a246eab49ec25a2f7c3abfbdca51
1/*
2 * Copyright (C) 2014 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
15package android.support.v17.leanback.widget;
16
17import static android.support.v7.widget.RecyclerView.HORIZONTAL;
18import static android.support.v7.widget.RecyclerView.VERTICAL;
19import static android.support.v17.leanback.widget.BaseListView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED;
20
21import android.graphics.Rect;
22import android.view.View;
23import android.view.ViewGroup;
24
25/**
26 * Defines alignment position on two directions of an item view. Typically item
27 * view alignment is at the center of the view. The class allows defining
28 * alignment at left/right or fixed offset/percentage position; it also allows
29 * using descendant view by id match.
30 */
31class ItemAlignment {
32
33    final static class Axis {
34        private int mOrientation;
35        private int mOffset = 0;
36        private float mOffsetPercent = 50;
37        private int mViewId = 0;
38        private Rect mRect = new Rect();
39
40        Axis(int orientation) {
41            mOrientation = orientation;
42        }
43
44        public void setItemAlignmentOffset(int offset) {
45            mOffset = offset;
46        }
47
48        public int getItemAlignmentOffset() {
49            return mOffset;
50        }
51
52        public void setItemAlignmentOffsetPercent(float percent) {
53            if ( (percent < 0 || percent > 100) &&
54                    percent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
55                throw new IllegalArgumentException();
56            }
57            mOffsetPercent = percent;
58        }
59
60        public float getItemAlignmentOffsetPercent() {
61            return mOffsetPercent;
62        }
63
64        public void setItemAlignmentViewId(int viewId) {
65            mViewId = viewId;
66        }
67
68        public int getItemAlignmentViewId() {
69            return mViewId;
70        }
71
72        public int getAlignmentPosition(View itemView, GridLayoutManagerChildTag tag) {
73
74            View view = itemView;
75            if (mViewId != 0) {
76                view = itemView.findViewById(mViewId);
77                if (view == null) {
78                    view = itemView;
79                }
80            }
81            int alignPos;
82            if (mOrientation == HORIZONTAL) {
83                if (mOffset >= 0) {
84                    alignPos = mOffset;
85                } else {
86                    alignPos = tag.getOpticalWidth() + mOffset;
87                }
88                if (mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
89                    alignPos += (tag.getOpticalWidth() * mOffsetPercent) / 100;
90                }
91                if (itemView != view) {
92                    mRect.left = alignPos;
93                    ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, mRect);
94                    alignPos = mRect.left;
95                }
96            } else {
97                if (mOffset >= 0) {
98                    alignPos = mOffset;
99                } else {
100                    alignPos = tag.getOpticalHeight() + mOffset;
101                }
102                if (mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
103                    alignPos += (tag.getOpticalHeight() * mOffsetPercent) / 100;
104                }
105                if (itemView != view) {
106                    mRect.top = alignPos;
107                    ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, mRect);
108                    alignPos = mRect.top;
109                }
110            }
111            return alignPos;
112        }
113    }
114
115    private int mOrientation = HORIZONTAL;
116
117    final public Axis vertical = new Axis(VERTICAL);
118
119    final public Axis horizontal = new Axis(HORIZONTAL);
120
121    private Axis mMainAxis = horizontal;
122
123    private Axis mSecondAxis = vertical;
124
125    final public Axis mainAxis() {
126        return mMainAxis;
127    }
128
129    final public Axis secondAxis() {
130        return mSecondAxis;
131    }
132
133    final public void setOrientation(int orientation) {
134        mOrientation = orientation;
135        if (mOrientation == HORIZONTAL) {
136            mMainAxis = horizontal;
137            mSecondAxis = vertical;
138        } else {
139            mMainAxis = vertical;
140            mSecondAxis = horizontal;
141        }
142    }
143
144    final public int getOrientation() {
145        return mOrientation;
146    }
147
148
149}
150