• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.internal.widget;
18 
19 import android.annotation.UnsupportedAppUsage;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.widget.ImageView;
23 
24 /**
25  * Extension of ImageView that correctly applies maxWidth and maxHeight.
26  */
27 public class PreferenceImageView extends ImageView {
28 
PreferenceImageView(Context context)29     public PreferenceImageView(Context context) {
30         this(context, null);
31     }
32 
33     @UnsupportedAppUsage
PreferenceImageView(Context context, AttributeSet attrs)34     public PreferenceImageView(Context context, AttributeSet attrs) {
35         this(context, attrs, 0);
36     }
37 
PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr)38     public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr) {
39         this(context, attrs, defStyleAttr, 0);
40     }
41 
PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)42     public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr,
43             int defStyleRes) {
44         super(context, attrs, defStyleAttr, defStyleRes);
45     }
46 
47     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)48     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
49         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
50         if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
51             final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
52             final int maxWidth = getMaxWidth();
53             if (maxWidth != Integer.MAX_VALUE
54                     && (maxWidth < widthSize || widthMode == MeasureSpec.UNSPECIFIED)) {
55                 widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
56             }
57         }
58 
59         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
60         if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
61             final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
62             final int maxHeight = getMaxHeight();
63             if (maxHeight != Integer.MAX_VALUE
64                     && (maxHeight < heightSize || heightMode == MeasureSpec.UNSPECIFIED)) {
65                 heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
66             }
67         }
68 
69         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
70     }
71 }
72