BitmapDrawableImageView.java revision b6ec2afe9710112214d79b36b2233fef6a52845a
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        }
71    }
72
73    @Override
74    public void setImageResource(final int resId) {
75        super.setImageResource(resId);
76        unbindDrawable();
77        mDrawable = null;
78    }
79
80    @Override
81    public void setImageURI(final Uri uri) {
82        super.setImageURI(uri);
83        unbindDrawable();
84        mDrawable = null;
85    }
86
87    @Override
88    public void setImageDrawable(final Drawable drawable) {
89        super.setImageDrawable(drawable);
90        unbindDrawable();
91        mDrawable = null;
92    }
93
94    @Override
95    public void setImageBitmap(final Bitmap bm) {
96        super.setImageBitmap(bm);
97        unbindDrawable();
98        mDrawable = null;
99    }
100
101    @Override
102    protected void onDetachedFromWindow() {
103        super.onDetachedFromWindow();
104        unbindDrawable();
105    }
106}
107