ScriptModで画像を効率的に読み込んでみよう
端末の解像度より大きい画像を準備していてそのまま読み込むと無駄が出ますよね?(メモリにも優しくないですし)
あなたはSですか?
今回は テクスチャから効率的に読み込む方法を紹介します。
decodeAssets.js
function decodeAssets(url,width,height){
const byte=ModPE.getBytesFromTexturePack(url);
// テクスチャからバイト配列を取得
if(byte===null)return null;
const options=new android.graphics.BitmapFactory.Options();
options.inJustDecodeBounds=true;
// Bitmapを読み込まないように設定
android.graphics.BitmapFactory.decodeByteArray(byte,0,byte.length,options);
const bitmap_height=options.outHeight-0;
// 高さ取得
const bitmap_width=options.outWidth-0;
// 幅取得
let inSampleSize=1;
if(bitmap_height>height||bitmap_width>width){
if(bitmap_width>bitmap_height)
inSampleSize=Math.round(bitmap_height/height);
else
inSampleSize=Math.round(bitmap_width/width);
}
options.inSampleSize=inSampleSize;
// 指定のサイズより大きかった場合リサイズして読み込むように設定
options.inJustDecodeBounds=true;
// Bitmapを読み込むように設定
return android.graphics.BitmapFactory.decodeByteArray(byte,0,byte.length,options);
}
例
横1920 縦1080 の画像を用意したします。
しかし、端末の解像度は 960 540 しかありません。
decodeAssets("images/test.png",960,540);