OSDN Git Service

b2235d093289e9346f8ae91d8d8fddd0aed19a35
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / widgets / SquareView.java
1 /*
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.
10  */
11
12 package com.andrew.apollo.widgets;
13
14 import android.content.Context;
15 import android.util.AttributeSet;
16 import android.view.View;
17 import android.view.ViewGroup;
18
19 /**
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.
22  * 
23  * @author Andrew Neal (andrewdneal@gmail.com)
24  */
25 public class SquareView extends ViewGroup {
26
27     /**
28      * @param context The {@link Context} to use
29      * @param attrs The attributes of the XML tag that is inflating the view.
30      */
31     public SquareView(final Context context, final AttributeSet attrs) {
32         super(context, attrs);
33     }
34
35     /**
36      * {@inheritDoc}
37      */
38     @Override
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);
45     }
46
47     /**
48      * {@inheritDoc}
49      */
50     @Override
51     protected void onLayout(final boolean changed, final int l, final int u, final int r,
52             final int d) {
53         getChildAt(0).layout(0, 0, r - l, d - u);
54     }
55
56     /**
57      * {@inheritDoc}
58      */
59     @Override
60     public void requestLayout() {
61         forceLayout();
62     }
63 }