1/*
2 * Copyright (C) 2012 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
17package com.android.gallery3d.filtershow;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import android.util.TypedValue;
24import android.widget.LinearLayout;
25
26import com.android.gallery3d.R;
27
28public class CenteredLinearLayout extends LinearLayout {
29    private final int mMaxWidth;
30
31    public CenteredLinearLayout(Context context, AttributeSet attrs) {
32        super(context, attrs);
33        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CenteredLinearLayout);
34        mMaxWidth = a.getDimensionPixelSize(R.styleable.CenteredLinearLayout_max_width, 0);
35    }
36
37    @Override
38    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
39        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
40        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
41        Resources r = getContext().getResources();
42        float value = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, parentWidth,
43                r.getDisplayMetrics());
44        if (mMaxWidth > 0 && parentWidth > mMaxWidth) {
45            int measureMode = MeasureSpec.getMode(widthMeasureSpec);
46            widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, measureMode);
47        }
48        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
49    }
50
51}
52