FixedSizeImageView.java revision 82c8c5e9872b984ba6f43b01da0c7731fd5bf762
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 17package com.android.systemui.recents.views; 18 19import android.content.Context; 20import android.graphics.drawable.Drawable; 21import android.util.AttributeSet; 22import android.widget.ImageView; 23 24/** 25 * This is an optimized ImageView that does not trigger a requestLayout() or invalidate() when 26 * setting the image to Null. 27 */ 28public class FixedSizeImageView extends ImageView { 29 30 int mFixedWidth; 31 int mFixedHeight; 32 boolean mAllowRelayout = true; 33 boolean mAllowInvalidate = true; 34 35 public FixedSizeImageView(Context context) { 36 this(context, null); 37 } 38 39 public FixedSizeImageView(Context context, AttributeSet attrs) { 40 this(context, attrs, 0); 41 } 42 43 public FixedSizeImageView(Context context, AttributeSet attrs, int defStyleAttr) { 44 this(context, attrs, defStyleAttr, 0); 45 } 46 47 public FixedSizeImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 48 super(context, attrs, defStyleAttr, defStyleRes); 49 } 50 51 @Override 52 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 53 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 54 mFixedWidth = getMeasuredWidth(); 55 mFixedHeight = getMeasuredHeight(); 56 } 57 58 @Override 59 public void requestLayout() { 60 if (mAllowRelayout) { 61 super.requestLayout(); 62 } 63 } 64 65 @Override 66 public void invalidate() { 67 if (mAllowInvalidate) { 68 super.invalidate(); 69 } 70 } 71 72 @Override 73 public void setImageDrawable(Drawable drawable) { 74 if (drawable == null || (mFixedWidth > 0 && mFixedHeight > 0)) { 75 mAllowRelayout = false; 76 mAllowInvalidate = false; 77 } 78 super.setImageDrawable(drawable); 79 mAllowRelayout = true; 80 mAllowInvalidate = true; 81 } 82} 83