BitmapDrawableImageView.java revision 9c6ac19d4a3d39b7c2992060957920118ff56a65
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 BasicBitmapDrawable for this BitmapDrawableImageView.
51     * @return The source drawable.
52     */
53    public BasicBitmapDrawable getBasicBitmapDrawable() {
54        return mDrawable;
55    }
56
57    /**
58     * Set the given BasicBitmapDrawable as the source for this BitmapDrawableImageView.
59     * @param drawable The source drawable.
60     */
61    public void setBasicBitmapDrawable(BasicBitmapDrawable drawable) {
62        super.setImageDrawable(drawable);
63        unbindDrawable();
64        mDrawable = drawable;
65    }
66
67    private void unbindDrawable() {
68        if (mDrawable != null) {
69            mDrawable.unbind();
70            mDrawable = null;
71        }
72    }
73
74    @Override
75    public void setImageResource(final int resId) {
76        super.setImageResource(resId);
77        unbindDrawable();
78    }
79
80    @Override
81    public void setImageURI(final Uri uri) {
82        super.setImageURI(uri);
83        unbindDrawable();
84    }
85
86    @Override
87    public void setImageDrawable(final Drawable drawable) {
88        super.setImageDrawable(drawable);
89        unbindDrawable();
90    }
91
92    @Override
93    public void setImageBitmap(final Bitmap bm) {
94        super.setImageBitmap(bm);
95        unbindDrawable();
96    }
97
98    @Override
99    protected void onDetachedFromWindow() {
100        super.onDetachedFromWindow();
101        unbindDrawable();
102    }
103}
104