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.config.BaseFlags 25 import com.android.wallpaper.util.ScreenSizeCalculator 26 import kotlin.math.max 27 28 /** 29 * [FrameLayout] that sizes its children using a fixed aspect ratio that is the same as that of the 30 * display. 31 * 32 * Uses the initial height to calculate width based on the display ratio, then use the new width to 33 * get the new height, this will wrap the child view inside like wrap content for both width and 34 * height, for this to work the width must be wrap_content or 0dp and the height cannot be 0dp. 35 */ 36 class DisplayAspectRatioFrameLayout(context: Context, attrs: AttributeSet?) : 37 FrameLayout(context, attrs) { 38 39 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 40 super.onMeasure(widthMeasureSpec, heightMeasureSpec) 41 val screenAspectRatio = ScreenSizeCalculator.getInstance().getScreenAspectRatio(context) 42 // We're always forcing the width based on the height. This will only work if the 43 // DisplayAspectRatioFrameLayout is allowed to stretch to fill its parent (for example if 44 // the parent is a vertical LinearLayout and the DisplayAspectRatioFrameLayout has a height 45 // if 0 and a weight of 1. 46 // However we make sure that the width of the children never exceeds the width of the parent 47 // 48 // If you need to use this class to force the height dimension based on the width instead, 49 // you will need to flip the logic below. 50 var maxWidth = 0 51 children.forEach { child -> 52 // Calculate child width from height based on display ratio, max at parent width. 53 val childWidth = 54 (child.measuredHeight / screenAspectRatio).toInt().coerceAtMost(measuredWidth) 55 child.measure( 56 MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY), 57 MeasureSpec.makeMeasureSpec( 58 if (childWidth < measuredWidth) { 59 // Child width not capped, height is the same. 60 child.measuredHeight 61 } else { 62 // Child width capped at parent width, recalculates height based on ratio. 63 (childWidth * screenAspectRatio).toInt() 64 }, 65 MeasureSpec.EXACTLY, 66 ), 67 ) 68 // Find max width among all children. 69 maxWidth = max(maxWidth, child.measuredWidth) 70 } 71 72 if (BaseFlags.get().isNewPickerUi()) { 73 // New height based on the new width 74 val newHeight = (maxWidth * screenAspectRatio).toInt() 75 76 // Makes width wrap content 77 setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec), newHeight) 78 } 79 } 80 } 81