better icon loading

This commit is contained in:
olebeck 2024-05-02 23:10:13 +02:00
parent df15995772
commit a7ee667b9c
2 changed files with 85 additions and 42 deletions

View File

@ -13,41 +13,89 @@ import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class DownloadImage {
static void download(Context context, String url, ImageView imageView, File cacheIcon, String titleId) {
Handler handler = new Handler(context.getMainLooper());
static ExecutorService pool = Executors.newCachedThreadPool();
private static final Map<String, ArrayList<imageCallback>> downloadTasks = new HashMap<>();
new Thread(() -> {
try {
InputStream in = new URL(url).openStream();
OutputStream out = new FileOutputStream(cacheIcon);
private interface imageCallback {
void run(Bitmap image);
}
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close();
final Bitmap icon = BitmapFactory.decodeFile(cacheIcon.getAbsolutePath());
handler.post(() -> {
String tag = (String)imageView.getTag();
if(!titleId.equals(tag)) {
return;
}
AlphaAnimation animation = new AlphaAnimation(0f, 1f);
animation.setDuration(250);
animation.setFillAfter(true);
imageView.setAnimation(animation);
imageView.setImageBitmap(icon);
});
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
static imageCallback getImageCallback(String filename, ImageView imageView, Handler handler) {
return image -> {
String tag = (String)imageView.getTag();
if(!filename.equals(tag)) {
return;
}
}).start();
AlphaAnimation animation = new AlphaAnimation(0f, 1f);
animation.setDuration(250);
animation.setFillAfter(true);
imageView.setAnimation(animation);
imageView.setImageBitmap(image);
};
}
static void loadImage(Context context, URL url, String filename, ImageView imageView) {
Handler handler = new Handler(context.getMainLooper());
imageView.setTag(filename);
imageView.setImageBitmap(null);
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.outWidth = imageView.getWidth();
bitmapOptions.outHeight = imageView.getHeight();
File cachedIcon = new File(context.getCacheDir(), filename);
synchronized (downloadTasks) {
if(downloadTasks.containsKey(filename)) {
downloadTasks.get(filename).add(getImageCallback(filename, imageView, handler));
} else if(cachedIcon.exists()) {
final Bitmap icon = BitmapFactory.decodeFile(cachedIcon.getAbsolutePath(), bitmapOptions);
imageView.setImageBitmap(icon);
} else {
ArrayList<imageCallback> callbacks = new ArrayList<>();
callbacks.add(getImageCallback(filename, imageView, handler));
downloadTasks.put(filename, callbacks);
pool.submit(() -> {
try {
InputStream in = url.openStream();
OutputStream out = new FileOutputStream(cachedIcon);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
out.close();
in.close();
synchronized (downloadTasks) {
downloadTasks.remove(filename);
}
final Bitmap icon = BitmapFactory.decodeFile(cachedIcon.getAbsolutePath(), bitmapOptions);
handler.post(() -> {
synchronized (downloadTasks) {
for (imageCallback callback : callbacks) {
callback.run(icon);
}
}
});
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
});
}
}
}
}

View File

@ -1,8 +1,6 @@
package com.psmreborn.pkga;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -13,7 +11,8 @@ import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class GameListAdapter extends ArrayAdapter<Game> implements Filterable {
@ -45,14 +44,10 @@ public class GameListAdapter extends ArrayAdapter<Game> implements Filterable {
});
ImageView icon = (ImageView)convertView.findViewById(R.id.download_item_image);
File cachedIcon = new File(getContext().getCacheDir(), game.titleId+".png");
if(cachedIcon.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(cachedIcon.getAbsolutePath());
icon.setImageBitmap(bitmap);
} else {
icon.setImageBitmap(null);
icon.setTag(game.titleId);
DownloadImage.download(getContext(), String.format("http://psmreborn.com/gameinfo/%s/icon_128x128.png", game.titleId), icon, cachedIcon, game.titleId);
try {
DownloadImage.loadImage(getContext(), new URL(String.format("http://psmreborn.com/gameinfo/%s/icon_128x128.png", game.titleId)), game.titleId+"_icon.png", icon);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
return convertView;