Skip to content

Commit

Permalink
Downsample large skin image when loading
Browse files Browse the repository at this point in the history
  • Loading branch information
woesss committed Dec 13, 2023
1 parent 6d6d5c1 commit 8b082a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
42 changes: 40 additions & 2 deletions app/src/main/java/javax/microedition/lcdui/skin/SkinLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
package javax.microedition.lcdui.skin;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.RectF;

import androidx.core.content.ContextCompat;

import java.io.File;
import java.util.Objects;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.graphics.CanvasWrapper;
import javax.microedition.lcdui.overlay.Overlay;
import javax.microedition.util.ContextHolder;

import ru.playsoftware.j2meloader.config.Config;
import ru.playsoftware.j2meloader.config.ProfileModel;

public class SkinLayer implements Overlay {
Expand Down Expand Up @@ -69,8 +78,37 @@ public static SkinLayer getInstance() {
return instance;
}

public static void init(Bitmap image, ProfileModel settings) {
instance = new SkinLayer(image, settings);
public static void init(ProfileModel settings) {
File workDir = Objects.requireNonNull(settings.dir.getParentFile()).getParentFile();
File skin = new File(workDir + Config.SKINS_DIR + settings.screenBackgroundImage);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(skin.getPath(), opts);
if (opts.outWidth <= 0 || opts.outHeight <= 0) {
return;
}
Point p = new Point();
ContextCompat.getDisplayOrDefault(ContextHolder.getAppContext()).getSize(p);
int max = opts.outWidth / p.x;
int sample = opts.outWidth / p.y;
if (sample > max) {
max = sample;
}
sample = opts.outHeight / p.x;
if (sample > max) {
max = sample;
}
sample = opts.outHeight / p.y;
if (sample > max) {
max = sample;
}
opts.inSampleSize = max;
opts.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(skin.getPath(), opts);
if (bitmap == null) {
return;
}
instance = new SkinLayer(bitmap, settings);
}

@Override
Expand Down
7 changes: 2 additions & 5 deletions app/src/main/java/javax/microedition/shell/MicroLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
Expand Down Expand Up @@ -273,10 +272,8 @@ void applyConfiguration() {
if (sb.exists()) {
soundBank = sb.getPath();
}
File skin = new File(workDir + Config.SKINS_DIR + params.screenBackgroundImage);
Bitmap bitmap = BitmapFactory.decodeFile(skin.getPath(), null);
if (bitmap != null) {
SkinLayer.init(bitmap, params);
if (params.screenBackgroundImage != null) {
SkinLayer.init(params);
}
} catch (Exception e) {
e.printStackTrace();
Expand Down

0 comments on commit 8b082a8

Please sign in to comment.