Implemented async FeedImage loader
This commit is contained in:
parent
3768761e90
commit
84eff944ab
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
|
@ -10,6 +10,7 @@ import com.actionbarsherlock.view.Menu;
|
|||
import com.actionbarsherlock.view.MenuItem;
|
||||
|
||||
import de.podfetcher.R;
|
||||
import de.podfetcher.asynctask.FeedImageLoader;
|
||||
import de.podfetcher.feed.Feed;
|
||||
import de.podfetcher.feed.FeedManager;
|
||||
|
||||
|
@ -41,8 +42,8 @@ public class FeedInfoActivity extends SherlockActivity {
|
|||
txtvDescription = (TextView) findViewById(R.id.txtvDescription);
|
||||
txtvLanguage = (TextView) findViewById(R.id.txtvLanguage);
|
||||
txtvAuthor = (TextView) findViewById(R.id.txtvAuthor);
|
||||
FeedImageLoader.getInstance().loadBitmap(feed.getImage(), imgvCover);
|
||||
|
||||
imgvCover.setImageBitmap(feed.getImage().getImageBitmap());
|
||||
txtvTitle.setText(feed.getTitle());
|
||||
txtvDescription.setText(feed.getDescription());
|
||||
if (feed.getAuthor() != null) {
|
||||
|
|
|
@ -42,7 +42,6 @@ public class FeedItemlistActivity extends SherlockFragmentActivity {
|
|||
if(feedId == -1) Log.e(TAG, "Received invalid feed selection.");
|
||||
|
||||
feed = manager.getFeed(feedId);
|
||||
getSupportActionBar().setLogo(new BitmapDrawable(feed.getImage().getImageBitmap()));
|
||||
setTitle(feed.getTitle());
|
||||
|
||||
FragmentManager fragmentManager = getSupportFragmentManager();
|
||||
|
|
|
@ -69,8 +69,6 @@ public class ItemviewActivity extends SherlockFragmentActivity {
|
|||
private void populateUI() {
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
setContentView(R.layout.feeditemview);
|
||||
getSupportActionBar().setLogo(
|
||||
new BitmapDrawable(item.getFeed().getImage().getImageBitmap()));
|
||||
txtvTitle = (TextView) findViewById(R.id.txtvItemname);
|
||||
txtvPublished = (TextView) findViewById(R.id.txtvPublished);
|
||||
setTitle(item.getFeed().getTitle());
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.text.DateFormat;
|
|||
import java.util.List;
|
||||
|
||||
import de.podfetcher.R;
|
||||
import de.podfetcher.asynctask.FeedImageLoader;
|
||||
import de.podfetcher.feed.Feed;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
import android.content.Context;
|
||||
|
@ -25,12 +26,14 @@ public class FeedlistAdapter extends ArrayAdapter<Feed> {
|
|||
private static final String TAG = "FeedlistAdapter";
|
||||
|
||||
private int selectedItemIndex;
|
||||
private FeedImageLoader imageLoader;
|
||||
public static final int SELECTION_NONE = -1;
|
||||
|
||||
public FeedlistAdapter(Context context, int textViewResourceId,
|
||||
List<Feed> objects) {
|
||||
super(context, textViewResourceId, objects);
|
||||
selectedItemIndex = SELECTION_NONE;
|
||||
imageLoader = FeedImageLoader.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -82,16 +85,8 @@ public class FeedlistAdapter extends ArrayAdapter<Feed> {
|
|||
holder.newEpisodes.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
if (feed.getImage() != null) {
|
||||
holder.image.setImageBitmap(feed.getImage().getImageBitmap()); // TODO
|
||||
// select
|
||||
// default
|
||||
// picture
|
||||
// when
|
||||
// no
|
||||
// image
|
||||
// downloaded
|
||||
}
|
||||
imageLoader.loadBitmap(feed.getImage(), holder.image);
|
||||
|
||||
// TODO find new Episodes txtvNewEpisodes.setText(feed)
|
||||
return convertView;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,101 @@
|
|||
package de.podfetcher.asynctask;
|
||||
|
||||
import de.podfetcher.R;
|
||||
import de.podfetcher.feed.FeedImage;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.v4.util.LruCache;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
/** Caches and loads FeedImage bitmaps in the background */
|
||||
public class FeedImageLoader {
|
||||
private static FeedImageLoader singleton;
|
||||
|
||||
/**
|
||||
* Stores references to loaded bitmaps. Bitmaps can be accessed by the id of
|
||||
* the FeedImage the bitmap belongs to.
|
||||
*/
|
||||
private LruCache<Long, Bitmap> imageCache;
|
||||
private static final int CACHE_SIZE = 4 * 1024 * 1024;
|
||||
|
||||
private FeedImageLoader() {
|
||||
imageCache = new LruCache<Long, Bitmap>(CACHE_SIZE) {
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
@Override
|
||||
protected int sizeOf(Long key, Bitmap value) {
|
||||
if (Integer.valueOf(android.os.Build.VERSION.SDK_INT) >= 12)
|
||||
return value.getByteCount();
|
||||
else
|
||||
return (value.getRowBytes() * value.getHeight());
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public static FeedImageLoader getInstance() {
|
||||
if (singleton == null) {
|
||||
singleton = new FeedImageLoader();
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
public void loadBitmap(FeedImage image, ImageView target) {
|
||||
if (image != null) {
|
||||
Bitmap bitmap = getBitmapFromCache(image.getId());
|
||||
if (bitmap != null) {
|
||||
target.setImageBitmap(bitmap);
|
||||
} else {
|
||||
target.setImageResource(R.drawable.default_cover);
|
||||
BitmapWorkerTask worker = new BitmapWorkerTask(target);
|
||||
worker.execute(image);
|
||||
}
|
||||
} else {
|
||||
target.setImageResource(R.drawable.default_cover);
|
||||
}
|
||||
}
|
||||
|
||||
public void addBitmapToCache(long id, Bitmap bitmap) {
|
||||
imageCache.put(id, bitmap);
|
||||
}
|
||||
|
||||
public Bitmap getBitmapFromCache(long id) {
|
||||
return imageCache.get(id);
|
||||
}
|
||||
|
||||
class BitmapWorkerTask extends AsyncTask<FeedImage, Void, Void> {
|
||||
private static final String TAG = "BitmapWorkerTask";
|
||||
private ImageView target;
|
||||
private Bitmap bitmap;
|
||||
|
||||
public BitmapWorkerTask(ImageView target) {
|
||||
super();
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
super.onPostExecute(result);
|
||||
target.setImageBitmap(bitmap);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(FeedImage... params) {
|
||||
if (params[0].getFile_url() != null) {
|
||||
bitmap = BitmapFactory.decodeFile(params[0].getFile_url());
|
||||
addBitmapToCache(params[0].getId(), bitmap);
|
||||
Log.d(TAG, "Finished loading bitmaps");
|
||||
} else {
|
||||
Log.e(TAG, "FeedImage has no file url. Using default image");
|
||||
bitmap = BitmapFactory.decodeResource(target.getResources(),
|
||||
R.drawable.default_cover);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +1,17 @@
|
|||
package de.podfetcher.feed;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
public class FeedImage extends FeedFile {
|
||||
protected String title;
|
||||
protected Bitmap image_bitmap;
|
||||
|
||||
public FeedImage(String download_url, String title) {
|
||||
super(null, download_url, false);
|
||||
this.download_url = download_url;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public FeedImage(long id, String title, String file_url, String download_url, boolean downloaded) {
|
||||
|
||||
public FeedImage(long id, String title, String file_url,
|
||||
String download_url, boolean downloaded) {
|
||||
super(file_url, download_url, downloaded);
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
|
@ -26,22 +24,9 @@ public class FeedImage extends FeedFile {
|
|||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Bitmap getImageBitmap() {
|
||||
if(image_bitmap == null) {
|
||||
image_bitmap = BitmapFactory.decodeFile(getFile_url());
|
||||
}
|
||||
return image_bitmap;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import android.widget.TextView;
|
|||
import com.actionbarsherlock.app.SherlockFragment;
|
||||
|
||||
import de.podfetcher.R;
|
||||
import de.podfetcher.asynctask.FeedImageLoader;
|
||||
import de.podfetcher.feed.Feed;
|
||||
import de.podfetcher.feed.FeedItem;
|
||||
import de.podfetcher.feed.FeedManager;
|
||||
|
@ -42,7 +43,7 @@ public class CoverFragment extends SherlockFragment {
|
|||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
|
||||
FeedManager manager = FeedManager.getInstance();
|
||||
FeedItem item = null;
|
||||
Bundle args = getArguments();
|
||||
|
@ -68,7 +69,7 @@ public class CoverFragment extends SherlockFragment {
|
|||
imgvCover = (ImageView) root.findViewById(R.id.imgvCover);
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
@ -80,8 +81,8 @@ public class CoverFragment extends SherlockFragment {
|
|||
}
|
||||
|
||||
private void loadMediaInfo() {
|
||||
imgvCover.setImageBitmap(media.getItem().getFeed().getImage()
|
||||
.getImageBitmap());
|
||||
FeedImageLoader.getInstance().loadBitmap(
|
||||
media.getItem().getFeed().getImage(), imgvCover);
|
||||
txtvTitle.setText(media.getItem().getTitle());
|
||||
txtvFeed.setText(media.getItem().getFeed().getTitle());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue