1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画像を効率的に読み込む(from Assets)

1
Last updated at Posted at 2017-02-03

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);

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?