diff options
author | Andrew Dolgov <[email protected]> | 2013-10-15 15:59:13 +0400 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2013-10-15 15:59:13 +0400 |
commit | c5e1254d5957b46c1afb14f933db10e4023fa500 (patch) | |
tree | 086e63c872ff780bcb2f25cb08b7c67822dbc733 /src | |
parent | 01ba164f221c171275b8be96c4f2b15216b9fa02 (diff) |
load thumbnails in comicslist asynchronously
Diffstat (limited to 'src')
-rw-r--r-- | src/org/fox/ttcomics/ComicListFragment.java | 58 | ||||
-rw-r--r-- | src/org/fox/ttcomics/MainActivity.java | 6 |
2 files changed, 45 insertions, 19 deletions
diff --git a/src/org/fox/ttcomics/ComicListFragment.java b/src/org/fox/ttcomics/ComicListFragment.java index edd13ff..349084a 100644 --- a/src/org/fox/ttcomics/ComicListFragment.java +++ b/src/org/fox/ttcomics/ComicListFragment.java @@ -61,12 +61,10 @@ public class ComicListFragment extends Fragment implements OnItemClickListener { m_baseDirectory = baseDirectory;
}
- public ComicListFragment(int mode) {
- super();
-
+ public void setMode(int mode) {
m_mode = mode;
}
-
+
private class ComicsListAdapter extends SimpleCursorAdapter {
public ComicsListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
@@ -135,30 +133,56 @@ public class ComicListFragment extends Fragment implements OnItemClickListener { ImageView thumbnail = (ImageView) v.findViewById(R.id.thumbnail);
if (thumbnail != null) {
+ thumbnail.setImageResource(R.drawable.ic_launcher);
+
if (m_activity.isStorageAvailable() && thumbnailFile.exists()) {
- //
-
+ thumbnail.setTag(thumbnailFile.getAbsolutePath());
+
+ CoverImageLoader imageLoader = new CoverImageLoader();
+ imageLoader.execute(thumbnail);
+ }
+ }
+
+ return v;
+ }
+ }
+
+ class CoverImageLoader extends AsyncTask<ImageView, Void, Bitmap> {
+ private ImageView m_thumbnail;
+
+ @Override
+ protected Bitmap doInBackground(ImageView... params) {
+ m_thumbnail = params[0];
+
+ if (m_thumbnail != null) {
+ File thumbnailFile = new File(m_thumbnail.getTag().toString());
+
+ if (thumbnailFile.exists() && thumbnailFile.canRead()) {
+
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(thumbnailFile.getAbsolutePath(), options);
-
+
options.inSampleSize = CommonActivity.calculateInSampleSize(options, 128, 128);
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(thumbnailFile.getAbsolutePath(), options);
-
- if (bmp != null) {
- thumbnail.setImageBitmap(bmp);
- }
- } else {
- thumbnail.setImageResource(R.drawable.ic_launcher);
+
+ return bmp;
}
}
-
- return v;
+
+ return null;
}
- }
-
+
+ @Override
+ protected void onPostExecute(Bitmap bmp) {
+ if (isAdded() && bmp != null) {
+ m_thumbnail.setImageBitmap(bmp);
+ }
+ }
+
+ };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
diff --git a/src/org/fox/ttcomics/MainActivity.java b/src/org/fox/ttcomics/MainActivity.java index 648a12a..13af786 100644 --- a/src/org/fox/ttcomics/MainActivity.java +++ b/src/org/fox/ttcomics/MainActivity.java @@ -46,7 +46,8 @@ public class MainActivity extends CommonActivity { if (m_selectedTab != tab.getPosition() && m_selectedTab != -1) { - ComicListFragment frag = new ComicListFragment(tab.getPosition()); + ComicListFragment frag = new ComicListFragment(); + frag.setMode(tab.getPosition()); frag.setBaseDirectory(m_baseDirectory); @@ -84,7 +85,8 @@ public class MainActivity extends CommonActivity { Log.d(TAG, "selTab=" + m_selectedTab); - ComicListFragment frag = new ComicListFragment(m_selectedTab); + ComicListFragment frag = new ComicListFragment(); + frag.setMode(m_selectedTab); if (getIntent().getStringExtra("baseDir") != null) { m_baseDirectory = getIntent().getStringExtra("baseDir"); |