/* * Copyright 2018 Allan Wang * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ca.allanwang.kau.ui.views import android.content.Context import android.graphics.Rect import android.util.AttributeSet import android.view.View import ca.allanwang.kau.R import ca.allanwang.kau.utils.parentViewGroup /** * Created by Allan Wang on 2017-07-14. * * Handles relative sizes for any view * You may delegate all methods to [MeasureSpecDelegate] * and call the two methods: [initAttrs] and [onMeasure] */ interface MeasureSpecContract { /** * Width will be calculated as a percentage of the parent * This takes precedence over relativeWidth */ var relativeWidthToParent: Float /** * Height will be calculated as a percentage of the parent * This takes precedence over relativeHeight */ var relativeHeightToParent: Float /** * Width will be calculated based on the measured height */ var relativeWidth: Float /** * Height will be calculated based on the measure width */ var relativeHeight: Float /** * Width will be once again calculated from the current measured height * This is the last step */ var postRelativeWidth: Float /** * Height will be once again calculated from the current measured width * This is the last step */ var postRelativeHeight: Float /** * Retrieves relative values from the [AttributeSet] * Call this on init */ fun initAttrs(context: Context, attrs: AttributeSet?) /** * Calculates the final measure specs * Call this from [View.onMeasure] and send the Pair result as the specs * The pair is of the format (width, height) * * Example: *
* {@code * override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { * val result = onMeasure(this, widthMeasureSpec, heightMeasureSpec) * super.onMeasure(result.first, result.second) * } * } **/ fun onMeasure(view: View, widthMeasureSpec: Int, heightMeasureSpec: Int): Pair