/* * Copyright 2018 The Android Open Source Project * * 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 com.android.pump.widget; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import androidx.annotation.AttrRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; @UiThread public class AspectRatioImageView extends UriImageView { public AspectRatioImageView(@NonNull Context context) { super(context); } public AspectRatioImageView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public AspectRatioImageView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // TODO Make aspect ratio configurable int aspectWidth = 16; int aspectHeight = 9; Drawable drawable = getDrawable(); if (drawable != null) { // TODO Landscape/Portrait preference should be configurable int intrinsicWidth = drawable.getIntrinsicWidth(); int intrinsicHeight = drawable.getIntrinsicHeight(); if (intrinsicWidth < intrinsicHeight) { aspectWidth = 2; aspectHeight = 3; } } int width = getMeasuredWidth(); int height = width * aspectHeight / aspectWidth; setMeasuredDimension(width, height); } }