• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 package com.android.car.media.common;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.util.AttributeSet;
22 import android.view.ViewGroup;
23 import android.widget.ImageView;
24 
25 /**
26  * An {@link ImageView} with a fixed aspect ratio. When using this view, either width or height
27  * must be set to MATCH_PARENT.
28  */
29 public class FixedRatioImageView extends ImageView {
30     private float mAspectRatio;
31 
32     private static final float DEFAULT_ASPECT_RATIO = 1f;
33 
FixedRatioImageView(Context context)34     public FixedRatioImageView(Context context) {
35         this(context, null);
36     }
37 
FixedRatioImageView(Context context, AttributeSet attrs)38     public FixedRatioImageView(Context context, AttributeSet attrs) {
39         this(context, attrs, 0);
40     }
41 
FixedRatioImageView(Context context, AttributeSet attrs, int defStyleAttr)42     public FixedRatioImageView(Context context, AttributeSet attrs, int defStyleAttr) {
43         super(context, attrs, defStyleAttr);
44         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedRatioImageView);
45         mAspectRatio = a.getFloat(R.styleable.FixedRatioImageView_aspect_ratio,
46                 DEFAULT_ASPECT_RATIO);
47     }
48 
49     /**
50      * Modify the aspect ratio of this view.
51      *
52      * @param aspectRatio a ratio between width and height. For example: 2 means this view's width
53      *                    would be double the height, while 0.5 means that this view's width will
54      *                    be half the height.
55      */
setAspectRatio(float aspectRatio)56     public void setAspectRatio(float aspectRatio) {
57         mAspectRatio = aspectRatio;
58         requestLayout();
59     }
60 
61     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)62     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
63         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
64 
65         if (getLayoutParams().height == ViewGroup.LayoutParams.MATCH_PARENT) {
66             int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
67             setMeasuredDimension((int) (height * mAspectRatio), height);
68         } else if (getLayoutParams().width == ViewGroup.LayoutParams.MATCH_PARENT) {
69             int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
70             setMeasuredDimension(width, (int) (width / mAspectRatio));
71         } else {
72             throw new IllegalArgumentException(
73                     "Either width or height should be set to MATCH_PARENT");
74         }
75     }
76 }
77