• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 android.support.v7.internal.widget;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.support.annotation.RestrictTo;
22 import android.support.v7.preference.R;
23 import android.util.AttributeSet;
24 import android.widget.ImageView;
25 
26 import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
27 
28 /**
29  * Extension of ImageView that correctly applies maxWidth and maxHeight.
30  * @hide
31  */
32 @RestrictTo(GROUP_ID)
33 public class PreferenceImageView extends ImageView {
34 
35     private int mMaxWidth = Integer.MAX_VALUE;
36     private int mMaxHeight = Integer.MAX_VALUE;
37 
PreferenceImageView(Context context)38     public PreferenceImageView(Context context) {
39         this(context, null);
40     }
41 
PreferenceImageView(Context context, AttributeSet attrs)42     public PreferenceImageView(Context context, AttributeSet attrs) {
43         this(context, attrs, 0);
44     }
45 
PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr)46     public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr) {
47         super(context, attrs, defStyleAttr);
48 
49         final TypedArray a = context.obtainStyledAttributes(
50                 attrs, R.styleable.PreferenceImageView, defStyleAttr, 0);
51 
52         setMaxWidth(a.getDimensionPixelSize(
53                 R.styleable.PreferenceImageView_maxWidth, Integer.MAX_VALUE));
54 
55         setMaxHeight(a.getDimensionPixelSize(
56                 R.styleable.PreferenceImageView_maxHeight, Integer.MAX_VALUE));
57 
58         a.recycle();
59     }
60 
61 //    public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr,
62 //            int defStyleRes) {
63 //        super(context, attrs, defStyleAttr, defStyleRes);
64 //    }
65 
66     @Override
setMaxWidth(int maxWidth)67     public void setMaxWidth(int maxWidth) {
68         mMaxWidth = maxWidth;
69         super.setMaxWidth(maxWidth);
70     }
71 
getMaxWidth()72     public int getMaxWidth() {
73         return mMaxWidth;
74     }
75 
76     @Override
setMaxHeight(int maxHeight)77     public void setMaxHeight(int maxHeight) {
78         mMaxHeight = maxHeight;
79         super.setMaxHeight(maxHeight);
80     }
81 
getMaxHeight()82     public int getMaxHeight() {
83         return mMaxHeight;
84     }
85 
86     @Override
onMeasure(int widthMeasureSpec, int heightMeasureSpec)87     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
88         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
89         if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
90             final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
91             final int maxWidth = getMaxWidth();
92             if (maxWidth != Integer.MAX_VALUE
93                     && (maxWidth < widthSize || widthMode == MeasureSpec.UNSPECIFIED)) {
94                 widthMeasureSpec = MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.AT_MOST);
95             }
96         }
97 
98         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
99         if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
100             final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
101             final int maxHeight = getMaxHeight();
102             if (maxHeight != Integer.MAX_VALUE
103                     && (maxHeight < heightSize || heightMode == MeasureSpec.UNSPECIFIED)) {
104                 heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
105             }
106         }
107 
108         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
109     }
110 }
111