aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/view/RecyclingImageView.java
blob: 501b363dce799b7a00972529cf81a7f407a75a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
  This file is part of Subsonic.
	Subsonic is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	Subsonic is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	You should have received a copy of the GNU General Public License
	along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
	Copyright 2015 (C) Scott Jackson
*/

package github.daneren2005.dsub.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RecyclingImageView extends ImageView {
	private boolean invalidated = false;
	private OnInvalidated onInvalidated;

	public RecyclingImageView(Context context) {
		super(context);
	}

	public RecyclingImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public RecyclingImageView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@TargetApi(Build.VERSION_CODES.LOLLIPOP)
	public RecyclingImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
	}

	@Override
	public void onDraw(Canvas canvas) {
		Drawable drawable = this.getDrawable();
		if(drawable != null) {
			if(drawable instanceof BitmapDrawable) {
				if (isBitmapRecycled(drawable)) {
					this.setImageDrawable(null);
					setInvalidated(true);
				}
			} else if(drawable instanceof TransitionDrawable) {
				TransitionDrawable transitionDrawable = (TransitionDrawable) drawable;

				// If last bitmap in chain is recycled, just blank this out since it would be invalid anyways
				Drawable lastDrawable = transitionDrawable.getDrawable(transitionDrawable.getNumberOfLayers() - 1);
				if(isBitmapRecycled(lastDrawable)) {
					this.setImageDrawable(null);
					setInvalidated(true);
				} else {
					// Go through earlier bitmaps and make sure that they are not recycled
					for (int i = 0; i < transitionDrawable.getNumberOfLayers(); i++) {
						Drawable layerDrawable = transitionDrawable.getDrawable(i);
						if (isBitmapRecycled(layerDrawable)) {
							// If anything in the chain is broken, just get rid of transition and go to last drawable
							this.setImageDrawable(lastDrawable);
							break;
						}
					}
				}
			}
		}

		super.onDraw(canvas);
	}

	@Override
	public void setImageDrawable(Drawable drawable) {
		super.setImageDrawable(drawable);
		setInvalidated(false);
	}

	private boolean isBitmapRecycled(Drawable drawable) {
		if(!(drawable instanceof BitmapDrawable)) {
			return false;
		}

		BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
		if (bitmapDrawable.getBitmap() != null && bitmapDrawable.getBitmap().isRecycled()) {
			return true;
		} else {
			return false;
		}
	}

	public void setInvalidated(boolean invalidated) {
		this.invalidated = invalidated;

		if(invalidated && onInvalidated != null) {
			onInvalidated.onInvalidated(this);
		}
	}
	public boolean isInvalidated() {
		return invalidated;
	}

	public void setOnInvalidated(OnInvalidated onInvalidated) {
		this.onInvalidated = onInvalidated;
	}

	public interface OnInvalidated {
		void onInvalidated(RecyclingImageView imageView);
	}
}