summaryrefslogtreecommitdiff
path: root/android/sdremote/src/pl/polidea/coverflow/ResourceImageAdapter.java
blob: 469d0d165b0b5017624decd822a913b32e8878f6 (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
package pl.polidea.coverflow;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

/**
 * This class is an adapter that provides images from a fixed set of resource
 * ids. Bitmaps and ImageViews are kept as weak references so that they can be
 * cleared by garbage collection when not needed.
 *
 */
public class ResourceImageAdapter extends AbstractCoverFlowImageAdapter {

	/** The Constant TAG. */
	private static final String TAG = ResourceImageAdapter.class
	                .getSimpleName();

	/** The Constant DEFAULT_LIST_SIZE. */
	private static final int DEFAULT_LIST_SIZE = 20;

	/** The Constant IMAGE_RESOURCE_IDS. */
	private static final List<Integer> IMAGE_RESOURCE_IDS = new ArrayList<Integer>(
	                DEFAULT_LIST_SIZE);

	/** The Constant DEFAULT_RESOURCE_LIST. */
	private static final int[] DEFAULT_RESOURCE_LIST = {};
	/** The bitmap map. */
	private final Map<Integer, WeakReference<Bitmap>> bitmapMap = new HashMap<Integer, WeakReference<Bitmap>>();

	private final Context context;

	/**
	 * Creates the adapter with default set of resource images.
	 *
	 * @param context
	 *            context
	 */
	public ResourceImageAdapter(final Context context) {
		super();
		this.context = context;
		setResources(DEFAULT_RESOURCE_LIST);
	}

	/**
	 * Replaces resources with those specified.
	 *
	 * @param resourceIds
	 *            array of ids of resources.
	 */
	public final synchronized void setResources(final int[] resourceIds) {
		IMAGE_RESOURCE_IDS.clear();
		for (final int resourceId : resourceIds) {
			IMAGE_RESOURCE_IDS.add(resourceId);
		}
		notifyDataSetChanged();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public synchronized int getCount() {
		return IMAGE_RESOURCE_IDS.size();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see pl.polidea.coverflow.AbstractCoverFlowImageAdapter#createBitmap(int)
	 */
	@Override
	protected Bitmap createBitmap(final int position) {
		Log.v(TAG, "creating item " + position);
		final Bitmap bitmap = ((BitmapDrawable) context.getResources()
		                .getDrawable(IMAGE_RESOURCE_IDS.get(position)))
		                .getBitmap();
		bitmapMap.put(position, new WeakReference<Bitmap>(bitmap));
		return bitmap;
	}
}