mirror of
https://github.com/viq/NewsBlur.git
synced 2025-09-18 21:43:31 +00:00
Modified ImageLoader to be a singleton available application-wide, fixed to search for either UID or url.
This commit is contained in:
parent
38b0848ad2
commit
bb34a535c5
8 changed files with 40 additions and 11 deletions
|
@ -15,7 +15,8 @@
|
|||
<application
|
||||
android:icon="@drawable/logo"
|
||||
android:label="@string/newsblur"
|
||||
android:theme="@style/NewsBlurTheme" >
|
||||
android:theme="@style/NewsBlurTheme"
|
||||
android:name=".activity.NewsBlurApplication" >
|
||||
|
||||
<activity
|
||||
android:name=".activity.Login"
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
package com.newsblur.activity;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import com.newsblur.util.ImageLoader;
|
||||
|
||||
public class NewsBlurApplication extends Application {
|
||||
|
||||
ImageLoader imageLoader;
|
||||
|
||||
public NewsBlurApplication() {
|
||||
super();
|
||||
imageLoader = new ImageLoader(this);
|
||||
}
|
||||
|
||||
public ImageLoader getImageLoader() {
|
||||
return imageLoader;
|
||||
}
|
||||
|
||||
}
|
|
@ -15,6 +15,7 @@ import android.widget.ImageView;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.newsblur.R;
|
||||
import com.newsblur.activity.NewsBlurApplication;
|
||||
import com.newsblur.domain.UserProfile;
|
||||
import com.newsblur.network.APIManager;
|
||||
import com.newsblur.util.ImageLoader;
|
||||
|
@ -37,7 +38,7 @@ public class ProfileDetailsFragment extends Fragment implements OnClickListener
|
|||
super.onCreate(savedInstanceState);
|
||||
noBio = getString(R.string.profile_no_bio);
|
||||
noLocation = getActivity().getResources().getString(R.string.profile_no_location);
|
||||
imageLoader = new ImageLoader(getActivity());
|
||||
imageLoader = ((NewsBlurApplication) getActivity().getApplicationContext()).getImageLoader();
|
||||
apiManager = new APIManager(getActivity());
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ import android.widget.LinearLayout;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.newsblur.R;
|
||||
import com.newsblur.activity.NewsBlurApplication;
|
||||
import com.newsblur.activity.Profile;
|
||||
import com.newsblur.database.FeedProvider;
|
||||
import com.newsblur.domain.Comment;
|
||||
|
@ -37,7 +38,7 @@ public class ReadingItemFragment extends Fragment {
|
|||
public Story story;
|
||||
private LayoutInflater inflater;
|
||||
private APIManager apiManager;
|
||||
private ImageLoader imageLoader = new ImageLoader(getActivity());
|
||||
private ImageLoader imageLoader;
|
||||
|
||||
public static ReadingItemFragment newInstance(Story story) {
|
||||
ReadingItemFragment readingFragment = new ReadingItemFragment();
|
||||
|
@ -53,6 +54,7 @@ public class ReadingItemFragment extends Fragment {
|
|||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
imageLoader = ((NewsBlurApplication) getActivity().getApplicationContext()).getImageLoader();
|
||||
apiManager = new APIManager(getActivity());
|
||||
story = getArguments() != null ? (Story) getArguments().getSerializable("story") : null;
|
||||
}
|
||||
|
@ -97,7 +99,7 @@ public class ReadingItemFragment extends Fragment {
|
|||
TextView commentText = (TextView) commentView.findViewById(R.id.comment_text);
|
||||
commentText.setText(comment.commentText);
|
||||
ImageView commentImage = (ImageView) commentView.findViewById(R.id.comment_user_image);
|
||||
imageLoader.displayImage(Integer.toString(comment.userId), commentImage);
|
||||
imageLoader.displayImageByUid(Integer.toString(comment.userId), commentImage);
|
||||
TextView commentSharedDate = (TextView) commentView.findViewById(R.id.comment_shareddate);
|
||||
commentSharedDate.setText(comment.sharedDate);
|
||||
|
||||
|
@ -119,7 +121,7 @@ public class ReadingItemFragment extends Fragment {
|
|||
|
||||
GridLayout grid = (GridLayout) view.findViewById(R.id.reading_social_shareimages);
|
||||
grid.addView(image);
|
||||
imageLoader.displayImage(userId, image);
|
||||
imageLoader.displayImageByUid(userId, image);
|
||||
image.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
|
|
|
@ -39,6 +39,9 @@ public class ImageLoader {
|
|||
public void displayImage(String url, String uid, ImageView imageView) {
|
||||
imageViews.put(imageView, uid);
|
||||
Bitmap bitmap = memoryCache.get(uid);
|
||||
if (bitmap == null) {
|
||||
bitmap = memoryCache.get(url);
|
||||
}
|
||||
if (bitmap != null) {
|
||||
bitmap = UIUtils.roundCorners(bitmap, 10f);
|
||||
imageView.setImageBitmap(bitmap);
|
||||
|
@ -49,7 +52,7 @@ public class ImageLoader {
|
|||
}
|
||||
|
||||
// Display an image assuming it's in cache
|
||||
public void displayImage(String uid, ImageView imageView) {
|
||||
public void displayImageByUid(String uid, ImageView imageView) {
|
||||
Bitmap bitmap = memoryCache.get(uid);
|
||||
if (bitmap != null) {
|
||||
bitmap = UIUtils.roundCorners(bitmap, 10f);
|
||||
|
|
|
@ -13,8 +13,8 @@ public class MemoryCache {
|
|||
|
||||
private static final String TAG = "MemoryCache";
|
||||
private Map<String, Bitmap> cache = Collections.synchronizedMap(new LinkedHashMap<String, Bitmap>(10, 1.5f, true));
|
||||
private long size = 0;//current allocated size
|
||||
private long limit = 1000000;//max memory in bytes
|
||||
private long size = 0; //current allocated size
|
||||
private long limit = 1000000; //max memory in bytes
|
||||
|
||||
public MemoryCache(){
|
||||
setLimit(Runtime.getRuntime().maxMemory()/4);
|
||||
|
|
|
@ -17,6 +17,7 @@ import android.widget.ImageView;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.newsblur.R;
|
||||
import com.newsblur.activity.NewsBlurApplication;
|
||||
import com.newsblur.activity.Profile;
|
||||
import com.newsblur.network.domain.ActivitiesResponse;
|
||||
import com.newsblur.util.ImageLoader;
|
||||
|
@ -33,7 +34,7 @@ public class ActivitiesAdapter extends ArrayAdapter<ActivitiesResponse> {
|
|||
public ActivitiesAdapter(final Context context, final ActivitiesResponse[] activities) {
|
||||
super(context, R.id.row_activity_text);
|
||||
inflater = LayoutInflater.from(context);
|
||||
imageLoader = new ImageLoader(context);
|
||||
imageLoader = ((NewsBlurApplication) context.getApplicationContext()).getImageLoader();
|
||||
this.context = context;
|
||||
|
||||
for (ActivitiesResponse response : activities) {
|
||||
|
|
|
@ -8,6 +8,7 @@ import android.view.View;
|
|||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.newsblur.activity.NewsBlurApplication;
|
||||
import com.newsblur.database.DatabaseConstants;
|
||||
import com.newsblur.util.AppConstants;
|
||||
import com.newsblur.util.ImageLoader;
|
||||
|
@ -18,7 +19,7 @@ public class SocialFeedViewBinder implements ViewBinder {
|
|||
private ImageLoader imageLoader;
|
||||
|
||||
public SocialFeedViewBinder(final Context context) {
|
||||
imageLoader = new ImageLoader(context);
|
||||
imageLoader = ((NewsBlurApplication) context.getApplicationContext()).getImageLoader();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -43,7 +44,7 @@ public class SocialFeedViewBinder implements ViewBinder {
|
|||
return true;
|
||||
} else if (TextUtils.equals(cursor.getColumnName(columnIndex), DatabaseConstants.SOCIAL_FEED_ICON)) {
|
||||
String url = cursor.getString(columnIndex);
|
||||
imageLoader.displayImage(url, (ImageView) view);
|
||||
imageLoader.displayImage(url, url, (ImageView) view);
|
||||
return true;
|
||||
} else if (TextUtils.equals(cursor.getColumnName(columnIndex), DatabaseConstants.SOCIAL_FEED_NEGATIVE_COUNT)) {
|
||||
int feedNegative = cursor.getInt(columnIndex);
|
||||
|
|
Loading…
Add table
Reference in a new issue