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