mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-18 09:07:48 -04:00
Feat[icon]: automatically resize cached icons to 256x256 and compress as JPEG
This commit is contained in:
parent
ecbbbfcc4d
commit
be0d98e09a
@ -1,10 +1,15 @@
|
|||||||
package net.kdt.pojavlaunch.modloaders.modpacks.imagecache;
|
package net.kdt.pojavlaunch.modloaders.modpacks.imagecache;
|
||||||
|
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
|
||||||
import net.kdt.pojavlaunch.utils.DownloadUtils;
|
import net.kdt.pojavlaunch.utils.DownloadUtils;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
class DownloadImageTask implements Runnable {
|
class DownloadImageTask implements Runnable {
|
||||||
|
private static final float BITMAP_FINAL_DIMENSION = 256f;
|
||||||
private final ReadFromDiskTask mParentTask;
|
private final ReadFromDiskTask mParentTask;
|
||||||
private int mRetryCount;
|
private int mRetryCount;
|
||||||
DownloadImageTask(ReadFromDiskTask parentTask) {
|
DownloadImageTask(ReadFromDiskTask parentTask) {
|
||||||
@ -28,6 +33,25 @@ class DownloadImageTask implements Runnable {
|
|||||||
try {
|
try {
|
||||||
IconCacheJanitor.waitForJanitorToFinish();
|
IconCacheJanitor.waitForJanitorToFinish();
|
||||||
DownloadUtils.downloadFile(mParentTask.imageUrl, mParentTask.cacheFile);
|
DownloadUtils.downloadFile(mParentTask.imageUrl, mParentTask.cacheFile);
|
||||||
|
Bitmap bitmap = BitmapFactory.decodeFile(mParentTask.cacheFile.getAbsolutePath());
|
||||||
|
if(bitmap == null) return false;
|
||||||
|
int bitmapWidth = bitmap.getWidth(), bitmapHeight = bitmap.getHeight();
|
||||||
|
if(bitmapWidth <= BITMAP_FINAL_DIMENSION && bitmapHeight <= BITMAP_FINAL_DIMENSION) {
|
||||||
|
bitmap.recycle();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
float imageRescaleRatio = Math.min(BITMAP_FINAL_DIMENSION/bitmapWidth, BITMAP_FINAL_DIMENSION/bitmapHeight);
|
||||||
|
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap,
|
||||||
|
(int)(bitmapWidth * imageRescaleRatio),
|
||||||
|
(int)(bitmapHeight * imageRescaleRatio),
|
||||||
|
true);
|
||||||
|
bitmap.recycle();
|
||||||
|
if(resizedBitmap == bitmap) return true;
|
||||||
|
try (FileOutputStream fileOutputStream = new FileOutputStream(mParentTask.cacheFile)) {
|
||||||
|
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fileOutputStream);
|
||||||
|
} finally {
|
||||||
|
resizedBitmap.recycle();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}catch (IOException e) {
|
}catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user