BitmapDrawableImageView.java revision 1a340d4fca7babcb67d6cfa754f58c1a756dfca0
1/*
2 * Copyright (C) 2013 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.bitmap.view;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.drawable.Drawable;
22import android.net.Uri;
23import android.os.Build;
24import android.util.AttributeSet;
25import android.widget.ImageView;
26
27import com.android.bitmap.RequestKey;
28import com.android.bitmap.drawable.BasicBitmapDrawable;
29
30/**
31 * A helpful ImageView replacement that can generally be used in lieu of ImageView.
32 * BitmapDrawableImageView has logic to unbind its BasicBitmapDrawable when it is detached from the
33 * window.
34 */
35public class BitmapDrawableImageView extends ImageView {
36    private static final boolean HAS_TRANSIENT_STATE_SUPPORTED =
37        Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
38    private BasicBitmapDrawable mDrawable;
39    private boolean mAttachedToWindow;
40    private RequestKey mKeyAtLastUnbindDueToDetach;
41
42    public BitmapDrawableImageView(final Context context) {
43        this(context, null);
44    }
45
46    public BitmapDrawableImageView(final Context context, final AttributeSet attrs) {
47        this(context, attrs, 0);
48    }
49
50    public BitmapDrawableImageView(final Context context, final AttributeSet attrs,
51            final int defStyle) {
52        super(context, attrs, defStyle);
53    }
54
55    /**
56     * Get the source drawable for this BitmapDrawableImageView.
57     * @return The source drawable casted to the given type, or null if the type does not match.
58     */
59    @SuppressWarnings("unchecked") // Cast to type parameter.
60    public <E extends BasicBitmapDrawable> E getTypedDrawable() {
61        try {
62            return (E) mDrawable;
63        } catch (Exception ignored) {
64            return null;
65        }
66    }
67
68    /**
69     * Set the given drawable as the source for this BitmapDrawableImageView.
70     * @param drawable The source drawable.
71     */
72    public <E extends BasicBitmapDrawable> void setTypedDrawable(E drawable) {
73        super.setImageDrawable(drawable);
74        unbindDrawable();
75        mDrawable = drawable;
76        mKeyAtLastUnbindDueToDetach = null;
77    }
78
79    private void unbindDrawable() {
80        if (mDrawable != null) {
81            mDrawable.unbind();
82        }
83    }
84
85    @Override
86    public void setImageResource(final int resId) {
87        super.setImageResource(resId);
88        unbindDrawable();
89        mDrawable = null;
90        mKeyAtLastUnbindDueToDetach = null;
91    }
92
93    @Override
94    public void setImageURI(final Uri uri) {
95        super.setImageURI(uri);
96        unbindDrawable();
97        mDrawable = null;
98        mKeyAtLastUnbindDueToDetach = null;
99    }
100
101    @Override
102    public void setImageDrawable(final Drawable drawable) {
103        super.setImageDrawable(drawable);
104        unbindDrawable();
105        mDrawable = null;
106        mKeyAtLastUnbindDueToDetach = null;
107    }
108
109    @Override
110    public void setImageBitmap(final Bitmap bm) {
111        super.setImageBitmap(bm);
112        unbindDrawable();
113        mDrawable = null;
114        mKeyAtLastUnbindDueToDetach = null;
115    }
116
117    @Override
118    protected void onAttachedToWindow() {
119        super.onAttachedToWindow();
120        mAttachedToWindow = true;
121        if (mDrawable != null && mDrawable.getKey() == null) {
122            mDrawable.bind(mKeyAtLastUnbindDueToDetach);
123        }
124        mKeyAtLastUnbindDueToDetach = null;
125    }
126
127    @Override
128    protected void onDetachedFromWindow() {
129        super.onDetachedFromWindow();
130        mAttachedToWindow = false;
131        if (mDrawable != null) {
132            mKeyAtLastUnbindDueToDetach = mDrawable.getKey();
133        }
134        if (HAS_TRANSIENT_STATE_SUPPORTED && !hasTransientState()) {
135            unbindDrawable();
136        }
137    }
138
139    @Override
140    public void setHasTransientState(boolean hasTransientState) {
141        super.setHasTransientState(hasTransientState);
142        if (!hasTransientState && !mAttachedToWindow) {
143            unbindDrawable();
144        }
145    }
146}
147