Skip to content

Commit 8180be1

Browse files
author
Adam Koch
committed
Bitmapfun Sample: Minor updates/fixes.
Change-Id: I71dce2fdbe41ff67cc0aac9456d72d9aa7cb8318
1 parent a0b7965 commit 8180be1

File tree

5 files changed

+46
-26
lines changed

5 files changed

+46
-26
lines changed

samples/training/bitmapfun/BitmapFun/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ repositories {
1414

1515
android {
1616
compileSdkVersion 19
17-
buildToolsVersion "18.1.1"
17+
buildToolsVersion "19.0.0"
1818

1919
defaultConfig {
2020
minSdkVersion 7
@@ -23,5 +23,5 @@ android {
2323
}
2424

2525
dependencies {
26-
compile 'com.android.support:support-v4:18.0.0'
26+
compile 'com.android.support:support-v4:19.0.+'
2727
}

samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageDetailActivity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.annotation.TargetApi;
2020
import android.app.ActionBar;
21+
import android.os.Build.VERSION_CODES;
2122
import android.os.Bundle;
2223
import android.support.v4.app.Fragment;
2324
import android.support.v4.app.FragmentActivity;
@@ -48,7 +49,7 @@ public class ImageDetailActivity extends FragmentActivity implements OnClickList
4849
private ImageFetcher mImageFetcher;
4950
private ViewPager mPager;
5051

51-
@TargetApi(11)
52+
@TargetApi(VERSION_CODES.HONEYCOMB)
5253
@Override
5354
public void onCreate(Bundle savedInstanceState) {
5455
if (BuildConfig.DEBUG) {
@@ -199,7 +200,7 @@ public Fragment getItem(int position) {
199200
* Set on the ImageView in the ViewPager children fragments, to enable/disable low profile mode
200201
* when the ImageView is touched.
201202
*/
202-
@TargetApi(11)
203+
@TargetApi(VERSION_CODES.HONEYCOMB)
203204
@Override
204205
public void onClick(View v) {
205206
final int vis = mPager.getSystemUiVisibility();

samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/ui/ImageGridFragment.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.app.ActivityOptions;
2121
import android.content.Context;
2222
import android.content.Intent;
23+
import android.os.Build.VERSION_CODES;
2324
import android.os.Bundle;
2425
import android.support.v4.app.Fragment;
2526
import android.util.Log;
@@ -134,6 +135,13 @@ public void onGlobalLayout() {
134135
if (BuildConfig.DEBUG) {
135136
Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
136137
}
138+
if (Utils.hasJellyBean()) {
139+
mGridView.getViewTreeObserver()
140+
.removeOnGlobalLayoutListener(this);
141+
} else {
142+
mGridView.getViewTreeObserver()
143+
.removeGlobalOnLayoutListener(this);
144+
}
137145
}
138146
}
139147
}
@@ -163,7 +171,7 @@ public void onDestroy() {
163171
mImageFetcher.closeCache();
164172
}
165173

166-
@TargetApi(16)
174+
@TargetApi(VERSION_CODES.JELLY_BEAN)
167175
@Override
168176
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
169177
final Intent i = new Intent(getActivity(), ImageDetailActivity.class);
@@ -226,6 +234,11 @@ public ImageAdapter(Context context) {
226234

227235
@Override
228236
public int getCount() {
237+
// If columns have yet to be determined, return no items
238+
if (getNumColumns() == 0) {
239+
return 0;
240+
}
241+
229242
// Size + number of columns for top empty row
230243
return Images.imageThumbUrls.length + mNumColumns;
231244
}

samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/ImageCache.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import android.graphics.Bitmap.Config;
2424
import android.graphics.BitmapFactory;
2525
import android.graphics.drawable.BitmapDrawable;
26+
import android.os.Build.VERSION_CODES;
2627
import android.os.Bundle;
2728
import android.os.Environment;
2829
import android.os.StatFs;
@@ -354,24 +355,26 @@ protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
354355
Bitmap bitmap = null;
355356

356357
if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
357-
final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
358-
Bitmap item;
358+
synchronized (mReusableBitmaps) {
359+
final Iterator<SoftReference<Bitmap>> iterator = mReusableBitmaps.iterator();
360+
Bitmap item;
359361

360-
while (iterator.hasNext()) {
361-
item = iterator.next().get();
362+
while (iterator.hasNext()) {
363+
item = iterator.next().get();
362364

363-
if (null != item && item.isMutable()) {
364-
// Check to see it the item can be used for inBitmap
365-
if (canUseForInBitmap(item, options)) {
366-
bitmap = item;
365+
if (null != item && item.isMutable()) {
366+
// Check to see it the item can be used for inBitmap
367+
if (canUseForInBitmap(item, options)) {
368+
bitmap = item;
367369

368-
// Remove from reusable set so it can't be used again
370+
// Remove from reusable set so it can't be used again
371+
iterator.remove();
372+
break;
373+
}
374+
} else {
375+
// Remove from the set if the reference has been cleared.
369376
iterator.remove();
370-
break;
371377
}
372-
} else {
373-
// Remove from the set if the reference has been cleared.
374-
iterator.remove();
375378
}
376379
}
377380
}
@@ -503,6 +506,7 @@ public void setMemCacheSizePercent(float percent) {
503506
* @return true if <code>candidate</code> can be used for inBitmap re-use with
504507
* <code>targetOptions</code>
505508
*/
509+
@TargetApi(VERSION_CODES.KITKAT)
506510
private static boolean canUseForInBitmap(
507511
Bitmap candidate, BitmapFactory.Options targetOptions) {
508512

@@ -594,7 +598,7 @@ private static String bytesToHexString(byte[] bytes) {
594598
* @param value
595599
* @return size in bytes
596600
*/
597-
@TargetApi(12)
601+
@TargetApi(VERSION_CODES.KITKAT)
598602
public static int getBitmapSize(BitmapDrawable value) {
599603
Bitmap bitmap = value.getBitmap();
600604

@@ -618,7 +622,7 @@ public static int getBitmapSize(BitmapDrawable value) {
618622
* @return True if external storage is removable (like an SD card), false
619623
* otherwise.
620624
*/
621-
@TargetApi(9)
625+
@TargetApi(VERSION_CODES.GINGERBREAD)
622626
public static boolean isExternalStorageRemovable() {
623627
if (Utils.hasGingerbread()) {
624628
return Environment.isExternalStorageRemovable();
@@ -632,7 +636,7 @@ public static boolean isExternalStorageRemovable() {
632636
* @param context The context to use
633637
* @return The external cache dir
634638
*/
635-
@TargetApi(8)
639+
@TargetApi(VERSION_CODES.FROYO)
636640
public static File getExternalCacheDir(Context context) {
637641
if (Utils.hasFroyo()) {
638642
return context.getExternalCacheDir();
@@ -649,7 +653,7 @@ public static File getExternalCacheDir(Context context) {
649653
* @param path The path to check
650654
* @return The space available in bytes
651655
*/
652-
@TargetApi(9)
656+
@TargetApi(VERSION_CODES.GINGERBREAD)
653657
public static long getUsableSpace(File path) {
654658
if (Utils.hasGingerbread()) {
655659
return path.getUsableSpace();

samples/training/bitmapfun/BitmapFun/src/main/java/com/example/android/bitmapfun/util/Utils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@
1616

1717
package com.example.android.bitmapfun.util;
1818

19-
import com.example.android.bitmapfun.ui.ImageDetailActivity;
20-
import com.example.android.bitmapfun.ui.ImageGridActivity;
21-
2219
import android.annotation.TargetApi;
2320
import android.os.Build;
21+
import android.os.Build.VERSION_CODES;
2422
import android.os.StrictMode;
2523

24+
import com.example.android.bitmapfun.ui.ImageDetailActivity;
25+
import com.example.android.bitmapfun.ui.ImageGridActivity;
26+
2627
/**
2728
* Class containing some static utility methods.
2829
*/
2930
public class Utils {
3031
private Utils() {};
3132

32-
@TargetApi(11)
33+
34+
@TargetApi(VERSION_CODES.HONEYCOMB)
3335
public static void enableStrictMode() {
3436
if (Utils.hasGingerbread()) {
3537
StrictMode.ThreadPolicy.Builder threadPolicyBuilder =

0 commit comments

Comments
 (0)