AspectRatioFrameLayout.java revision 9a3cf7725baf16afdce5a8380af8bf5939de2ee6
1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except 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
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.settings.widget;
16
17import android.content.Context;
18import android.content.res.TypedArray;
19import android.util.AttributeSet;
20import android.widget.FrameLayout;
21
22import com.android.settings.R;
23
24/**
25 * A {@link FrameLayout} with customizable aspect ration.
26 * This is used to avoid dynamically calculating the height for the frame. Default aspect
27 * ratio will be 1 if none is set in layout attribute.
28 */
29public final class AspectRatioFrameLayout extends FrameLayout {
30
31    private float mAspectRatio = 1.0f;
32
33    public AspectRatioFrameLayout(Context context) {
34        this(context, null);
35    }
36
37    public AspectRatioFrameLayout(Context context, AttributeSet attrs) {
38        this(context, attrs, 0);
39    }
40
41    public AspectRatioFrameLayout(Context context, AttributeSet attrs, int defStyle) {
42        super(context, attrs, defStyle);
43        if (attrs != null) {
44            TypedArray array =
45                    context.obtainStyledAttributes(attrs, R.styleable.AspectRatioFrameLayout);
46            mAspectRatio = array.getFloat(
47                    R.styleable.AspectRatioFrameLayout_aspectRatio, 1.0f);
48            array.recycle();
49        }
50    }
51
52    @Override
53    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
54        super.onMeasure(widthMeasureSpec, (int) (widthMeasureSpec / mAspectRatio));
55    }
56
57}
58