[Android] Adapt async loading to data binding
Geoffrey Métais
git at videolan.org
Mon Sep 7 14:29:24 CEST 2015
vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon Sep 7 14:23:39 2015 +0200| [1f1a1411859927f0650496c00c390ce16b357243] | committer: Geoffrey Métais
Adapt async loading to data binding
> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=1f1a1411859927f0650496c00c390ce16b357243
---
.../src/org/videolan/vlc/gui/AsyncImageLoader.java | 33 ++++++++++----------
.../vlc/gui/audio/AudioBrowserListAdapter.java | 14 ++-------
2 files changed, 19 insertions(+), 28 deletions(-)
diff --git a/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java b/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
index f147660..fd5df92 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
+++ b/vlc-android/src/org/videolan/vlc/gui/AsyncImageLoader.java
@@ -24,8 +24,12 @@
package org.videolan.vlc.gui;
import android.app.Activity;
+import android.databinding.ViewDataBinding;
import android.graphics.Bitmap;
-import android.widget.ImageView;
+import android.graphics.drawable.BitmapDrawable;
+
+import org.videolan.vlc.BR;
+import org.videolan.vlc.VLCApplication;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
@@ -38,30 +42,27 @@ public class AsyncImageLoader {
static ExecutorService executor = Executors.newSingleThreadExecutor();
- public static void LoadImage(Callable<Bitmap> loader, final ImageView view, Activity activity){
+ public static void LoadImage(Callable<Bitmap> loader, final ViewDataBinding binding, Activity activity){
Future<Bitmap> future = executor.submit(loader);
- handleImage(view, activity, future);
+ handleImage(binding, activity, future);
}
- private static void handleImage(final ImageView view, final Activity activity, final Future<Bitmap> future) {
+ private static void handleImage(final ViewDataBinding binding, final Activity activity, final Future<Bitmap> future) {
new Thread(new Runnable() {
@Override
public void run() {
try {
final Bitmap bitmap = future.get();
-
- activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- view.post(new Runnable() {
- @Override
- public void run() {
- view.setImageBitmap(bitmap);
- }
- });
- }
- });
+ if (activity != null) {
+ activity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ binding.setVariable(BR.cover, new BitmapDrawable(VLCApplication.getAppResources(), bitmap));
+ binding.executePendingBindings();
+ }
+ });
+ }
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java
index 5d133df..a2bc098 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserListAdapter.java
@@ -26,18 +26,12 @@ import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
-import android.graphics.Bitmap;
-import android.graphics.drawable.BitmapDrawable;
-import android.os.Handler;
-import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v4.util.ArrayMap;
-import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
@@ -46,9 +40,8 @@ import android.widget.TextView;
import org.videolan.vlc.BR;
import org.videolan.vlc.MediaWrapper;
import org.videolan.vlc.R;
-import org.videolan.vlc.interfaces.IAudioClickHandler;
import org.videolan.vlc.gui.AsyncImageLoader;
-import org.videolan.vlc.util.BitmapCache;
+import org.videolan.vlc.interfaces.IAudioClickHandler;
import org.videolan.vlc.util.Util;
import java.util.ArrayList;
@@ -58,7 +51,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
-import java.util.concurrent.Callable;
public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndexer, IAudioClickHandler {
public final static String TAG = "VLC/AudioBrowserListAdapter";
@@ -337,9 +329,7 @@ public class AudioBrowserListAdapter extends BaseAdapter implements SectionIndex
if (mItemType == ITEM_WITH_COVER) {
AudioUtil.AudioCoverFetcher fetcher = new AudioUtil.AudioCoverFetcher(mContext, mItems.get(position).mMediaList);
- AsyncImageLoader.LoadImage(fetcher, holder.cover, mContext);
- //TODO
- //holder.binding.setVariable(BR.cover, new BitmapDrawable(mContext.getResources(), cover));
+ AsyncImageLoader.LoadImage(fetcher, holder.binding, mContext);
}
holder.binding.setVariable(BR.footer, !isMediaItemAboveASeparator(position));
More information about the Android
mailing list