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