FixedSizeImageView.java revision 9b5c847a19bf7dbfe3fe00f11cbbdf581794fccc
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;
23
24import com.android.systemui.statusbar.AlphaOptimizedImageView;
25
26/**
27 * This is an optimized ImageView that does not trigger a <code>requestLayout()</code> or
28 * <code>invalidate()</code> when setting the image to <code>null</code>.
29 */
30public class FixedSizeImageView extends AlphaOptimizedImageView {
31
32    private boolean mAllowRelayout = true;
33    private 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    public void requestLayout() {
53        if (mAllowRelayout) {
54            super.requestLayout();
55        }
56    }
57
58    @Override
59    public void invalidate() {
60        if (mAllowInvalidate) {
61            super.invalidate();
62        }
63    }
64
65    @Override
66    public void setImageDrawable(Drawable drawable) {
67        boolean isNullBitmapDrawable = (drawable instanceof BitmapDrawable) &&
68                (((BitmapDrawable) drawable).getBitmap() == null);
69        if (drawable == null || isNullBitmapDrawable) {
70            mAllowRelayout = false;
71            mAllowInvalidate = false;
72        }
73        super.setImageDrawable(drawable);
74        mAllowRelayout = true;
75        mAllowInvalidate = true;
76    }
77}
78