• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2022 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 
18 package com.android.wallpaper.picker
19 
20 import android.content.Context
21 import android.util.AttributeSet
22 import android.widget.FrameLayout
23 import androidx.core.view.children
24 import com.android.wallpaper.util.ScreenSizeCalculator
25 
26 /**
27  * [FrameLayout] that sizes its children using a fixed aspect ratio that is the same as that of the
28  * display.
29  */
30 class DisplayAspectRatioFrameLayout(
31     context: Context,
32     attrs: AttributeSet?,
33 ) : FrameLayout(context, attrs) {
34 
35     override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
36         super.onMeasure(widthMeasureSpec, heightMeasureSpec)
37         val screenAspectRatio = ScreenSizeCalculator.getInstance().getScreenAspectRatio(context)
38         // We're always forcing the width based on the height. This will only work if the
39         // DisplayAspectRatioFrameLayout is allowed to stretch to fill its parent (for example if
40         // the parent is a vertical LinearLayout and the DisplayAspectRatioFrameLayout has a height
41         // if 0 and a weight of 1.
42         //
43         // If you need to use this class to force the height dimension based on the width instead,
44         // you will need to flip the logic below.
45         children.forEach { child ->
46             child.measure(
47                 MeasureSpec.makeMeasureSpec(
48                     (child.measuredHeight / screenAspectRatio).toInt(),
49                     MeasureSpec.EXACTLY
50                 ),
51                 MeasureSpec.makeMeasureSpec(
52                     child.measuredHeight,
53                     MeasureSpec.EXACTLY,
54                 ),
55             )
56         }
57     }
58 }
59