-
Notifications
You must be signed in to change notification settings - Fork 982
/
ReusableBitmapDrawable.java
52 lines (44 loc) · 1.5 KB
/
ReusableBitmapDrawable.java
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
package org.osmdroid.tileprovider;
import android.graphics.Bitmap;
/**
* A {@link ExpirableBitmapDrawable} class that allows keeping track of usage references. This
* facilitates the ability to reuse the underlying Bitmaps if no references are active. To safely
* use the Drawable first call {@link #beginUsingDrawable()} and then check {@link #isBitmapValid()}
* to ensure that the Drawable is still valid. When done using the Drawable you must call
* {@link #finishUsingDrawable()} to release the reference and allow the Bitmap to be reused later.
*
* @author Marc Kurtz
*/
public class ReusableBitmapDrawable extends ExpirableBitmapDrawable {
private boolean mBitmapRecycled = false;
private int mUsageRefCount = 0;
public ReusableBitmapDrawable(Bitmap pBitmap) {
super(pBitmap);
}
public void beginUsingDrawable() {
synchronized (this) {
mUsageRefCount++;
}
}
public void finishUsingDrawable() {
synchronized (this) {
mUsageRefCount--;
if (mUsageRefCount < 0)
throw new IllegalStateException("Unbalanced endUsingDrawable() called.");
}
}
public Bitmap tryRecycle() {
synchronized (this) {
if (mUsageRefCount == 0) {
mBitmapRecycled = true;
return getBitmap();
}
}
return null;
}
public boolean isBitmapValid() {
synchronized (this) {
return !mBitmapRecycled;
}
}
}