BitmapDrawableImageView.java revision 89e59f00d67791754e44e65413baa95f94056df4
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.util.AttributeSet;
24import android.widget.ImageView;
25
26import com.android.bitmap.drawable.BasicBitmapDrawable;
27
28/**
29 * A helpful ImageView replacement that can generally be used in lieu of ImageView.
30 * BitmapDrawableImageView has logic to unbind its BasicBitmapDrawable when it is detached from the
31 * window.
32 */
33public class BitmapDrawableImageView extends ImageView {
34    private BasicBitmapDrawable mDrawable;
35
36    public BitmapDrawableImageView(final Context context) {
37        this(context, null);
38    }
39
40    public BitmapDrawableImageView(final Context context, final AttributeSet attrs) {
41        this(context, attrs, 0);
42    }
43
44    public BitmapDrawableImageView(final Context context, final AttributeSet attrs,
45            final int defStyle) {
46        super(context, attrs, defStyle);
47    }
48
49    /**
50     * Get the source drawable for this BitmapDrawableImageView.
51     * @return The source drawable casted to the given type, or null if the type does not match.
52     */
53    @SuppressWarnings("unchecked") // Cast to type parameter.
54    public <E extends BasicBitmapDrawable> E getTypedDrawable() {
55        try {
56            return (E) mDrawable;
57        } catch (Exception ignored) {
58            return null;
59        }
60    }
61
62    /**
63     * Set the given drawable as the source for this BitmapDrawableImageView.
64     * @param drawable The source drawable.
65     */
66    public <E extends BasicBitmapDrawable> void setTypedDrawable(E drawable) {
67        super.setImageDrawable(drawable);
68        unbindDrawable();
69        mDrawable = drawable;
70    }
71
72    private void unbindDrawable() {
73        if (mDrawable != null) {
74            mDrawable.unbind();
75        }
76    }
77
78    @Override
79    public void setImageResource(final int resId) {
80        super.setImageResource(resId);
81        unbindDrawable();
82        mDrawable = null;
83    }
84
85    @Override
86    public void setImageURI(final Uri uri) {
87        super.setImageURI(uri);
88        unbindDrawable();
89        mDrawable = null;
90    }
91
92    @Override
93    public void setImageDrawable(final Drawable drawable) {
94        super.setImageDrawable(drawable);
95        unbindDrawable();
96        mDrawable = null;
97    }
98
99    @Override
100    public void setImageBitmap(final Bitmap bm) {
101        super.setImageBitmap(bm);
102        unbindDrawable();
103        mDrawable = null;
104    }
105
106    @Override
107    protected void onDetachedFromWindow() {
108        super.onDetachedFromWindow();
109        unbindDrawable();
110    }
111}
112