From 27c964c4ccef882bf63baf8ae63ad9c00c628cf4 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Mon, 21 Jan 2013 14:00:28 -0800 Subject: [PATCH] Cache images in TaskAdapter instead of using setImageResource --- .../astrid/core/LinkActionExposer.java | 24 ++++++++++---- .../todoroo/astrid/adapter/TaskAdapter.java | 33 +++++++++++-------- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/core/LinkActionExposer.java b/astrid/plugin-src/com/todoroo/astrid/core/LinkActionExposer.java index cfaf1a9e4..b691ce51f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/core/LinkActionExposer.java +++ b/astrid/plugin-src/com/todoroo/astrid/core/LinkActionExposer.java @@ -6,6 +6,7 @@ package com.todoroo.astrid.core; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import android.app.PendingIntent; @@ -67,13 +68,13 @@ public class LinkActionExposer { Resources r = context.getResources(); if (hasAttachments) { - BitmapDrawable icon = ((BitmapDrawable) r.getDrawable(R.drawable.action_attachments)); + BitmapDrawable icon = getBitmapDrawable(R.drawable.action_attachments, r); FilesAction filesAction = new FilesAction("", null, icon); //$NON-NLS-1$ result.add(filesAction); } if (!TextUtils.isEmpty(notes) && !Preferences.getBoolean(R.string.p_showNotes, false)) { - BitmapDrawable icon = ((BitmapDrawable) r.getDrawable(R.drawable.action_notes)); + BitmapDrawable icon = getBitmapDrawable(R.drawable.action_notes, r); NotesAction notesAction = new NotesAction("", null, icon); //$NON-NLS-1$ result.add(notesAction); } @@ -106,13 +107,12 @@ public class LinkActionExposer { Resources r = context.getResources(); Drawable icon; if (url.startsWith("mailto")) { - icon = r.getDrawable(R.drawable.action_mail); + icon = getBitmapDrawable(R.drawable.action_mail, r); } else if (url.startsWith("tel")) { - icon = r.getDrawable(R.drawable.action_tel); + icon = getBitmapDrawable(R.drawable.action_tel, r); } else { - icon = r.getDrawable(R.drawable.action_web); + icon = getBitmapDrawable(R.drawable.action_web, r); } -// Bitmap bitmap = ((BitmapDrawable)icon).getBitmap(); if(text.length() > 15) text = text.substring(0, 12) + "..."; //$NON-NLS-1$ @@ -122,4 +122,16 @@ public class LinkActionExposer { return action; } + private static final HashMap IMAGE_CACHE = new HashMap(); + + private static BitmapDrawable getBitmapDrawable(int resId, Resources resources) { + if (IMAGE_CACHE.containsKey(resId)) + return IMAGE_CACHE.get(resId); + else { + BitmapDrawable b = (BitmapDrawable) resources.getDrawable(resId); + IMAGE_CACHE.put(resId, b); + return b; + } + } + } diff --git a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java index 247bfd190..724313a4d 100644 --- a/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java +++ b/astrid/src/com/todoroo/astrid/adapter/TaskAdapter.java @@ -155,34 +155,31 @@ public class TaskAdapter extends CursorAdapter implements Filterable { Task.DELETION_DATE }; - public static int[] IMPORTANCE_RESOURCES = new int[] { + public static final int[] IMPORTANCE_RESOURCES = new int[] { R.drawable.importance_check_1, R.drawable.importance_check_2, R.drawable.importance_check_3, R.drawable.importance_check_4, }; - public static int[] LEGACY_IMPORTANCE_RESOURCES = new int[] { - R.drawable.importance_1, - R.drawable.importance_2, - R.drawable.importance_3, - R.drawable.importance_4, - }; - - public static int[] IMPORTANCE_RESOURCES_LARGE = new int[] { + public static final int[] IMPORTANCE_RESOURCES_LARGE = new int[] { R.drawable.check_box_large_1, R.drawable.check_box_large_2, R.drawable.check_box_large_3, R.drawable.check_box_large_4, }; - public static int[] IMPORTANCE_REPEAT_RESOURCES = new int[] { + public static final int[] IMPORTANCE_REPEAT_RESOURCES = new int[] { R.drawable.importance_check_repeat_1, R.drawable.importance_check_repeat_2, R.drawable.importance_check_repeat_3, R.drawable.importance_check_repeat_4, }; + private static final Drawable[] IMPORTANCE_DRAWABLES = new Drawable[IMPORTANCE_RESOURCES.length]; + private static final Drawable[] IMPORTANCE_DRAWABLES_LARGE = new Drawable[IMPORTANCE_RESOURCES_LARGE.length]; + private static final Drawable[] IMPORTANCE_REPEAT_DRAWABLES = new Drawable[IMPORTANCE_REPEAT_RESOURCES.length]; + // --- instance variables @Autowired @@ -270,6 +267,16 @@ public class TaskAdapter extends CursorAdapter implements Filterable { fragment.getActivity().getTheme().resolveAttribute(R.attr.asReadonlyTaskBackground, readonlyBg, false); readonlyBackground = readonlyBg.data; + preloadDrawables(IMPORTANCE_RESOURCES, IMPORTANCE_DRAWABLES); + preloadDrawables(IMPORTANCE_RESOURCES_LARGE, IMPORTANCE_DRAWABLES_LARGE); + preloadDrawables(IMPORTANCE_REPEAT_RESOURCES, IMPORTANCE_REPEAT_DRAWABLES); + + } + + private void preloadDrawables(int[] resourceIds, Drawable[] drawables) { + for (int i = 0; i < resourceIds.length; i++) { + drawables[i] = resources.getDrawable(resourceIds[i]); + } } protected int computeMinRowHeight() { @@ -1169,9 +1176,9 @@ public class TaskAdapter extends CursorAdapter implements Filterable { if (value >= IMPORTANCE_RESOURCES.length) value = IMPORTANCE_RESOURCES.length - 1; if (!TextUtils.isEmpty(task.getValue(Task.RECURRENCE))) { - checkBoxView.setImageResource(IMPORTANCE_REPEAT_RESOURCES[value]); + checkBoxView.setImageDrawable(IMPORTANCE_REPEAT_DRAWABLES[value]); //(IMPORTANCE_REPEAT_RESOURCES[value]); } else { - checkBoxView.setImageResource(IMPORTANCE_RESOURCES[value]); + checkBoxView.setImageDrawable(IMPORTANCE_DRAWABLES[value]); } if (titleOnlyLayout) return; @@ -1186,7 +1193,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable { if (pictureView != null && pictureView.getVisibility() == View.VISIBLE) { checkBoxView.setVisibility(View.INVISIBLE); if (viewHolder.pictureBorder != null) - viewHolder.pictureBorder.setBackgroundResource(IMPORTANCE_RESOURCES_LARGE[value]); + viewHolder.pictureBorder.setBackgroundDrawable(IMPORTANCE_DRAWABLES_LARGE[value]); } else { checkBoxView.setVisibility(View.VISIBLE); }