• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.activityscenetransitionbasic;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.widget.FrameLayout;
22 
23 /**
24  * {@link android.widget.FrameLayout} which forces itself to be laid out as square.
25  */
26 public class SquareFrameLayout extends FrameLayout {
27 
SquareFrameLayout(Context context)28     public SquareFrameLayout(Context context) {
29         super(context);
30     }
31 
SquareFrameLayout(Context context, AttributeSet attrs)32     public SquareFrameLayout(Context context, AttributeSet attrs) {
33         super(context, attrs);
34     }
35 
SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr)36     public SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
37         super(context, attrs, defStyleAttr);
38     }
39 
SquareFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40     public SquareFrameLayout(Context context, AttributeSet attrs,
41             int defStyleAttr, int defStyleRes) {
42         super(context, attrs, defStyleAttr, defStyleRes);
43     }
44 
45     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)46     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
47         final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
48         final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
49 
50         if (widthSize == 0 && heightSize == 0) {
51             // If there are no constraints on size, let FrameLayout measure
52             super.onMeasure(widthMeasureSpec, heightMeasureSpec);
53 
54             // Now use the smallest of the measured dimensions for both dimensions
55             final int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight());
56             setMeasuredDimension(minSize, minSize);
57             return;
58         }
59 
60         final int size;
61         if (widthSize == 0 || heightSize == 0) {
62             // If one of the dimensions has no restriction on size, set both dimensions to be the
63             // on that does
64             size = Math.max(widthSize, heightSize);
65         } else {
66             // Both dimensions have restrictions on size, set both dimensions to be the
67             // smallest of the two
68             size = Math.min(widthSize, heightSize);
69         }
70 
71         final int newMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
72         super.onMeasure(newMeasureSpec, newMeasureSpec);
73     }
74 }
75