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