Visual improvements when loading images
This commit is contained in:
parent
7df102daa3
commit
dbdda5643c
@ -2,105 +2,115 @@ package de.danoeh.antennapod.asynctask;
|
|||||||
|
|
||||||
import android.content.res.TypedArray;
|
import android.content.res.TypedArray;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.drawable.BitmapDrawable;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.graphics.drawable.TransitionDrawable;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import de.danoeh.antennapod.AppConfig;
|
import de.danoeh.antennapod.AppConfig;
|
||||||
|
import de.danoeh.antennapod.PodcastApp;
|
||||||
import de.danoeh.antennapod.R;
|
import de.danoeh.antennapod.R;
|
||||||
import de.danoeh.antennapod.asynctask.ImageLoader.ImageWorkerTaskResource;
|
import de.danoeh.antennapod.asynctask.ImageLoader.ImageWorkerTaskResource;
|
||||||
import de.danoeh.antennapod.util.BitmapDecoder;
|
import de.danoeh.antennapod.util.BitmapDecoder;
|
||||||
|
|
||||||
public class BitmapDecodeWorkerTask extends Thread {
|
public class BitmapDecodeWorkerTask extends Thread {
|
||||||
|
|
||||||
protected int PREFERRED_LENGTH;
|
protected int PREFERRED_LENGTH;
|
||||||
|
public static final int FADE_DURATION = 500;
|
||||||
|
|
||||||
/** Can be thumbnail or cover */
|
/**
|
||||||
protected int imageType;
|
* Can be thumbnail or cover
|
||||||
|
*/
|
||||||
|
protected int imageType;
|
||||||
|
|
||||||
private static final String TAG = "BitmapDecodeWorkerTask";
|
private static final String TAG = "BitmapDecodeWorkerTask";
|
||||||
private ImageView target;
|
private ImageView target;
|
||||||
protected CachedBitmap cBitmap;
|
protected CachedBitmap cBitmap;
|
||||||
|
|
||||||
protected ImageLoader.ImageWorkerTaskResource imageResource;
|
protected ImageLoader.ImageWorkerTaskResource imageResource;
|
||||||
|
|
||||||
private Handler handler;
|
private Handler handler;
|
||||||
|
|
||||||
private final int defaultCoverResource;
|
private final int defaultCoverResource;
|
||||||
|
|
||||||
public BitmapDecodeWorkerTask(Handler handler, ImageView target,
|
public BitmapDecodeWorkerTask(Handler handler, ImageView target,
|
||||||
ImageWorkerTaskResource imageResource, int length, int imageType) {
|
ImageWorkerTaskResource imageResource, int length, int imageType) {
|
||||||
super();
|
super();
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
this.imageResource = imageResource;
|
this.imageResource = imageResource;
|
||||||
this.PREFERRED_LENGTH = length;
|
this.PREFERRED_LENGTH = length;
|
||||||
this.imageType = imageType;
|
this.imageType = imageType;
|
||||||
TypedArray res = target.getContext().obtainStyledAttributes(
|
this.defaultCoverResource = android.R.color.transparent;
|
||||||
new int[] { R.attr.default_cover });
|
}
|
||||||
this.defaultCoverResource = res.getResourceId(0, 0);
|
|
||||||
res.recycle();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should return true if tag of the imageview is still the same it was
|
* Should return true if tag of the imageview is still the same it was
|
||||||
* before the bitmap was decoded
|
* before the bitmap was decoded
|
||||||
*/
|
*/
|
||||||
protected boolean tagsMatching(ImageView target) {
|
protected boolean tagsMatching(ImageView target) {
|
||||||
return target.getTag(R.id.imageloader_key) == null
|
return target.getTag(R.id.imageloader_key) == null
|
||||||
|| target.getTag(R.id.imageloader_key).equals(imageResource.getImageLoaderCacheKey());
|
|| target.getTag(R.id.imageloader_key).equals(imageResource.getImageLoaderCacheKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onPostExecute() {
|
protected void onPostExecute() {
|
||||||
// check if imageview is still supposed to display this image
|
// check if imageview is still supposed to display this image
|
||||||
if (tagsMatching(target) && cBitmap.getBitmap() != null) {
|
if (tagsMatching(target) && cBitmap.getBitmap() != null) {
|
||||||
target.setImageBitmap(cBitmap.getBitmap());
|
Drawable[] drawables = new Drawable[]{
|
||||||
} else {
|
PodcastApp.getInstance().getResources().getDrawable(android.R.color.transparent),
|
||||||
if (AppConfig.DEBUG)
|
new BitmapDrawable(PodcastApp.getInstance().getResources(), cBitmap.getBitmap())
|
||||||
Log.d(TAG, "Not displaying image");
|
};
|
||||||
}
|
TransitionDrawable transitionDrawable = new TransitionDrawable(drawables);
|
||||||
}
|
target.setImageDrawable(transitionDrawable);
|
||||||
|
transitionDrawable.startTransition(FADE_DURATION);
|
||||||
|
} else {
|
||||||
|
if (AppConfig.DEBUG)
|
||||||
|
Log.d(TAG, "Not displaying image");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
cBitmap = new CachedBitmap(BitmapDecoder.decodeBitmapFromWorkerTaskResource(
|
cBitmap = new CachedBitmap(BitmapDecoder.decodeBitmapFromWorkerTaskResource(
|
||||||
PREFERRED_LENGTH, imageResource), PREFERRED_LENGTH);
|
PREFERRED_LENGTH, imageResource), PREFERRED_LENGTH);
|
||||||
if (cBitmap.getBitmap() != null) {
|
if (cBitmap.getBitmap() != null) {
|
||||||
storeBitmapInCache(cBitmap);
|
storeBitmapInCache(cBitmap);
|
||||||
} else {
|
} else {
|
||||||
Log.w(TAG, "Could not load bitmap. Using default image.");
|
Log.w(TAG, "Could not load bitmap. Using default image.");
|
||||||
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(
|
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(
|
||||||
target.getResources(), defaultCoverResource),
|
target.getResources(), defaultCoverResource),
|
||||||
PREFERRED_LENGTH);
|
PREFERRED_LENGTH);
|
||||||
}
|
}
|
||||||
if (AppConfig.DEBUG)
|
if (AppConfig.DEBUG)
|
||||||
Log.d(TAG, "Finished loading bitmaps");
|
Log.d(TAG, "Finished loading bitmaps");
|
||||||
|
|
||||||
endBackgroundTask();
|
endBackgroundTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final void endBackgroundTask() {
|
protected final void endBackgroundTask() {
|
||||||
handler.post(new Runnable() {
|
handler.post(new Runnable() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
onPostExecute();
|
onPostExecute();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void onInvalidStream() {
|
protected void onInvalidStream() {
|
||||||
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(
|
cBitmap = new CachedBitmap(BitmapFactory.decodeResource(
|
||||||
target.getResources(), defaultCoverResource), PREFERRED_LENGTH);
|
target.getResources(), defaultCoverResource), PREFERRED_LENGTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void storeBitmapInCache(CachedBitmap cb) {
|
protected void storeBitmapInCache(CachedBitmap cb) {
|
||||||
ImageLoader loader = ImageLoader.getInstance();
|
ImageLoader loader = ImageLoader.getInstance();
|
||||||
if (imageType == ImageLoader.IMAGE_TYPE_COVER) {
|
if (imageType == ImageLoader.IMAGE_TYPE_COVER) {
|
||||||
loader.addBitmapToCoverCache(imageResource.getImageLoaderCacheKey(), cb);
|
loader.addBitmapToCoverCache(imageResource.getImageLoaderCacheKey(), cb);
|
||||||
} else if (imageType == ImageLoader.IMAGE_TYPE_THUMBNAIL) {
|
} else if (imageType == ImageLoader.IMAGE_TYPE_THUMBNAIL) {
|
||||||
loader.addBitmapToThumbnailCache(imageResource.getImageLoaderCacheKey(), cb);
|
loader.addBitmapToThumbnailCache(imageResource.getImageLoaderCacheKey(), cb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ public class ImageLoader {
|
|||||||
|
|
||||||
private ExecutorService createExecutor() {
|
private ExecutorService createExecutor() {
|
||||||
return Executors.newFixedThreadPool(Runtime.getRuntime()
|
return Executors.newFixedThreadPool(Runtime.getRuntime()
|
||||||
.availableProcessors() + 1, new ThreadFactory() {
|
.availableProcessors(), new ThreadFactory() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Thread newThread(Runnable r) {
|
public Thread newThread(Runnable r) {
|
||||||
@ -196,11 +196,7 @@ public class ImageLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int getDefaultCoverResource(Context context) {
|
private int getDefaultCoverResource(Context context) {
|
||||||
TypedArray res = context
|
return android.R.color.transparent;
|
||||||
.obtainStyledAttributes(new int[] { R.attr.default_cover });
|
|
||||||
final int defaultCoverResource = res.getResourceId(0, 0);
|
|
||||||
res.recycle();
|
|
||||||
return defaultCoverResource;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user