andengine--图片资源加载方式

 

 

http://blog.csdn.net/ajiangro/article/details/12648693

 

①         单张图片

/**

* welldone

*/

publicvoid loadResource() {

        bitmapTextureAtlas_welldone = new BitmapTextureAtlas(getTextureManager(), 668, 128);  //图片大小即可

        tiledTextureRegion_welldone  = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(bitmapTextureAtlas_welldone, this, "welldone.png", 0, 0, 1, 1);    

        bitmapTextureAtlas_welldone.load();

}

②         单张图片精灵表

/**

 * good

*/

publicvoid loadResource() {

        bitmapTextureAtlas_good = new BitmapTextureAtlas(getTextureManager(), 1762, 1995);

        tiledTextureRegion_good  = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(bitmapTextureAtlas_good, this, "good.png", 0, 0, 5, 7);  

        bitmapTextureAtlas_good .load();

}

③         多张图片加载精灵表一行排列

publicvoid loadResource() {

bitmapTextureAtlas_fullbox = new BitmapTextureAtlas(getTextureManager(),1024, 256);

        textureRegion_fullbox = new TextureRegion[4];

        for (int i = 0; i < 4; i++) {

            textureRegion_fullbox[i] = (TextureRegion) BitmapTextureAtlasTextureRegionFactory

                    .createFromAsset(bitmapTextureAtlas_fullbox, this,"boxfull_" + i + ".png", i * 239, 0);

        }

        tiledTextureRegion_fullbox = new TiledTextureRegion(bitmapTextureAtlas_fullbox, textureRegion_fullbox);

        bitmapTextureAtlas_fullbox.load();

}

 

④         多张图片加载精灵表多行多列

publicvoid loadResource() {

bitmapTextureAtlas_fullbox = new BitmapTextureAtlas(getTextureManager(),1024, 1024);

        textureRegion_fullbox = new TextureRegion[12];

        for (int j = 0; j < 3; j++) {

for (int i = 0; i < 4; i++) {

            textureRegion_fullbox[i] = (TextureRegion) BitmapTextureAtlasTextureRegionFactory

                    .createFromAsset(bitmapTextureAtlas_fullbox, this,"boxfull_" + i+j*4 + ".png", i * 239, j*128);  //图片名称String起始角标逻辑视情况而定

        }

}

        tiledTextureRegion_fullbox = new TiledTextureRegion(bitmapTextureAtlas_fullbox, textureRegion_fullbox);

        bitmapTextureAtlas_fullbox.load();

}

 

⑤         多张图片加载精灵表循环创建多个对象

/**

* good

*/

    public void  good() {

                for (int i = 0; i < 35; i++) {

                    bitmapTextureAtlas_good[i] = new BitmapTextureAtlas(getTextureManager(),

                            470, 380, TextureOptions.DEFAULT);

                    tiledTextureRegion_good[i] = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(bitmapTextureAtlas_good[i],

                            StartActivity.this, "good_"+i+".png", 0, 0, 1, 1);

                    bitmapTextureAtlas_good[i].load();

                }

        }

 

⑥         TexturePacker方式加载图片资源

需要implements a

publicvoid loadTexture() {

        TexturePack spriteTexturePack = null;

        try {

            spriteTexturePack = new TexturePackLoader(this.getAssets(), this.getTextureManager()).loadFromAsset("gfx/a.xml", "gfx/");

        } catch (TexturePackParseException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        spriteTexturePack.loadTexture();

        TexturePackTextureRegionLibrary library = spriteTexturePack.getTexturePackTextureRegionLibrary();

        textureRegion_combo_letter_0 = library.get(COMBO_LETTER_0_ID);

        textureRegion_combo_letter_1 = library.get(COMBO_LETTER_1_ID);

        textureRegion_combo_letter_2 = library.get(COMBO_LETTER_2_ID);

        textureRegion_combo_letter_3 = library.get(COMBO_LETTER_3_ID);

        textureRegion_combo_letter_4 = library.get(COMBO_LETTER_4_ID);

        textureRegion_combo_letter_5 = library.get(COMBO_LETTER_5_ID);

        textureRegion_combo_letter_6 = library.get(COMBO_LETTER_6_ID);

        textureRegion_combo_letter_7 = library.get(COMBO_LETTER_7_ID);

        textureRegion_combo_letter_8 = library.get(COMBO_LETTER_8_ID);

        textureRegion_combo_letter_9 = library.get(COMBO_LETTER_9_ID);

        textureRegion_combo_coin_0 = library.get(COMBO_COIN_0_ID);

        textureRegion_combo_coin_1 = library.get(COMBO_COIN_1_ID);

        textureRegion_combo_coin_2 = library.get(COMBO_COIN_2_ID);

        textureRegion_combo_coin_3 = library.get(COMBO_COIN_3_ID);

        textureRegion_combo_coin_4 = library.get(COMBO_COIN_4_ID);

        textureRegion_combo_coin_5 = library.get(COMBO_COIN_5_ID);

        textureRegion_combo_coin_6 = library.get(COMBO_COIN_6_ID);

        textureRegion_combo_coin_7 = library.get(COMBO_COIN_7_ID);

        textureRegion_combo_coin_8 = library.get(COMBO_COIN_8_ID);

        textureRegion_combo_coin_9 = library.get(COMBO_COIN_9_ID);

        

        

    }

⑦         从sd卡中读取图片

File resFile = new File("/mnt/sdcard/storage_image.png");  

        sdBitmapTextureAlAtlas = new BitmapTextureAtlas(getTextureManager(), 128, 128);  

        FileBitmapTextureAtlasSource fileBitmapTextureAtlasSource = FileBitmapTextureAtlasSource.create(resFile);  

        sdTextureRegion = (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromSource(sdBitmapTextureAlAtlas, fileBitmapTextureAtlasSource, 0, 0);  

        sdBitmapTextureAlAtlas.load();

⑧         从sd卡中读取索引文件通过索引顺序读取图片

            路径下有index.txt #分割图片名称;图片

file = new File(Path+"/index.txt");

            System.out.println(file);

            try{

                is=new FileInputStream(file);

                isr=new InputStreamReader(is);

                br=new BufferedReader(isr);

                System.out.println("Read: ");

                while((s=br.readLine())!=null) {

                    System.out.println("  "+s);

                    t=s.trim();

                    chStrings = t.split("#");

                }

            }catch(FileNotFoundException e){

                System.out.println("File "+Path+"/index.txt"+" not found or permission denied");

                System.exit(-2);

            }catch(IOException e){

                System.out.println("Error: "+e);

                System.exit(-3);

            }

for (int i = 0; i < chStrings.length; i++) {

            pngFile[i] = new File(Path+ "/"+ chStrings[i] +".png");

            BitmapTextureAtlas sdBitmapTextureAlAtlas = null;

            sdBitmapTextureAlAtlas = new BitmapTextureAtlas(getTextureManager(), 262*10, 270);  

                for (int i = 0; i < fileBitmapTextureAtlasSource.length; i++) {

                    fileBitmapTextureAtlasSource[i] = FileBitmapTextureAtlasSource.create(pngFile[i]); 

                    textureRegion_qq[i] = (TextureRegion) BitmapTextureAtlasTextureRegionFactory.createFromSource(sdBitmapTextureAlAtlas, fileBitmapTextureAtlasSource[i], i*262, 0);  

                }

                sdBitmapTextureAlAtlas.load();


 

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值