2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.andrew.apollo.widgets;
14 import android.content.Context;
15 import android.util.AttributeSet;
16 import android.view.View;
17 import android.view.ViewGroup;
20 * A custom {@link ViewGroup} used to make it's children into perfect squares.
21 * Useful when dealing with grid images and especially album art.
23 * @author Andrew Neal (andrewdneal@gmail.com)
25 public class SquareView extends ViewGroup {
28 * @param context The {@link Context} to use
29 * @param attrs The attributes of the XML tag that is inflating the view.
31 public SquareView(final Context context, final AttributeSet attrs) {
32 super(context, attrs);
39 protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
40 final View mChildren = getChildAt(0);
41 mChildren.measure(widthMeasureSpec, widthMeasureSpec);
42 final int mWidth = resolveSize(mChildren.getMeasuredWidth(), widthMeasureSpec);
43 mChildren.measure(mWidth, mWidth);
44 setMeasuredDimension(mWidth, mWidth);
51 protected void onLayout(final boolean changed, final int l, final int u, final int r,
53 getChildAt(0).layout(0, 0, r - l, d - u);
60 public void requestLayout() {