01
02
03
04
05
06
07
08
09
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
|
private
int
mCacheRefCount =
0
;
private
int
mDisplayRefCount =
0
;
...
// Notify the drawable that the displayed state has changed.
// Keep a count to determine when the drawable is no longer displayed.
public
void
setIsDisplayed(
boolean
isDisplayed) {
synchronized
(
this
) {
if
(isDisplayed) {
mDisplayRefCount++;
mHasBeenDisplayed =
true
;
}
else
{
mDisplayRefCount--;
}
}
// Check to see if recycle() can be called.
checkState();
}
// Notify the drawable that the cache state has changed.
// Keep a count to determine when the drawable is no longer being cached.
public
void
setIsCached(
boolean
isCached) {
synchronized
(
this
) {
if
(isCached) {
mCacheRefCount++;
}
else
{
mCacheRefCount--;
}
}
// Check to see if recycle() can be called.
checkState();
}
private
synchronized
void
checkState() {
// If the drawable cache and display ref counts = 0, and this drawable
// has been displayed, then recycle.
if
(mCacheRefCount <=
0
&& mDisplayRefCount <=
0
&& mHasBeenDisplayed
&& hasValidBitmap()) {
getBitmap().recycle();
}
}
private
synchronized
boolean
hasValidBitmap() {
Bitmap bitmap = getBitmap();
return
bitmap !=
null
&& !bitmap.isRecycled();
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
HashSet<SoftReference<Bitmap>> mReusableBitmaps;
private
LruCache<String, BitmapDrawable> mMemoryCache;
// If you're running on Honeycomb or newer, create
// a HashSet of references to reusable bitmaps.
if
(Utils.hasHoneycomb()) {
mReusableBitmaps =
new
HashSet<SoftReference<Bitmap>>();
}
mMemoryCache =
new
LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) {
// Notify the removed entry that is no longer being cached.
@Override
protected
void
entryRemoved(
boolean
evicted, String key,
BitmapDrawable oldValue, BitmapDrawable newValue) {
if
(RecyclingBitmapDrawable.
class
.isInstance(oldValue)) {
// The removed entry is a recycling drawable, so notify it
// that it has been removed from the memory cache.
((RecyclingBitmapDrawable) oldValue).setIsCached(
false
);
}
else
{
// The removed entry is a standard BitmapDrawable.
if
(Utils.hasHoneycomb()) {
// We're running on Honeycomb or later, so add the bitmap
// to a SoftReference set for possible use with inBitmap later.
mReusableBitmaps.add
(
new
SoftReference<Bitmap>(oldValue.getBitmap()));
}
}
}
....
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
|
public
static
Bitmap decodeSampledBitmapFromFile(String filename,
int
reqWidth,
int
reqHeight, ImageCache cache) {
final
BitmapFactory.Options options =
new
BitmapFactory.Options();
...
BitmapFactory.decodeFile(filename, options);
...
// If we're running on Honeycomb or newer, try to use inBitmap.
if
(Utils.hasHoneycomb()) {
addInBitmapOptions(options, cache);
}
...
return
BitmapFactory.decodeFile(filename, options);
}
|
01
02
03
04
05
06
07
08
09
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
|
private
static
void
addInBitmapOptions(BitmapFactory.Options options,
ImageCache cache) {
// inBitmap only works with mutable bitmaps, so force the decoder to
// return mutable bitmaps.
options.inMutable =
true
;
if
(cache !=
null
) {
// Try to find a bitmap to use for inBitmap.
Bitmap inBitmap = cache.getBitmapFromReusableSet(options);
if
(inBitmap !=
null
) {
// If a suitable bitmap has been found, set it as the value of
// inBitmap.
options.inBitmap = inBitmap;
}
}
}
// This method iterates through the reusable bitmaps, looking for one
// to use for inBitmap:
protected
Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
Bitmap bitmap =
null
;
if
(mReusableBitmaps !=
null
&& !mReusableBitmaps.isEmpty()) {
final
Iterator<SoftReference<Bitmap>> iterator
= mReusableBitmaps.iterator();
Bitmap item;
while
(iterator.hasNext()) {
item = iterator.next().get();
if
(
null
!= item && item.isMutable()) {
// Check to see it the item can be used for inBitmap.
if
(canUseForInBitmap(item, options)) {
bitmap = item;
// Remove from reusable set so it can't be used again.
iterator.remove();
break
;
}
}
else
{
// Remove from the set if the reference has been cleared.
iterator.remove();
}
}
}
return
bitmap;
}
|
01
02
03
04
05
06
07
08
09
|
private
static
boolean
canUseForInBitmap(
Bitmap candidate, BitmapFactory.Options targetOptions) {
int
width = targetOptions.outWidth / targetOptions.inSampleSize;
int
height = targetOptions.outHeight / targetOptions.inSampleSize;
// Returns true if "candidate" can be used for inBitmap re-use with
// "targetOptions".
return
candidate.getWidth() == width && candidate.getHeight() == height;
}
|
01
02
03
04
05
06
07
08
09
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
|
public
class
ImageDetailActivity
extends
FragmentActivity {
public
static
final
String EXTRA_IMAGE =
"extra_image"
;
private
ImagePagerAdapter mAdapter;
private
ViewPager mPager;
// A static dataset to back the ViewPager adapter
public
final
static
Integer[] imageResIds =
new
Integer[] {
R.drawable.sample_image_1, R.drawable.sample_image_2, R.drawable.sample_image_3,
R.drawable.sample_image_4, R.drawable.sample_image_5, R.drawable.sample_image_6,
R.drawable.sample_image_7, R.drawable.sample_image_8, R.drawable.sample_image_9};
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.image_detail_pager);
// Contains just a ViewPager
mAdapter =
new
ImagePagerAdapter(getSupportFragmentManager(), imageResIds.length);
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public
static
class
ImagePagerAdapter
extends
FragmentStatePagerAdapter {
private
final
int
mSize;
public
ImagePagerAdapter(FragmentManager fm,
int
size) {
super
(fm);
mSize = size;
}
@Override
public
int
getCount() {
return
mSize;
}
@Override
public
Fragment getItem(
int
position) {
return
ImageDetailFragment.newInstance(position);
}
}
}
|
01
02
03
04
05
06
07
08
09
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
|
public
class
ImageDetailFragment
extends
Fragment {
private
static
final
String IMAGE_DATA_EXTRA =
"resId"
;
private
int
mImageNum;
private
ImageView mImageView;
static
ImageDetailFragment newInstance(
int
imageNum) {
final
ImageDetailFragment f =
new
ImageDetailFragment();
final
Bundle args =
new
Bundle();
args.putInt(IMAGE_DATA_EXTRA, imageNum);
f.setArguments(args);
return
f;
}
// Empty constructor, required as per Fragment docs
public
ImageDetailFragment() {}
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
mImageNum = getArguments() !=
null
? getArguments().getInt(IMAGE_DATA_EXTRA) : -
1
;
}
@Override
public
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// image_detail_fragment.xml contains just an ImageView
final
View v = inflater.inflate(R.layout.image_detail_fragment, container,
false
);
mImageView = (ImageView) v.findViewById(R.id.imageView);
return
v;
}
@Override
public
void
onActivityCreated(Bundle savedInstanceState) {
super
.onActivityCreated(savedInstanceState);
final
int
resId = ImageDetailActivity.imageResIds[mImageNum];
mImageView.setImageResource(resId);
// Load image into ImageView
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
|