1a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard/*
2a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Copyright (C) 2012 The Android Open Source Project
3a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
4a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Licensed under the Apache License, Version 2.0 (the "License");
5a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * you may not use this file except in compliance with the License.
6a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * You may obtain a copy of the License at
7a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
8a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *      http://www.apache.org/licenses/LICENSE-2.0
9a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard *
10a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * Unless required by applicable law or agreed to in writing, software
11a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * distributed under the License is distributed on an "AS IS" BASIS,
12a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * See the License for the specific language governing permissions and
14a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard * limitations under the License.
15a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard */
16a9f280f938b5fd5891c5cfe0999f8f1d4945d7a1nicolasroard
17ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordpackage com.android.gallery3d.filtershow;
18ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford
19ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordimport android.content.Context;
20ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordimport android.content.res.Resources;
21ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordimport android.content.res.TypedArray;
22ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordimport android.util.AttributeSet;
23ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordimport android.util.TypedValue;
24ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordimport android.widget.LinearLayout;
25ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford
26ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordimport com.android.gallery3d.R;
27ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford
28ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hofordpublic class CenteredLinearLayout extends LinearLayout {
29ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford    private final int mMaxWidth;
30ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford
31ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford    public CenteredLinearLayout(Context context, AttributeSet attrs) {
32ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        super(context, attrs);
33ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CenteredLinearLayout);
34ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        mMaxWidth = a.getDimensionPixelSize(R.styleable.CenteredLinearLayout_max_width, 0);
35ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford    }
36ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford
37ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford    @Override
38ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
39ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
40ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
41ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        Resources r = getContext().getResources();
42ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        float value = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, parentWidth,
43ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford                r.getDisplayMetrics());
44ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        if (mMaxWidth > 0 && parentWidth > mMaxWidth) {
45ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford            int measureMode = MeasureSpec.getMode(widthMeasureSpec);
46ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford            widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, measureMode);
47ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        }
48ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
49ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford    }
50ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford
51ea5df36ac1cdfbb569e6ba725c2df433d614d0f5John Hoford}
52