尺寸不同的小图片合调整成大小一致图像居中的图片

该博客介绍如何将不同尺寸的小图片调整为相同大小并实现居中展示的方法,提供了相关代码链接。

尺寸不同的小图片合调整成大小一致图像居中的图片

代码这里:https://download.csdn.net/download/wulong710/11459923

Main.ts

//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

enum EStatus {
    idle,
    save,
    end
}

class Main extends egret.DisplayObjectContainer {

    public constructor() {
        super();

        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addStage, this);
    }


    public addStage(event: egret.Event): void {

        egret.lifecycle.addLifecycleListener((context: egret.lifecycle.LifecycleContext) => {
            context.onUpdate = this.onUpdate.bind(this);

        });

        egret.lifecycle.onPause = (): void => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = (): void => {
            egret.ticker.resume();
        }

        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.configComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");

    }

    public configComplete(event: RES.ResourceEvent): void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.configComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onLoadGroup, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onLoadGroupComplete, this);
        RES.loadGroup("anim");
    }

    public onLoadGroup(event: RES.ResourceEvent) {
        console.log("onLoadGroup process ", event.itemsLoaded + "/" + event.itemsTotal, );
    }

    public onLoadGroupComplete(event: RES.ResourceEvent) {
        RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onLoadGroup, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onLoadGroupComplete, this);
        this.parseAnims();
    }

    public getWidth(): number {
        return egret.MainContext.instance.stage.stageWidth;
    }

    public getHeight(): number {
        return egret.MainContext.instance.stage.stageHeight;
    }

    private SD_JSONS = [
        "explosion.json",
    ];

    private lastTime: number = 0;
    private pastTime: number = 0;
    private animIndex: number = 0;
    private mc: SubMovieClip;
    private sprite: egret.Sprite = null;
    private status: EStatus = EStatus.idle;
    private bg: egret.Shape;
    private rect: egret.Rectangle = null;
    private imgSaveCount: number = 0;
    private loadCount: number = 0;

    public parseAnims() {
        this.rect = new egret.Rectangle();

        this.bg = new egret.Shape();
        this.addChild(this.bg);
        this.bg.x = 0;
        this.bg.y = 0;
        this.bg.graphics.beginFill(0x000000, 0);
        this.bg.graphics.drawRect(0, 0, this.getWidth(), this.getHeight());
        this.bg.graphics.endFill();

        this.sprite = new egret.Sprite();
        this.sprite.x = 0;
        this.sprite.y = 0;
        this.addChild(this.sprite);
        this.loadNextAnim();
    }


    public loadNextAnim() {

        this.status = EStatus.idle;

        if (this.mc) {
            this.mc.parent.removeChildren();
            this.mc = null;
        }

        if (this.animIndex >= Object.keys(SD_anim).length) {
            egret.log("finish");
            this.status = EStatus.end;
            return;
        }

        let arr: string[] = SD_anim[Object.keys(SD_anim)[this.animIndex]];
        this.loadCount += arr.length;
        this.mc = new SubMovieClip(arr);
        this.mc.setCompleteAction(this.completeAction, this);
        this.sprite.addChild(this.mc);

        this.lastTime = egret.getTimer();
        this.pastTime = 0;

        //还能再改进,但是不做了。
        this.mc.gotoAction();
        this.mc.getMaxWidth();
        this.animIndex++;
        this.status = EStatus.save;
    }

    public onUpdate(): void {
        let now = egret.getTimer();
        let delta = now - this.lastTime;
        this.lastTime = now;

        if (this.mc) {
            if (this.status === EStatus.save) {
                //andrew 保存图片的时候,时间太短texture.saveToFile会执行失败
                if (this.pastTime < 100) {
                    this.pastTime += delta;
                    return;
                }
            }

            if (this.status === EStatus.idle) {

            } else if (this.status === EStatus.save) {
                let img = this.mc.getImageName();
                let success = this.mc.runAction();
                if (success) {
                    this.rect.x = 0;
                    this.rect.y = 0;
                    this.rect.width = this.mc.getMaxWidth();
                    this.rect.height = this.mc.getMaxHeight();


                    if (img && img.length > 0) {
                        this.imgSaveCount++;
                        this.pastTime = 0;
                        let renderTexture = new egret.RenderTexture();
                        renderTexture.drawToTexture(this, this.rect);
                        let savePath = img;
                        renderTexture.saveToFile("image/png", savePath, this.rect);
                        renderTexture.dispose();
                    }
                    egret.log("image = ", img, ", width = ", this.rect.width, ", height = ", this.rect.height, ", this.imgDrawCount = ", this.imgSaveCount, ", this.loadCount = ", this.loadCount, ", img = ", img);

                }
            }
        }
    }

    private completeAction() {
        this.loadNextAnim();
    }
}

SD_anim.ts

let SD_anim:any = {
explosion: [
        "explosion_01.png",
        "explosion_02.png",
        "explosion_03.png",
        "explosion_04.png",
        "explosion_05.png",
        "explosion_06.png",
        "explosion_07.png",
        "explosion_08.png",
        "explosion_09.png",
        "explosion_10.png",
        "explosion_11.png",
        "explosion_12.png",
        "explosion_13.png",
        "explosion_14.png",
        "explosion_15.png",
   ],
fish_blue_firework: [
        "fish_blue_firework_01.png",
        "fish_blue_firework_02.png",
        "fish_blue_firework_03.png",
        "fish_blue_firework_04.png",
        "fish_blue_firework_05.png",
        "fish_blue_firework_06.png",
        "fish_blue_firework_07.png",
        "fish_blue_firework_08.png",
        "fish_blue_firework_09.png",
        "fish_blue_firework_10.png",
        "fish_blue_firework_11.png",
        "fish_blue_firework_12.png",
        "fish_blue_firework_13.png",
        "fish_blue_firework_14.png",
        "fish_blue_firework_15.png",
        "fish_blue_firework_16.png",
   ],
fish_change_weapon: [
        "fish_change_weapon_01.png",
        "fish_change_weapon_02.png",
        "fish_change_weapon_03.png",
        "fish_change_weapon_04.png",
        "fish_change_weapon_05.png",
        "fish_change_weapon_06.png",
   ],
fish_goldflash: [
        "fish_goldflash_01.png",
        "fish_goldflash_02.png",
        "fish_goldflash_03.png",
        "fish_goldflash_04.png",
        "fish_goldflash_05.png",
        "fish_goldflash_06.png",
        "fish_goldflash_07.png",
        "fish_goldflash_08.png",
        "fish_goldflash_09.png",
        "fish_goldflash_10.png",
        "fish_goldflash_11.png",
        "fish_goldflash_12.png",
        "fish_goldflash_13.png",
        "fish_goldflash_14.png",
        "fish_goldflash_15.png",
        "fish_goldflash_16.png",
        "fish_goldflash_17.png",
        "fish_goldflash_18.png",
   ],
fish_goldfly: [
        "fish_goldfly_01.png",
        "fish_goldfly_02.png",
        "fish_goldfly_03.png",
        "fish_goldfly_04.png",
        "fish_goldfly_05.png",
        "fish_goldfly_06.png",
        "fish_goldfly_07.png",
   ],
fish_net_10: [
        "fish_net_10_00.png",
        "fish_net_10_01.png",
        "fish_net_10_02.png",
        "fish_net_10_03.png",
        "fish_net_10_04.png",
        "fish_net_10_05.png",
        "fish_net_10_06.png",
        "fish_net_10_07.png",
        "fish_net_10_08.png",
        "fish_net_10_09.png",
        "fish_net_10_10.png",
        "fish_net_10_11.png",
   ],
fish_net_3: [
        "fish_net_3_00.png",
        "fish_net_3_01.png",
        "fish_net_3_02.png",
        "fish_net_3_03.png",
        "fish_net_3_04.png",
        "fish_net_3_05.png",
        "fish_net_3_06.png",
        "fish_net_3_07.png",
        "fish_net_3_08.png",
        "fish_net_3_09.png",
        "fish_net_3_10.png",
        "fish_net_3_11.png",
   ],
fish_net_4: [
        "fish_net_4_00.png",
        "fish_net_4_01.png",
        "fish_net_4_02.png",
        "fish_net_4_03.png",
        "fish_net_4_04.png",
        "fish_net_4_05.png",
        "fish_net_4_06.png",
        "fish_net_4_07.png",
        "fish_net_4_08.png",
        "fish_net_4_09.png",
        "fish_net_4_10.png",
        "fish_net_4_11.png",
   ],
fish_net_5: [
        "fish_net_5_00.png",
        "fish_net_5_01.png",
        "fish_net_5_02.png",
        "fish_net_5_03.png",
        "fish_net_5_04.png",
        "fish_net_5_05.png",
        "fish_net_5_06.png",
        "fish_net_5_07.png",
        "fish_net_5_08.png",
        "fish_net_5_09.png",
        "fish_net_5_10.png",
        "fish_net_5_11.png",
   ],
fish_net_6: [
        "fish_net_6_00.png",
        "fish_net_6_01.png",
        "fish_net_6_02.png",
        "fish_net_6_03.png",
        "fish_net_6_04.png",
        "fish_net_6_05.png",
        "fish_net_6_06.png",
        "fish_net_6_07.png",
        "fish_net_6_08.png",
        "fish_net_6_09.png",
        "fish_net_6_10.png",
   ],
fish_net_7: [
        "fish_net_7_00.png",
        "fish_net_7_01.png",
        "fish_net_7_02.png",
        "fish_net_7_03.png",
        "fish_net_7_04.png",
        "fish_net_7_05.png",
        "fish_net_7_06.png",
        "fish_net_7_07.png",
        "fish_net_7_08.png",
        "fish_net_7_09.png",
   ],
fish_net_8: [
        "fish_net_8_00.png",
        "fish_net_8_01.png",
        "fish_net_8_02.png",
        "fish_net_8_03.png",
        "fish_net_8_04.png",
        "fish_net_8_05.png",
        "fish_net_8_06.png",
        "fish_net_8_07.png",
        "fish_net_8_08.png",
        "fish_net_8_09.png",
   ],
fish_net_9: [
        "fish_net_9_00.png",
        "fish_net_9_01.png",
        "fish_net_9_02.png",
        "fish_net_9_03.png",
        "fish_net_9_04.png",
        "fish_net_9_05.png",
        "fish_net_9_06.png",
        "fish_net_9_07.png",
        "fish_net_9_08.png",
        "fish_net_9_09.png",
   ],
fish_yellow_firework: [
        "fish_yellow_firework_01.png",
        "fish_yellow_firework_02.png",
        "fish_yellow_firework_03.png",
        "fish_yellow_firework_04.png",
        "fish_yellow_firework_05.png",
        "fish_yellow_firework_06.png",
        "fish_yellow_firework_07.png",
        "fish_yellow_firework_08.png",
        "fish_yellow_firework_09.png",
        "fish_yellow_firework_10.png",
        "fish_yellow_firework_11.png",
        "fish_yellow_firework_12.png",
        "fish_yellow_firework_13.png",
        "fish_yellow_firework_14.png",
        "fish_yellow_firework_15.png",
        "fish_yellow_firework_16.png",
   ],
};

SubMovieClip.ts

/**
 * Created by yangsong on 2017/10/11.
 */

class DefaultAction {
    public static Normal: string = "normal";
    public static Die: string = "die";
}



class SubMovieData {
    private imgArray: string[] = null;

    public constructor(pngNames: string[]) {
        this.imgArray = pngNames.slice(0);
    }

    public getImagArray(): string[] {
        return this.imgArray;
    }

    public getTextureByFrame(frame: number): egret.Texture {
        if (frame < this.imgArray.length) {
            let png = this.imgArray[frame];
            let res = this.getResPngName(png);

            return RES.getRes(res);
        }
        return null;
    }

    public getResPngName(pngName: string): string {
        let index = pngName.lastIndexOf(".png");
        if (index !== -1) {
            return pngName.substring(0, index).concat("_png");
        }
        return pngName;
    }
}

/**
 * @description 只管播放动画。旋转角度的问题交给 rotation属性
 * @author (..)
 * @date 2018-10-31
 * @class DefaultMovieClip
 * @extends {egret.Bitmap}
 */
class SubMovieClip extends egret.Bitmap {
    private mcData: SubMovieData = null;
    private currFrame: number;
    private endFrame: number;
    private currFrameTime: number;
    private completeAction: Function;
    private completeActionObj: any;
    private maxWidth: number = 0;//每一帧图片大小不一,取最大那张
    private maxHeight: number = 0;//每一帧图片大小不一,取最大那张
    private currPlayNum: number = 0;
    private totalPlayNum: number = 1;

    public constructor(pngs: string[]) {
        super();
        this.mcData = new SubMovieData(pngs);
    }

    private setBitmap(texture: egret.Texture, scaleX: number): void {
        this.texture = texture;
        this.scaleX = scaleX;

        //TODO: andrew 这里偏移后子弹击中检测会有问题
        // AnchorUtil.setAnchorX(this, 0.5);
        // AnchorUtil.setAnchorY(this, 1);
    }

    public getImageName(): string {
        let name = null;
        let images: string[] = this.mcData.getImagArray();
        if (images.length > 0) {
            name = images[this.currFrame]
        }
        return name;
    }

    public setDefault(resName: string): void {
        this.setBitmap(RES.getRes(resName), 1);
    }

    public runAction(): boolean {
        if (!this.mcData && this.endFrame > 0) {
            return false;
        }

        if (this.currFrame > this.endFrame) {
            this.currFrame = 0;
            this.currPlayNum++;
        }

        if (this.currPlayNum >= this.totalPlayNum) {
            this.completeAction && this.completeAction.apply(this.completeActionObj);
            return false;
        } else {
            var bitmapTexture: egret.Texture = this.mcData.getTextureByFrame(this.currFrame);
            this.setBitmap(bitmapTexture, this.scaleX);
            this.adjustPosition();
            this.currFrame++;
            return true;
        }
    }

    public gotoAction(): void {
        if (!this.mcData || this.mcData.getImagArray().length <= 0) {
            return;
        }

        this.currFrame = 0;
        this.currFrameTime = 0;
        this.endFrame = this.mcData.getImagArray().length - 1;
        this.currPlayNum = 0;
        this.totalPlayNum = 1;

        this.maxWidth = 0;
        this.maxHeight = 0;
        for (let frame = 0; frame <= this.endFrame; frame++) {
            let texture: egret.Texture = this.mcData.getTextureByFrame(frame);
            if (texture) {
                if (texture.textureWidth > this.maxWidth) {
                    this.maxWidth = texture.textureWidth;
                }

                if (texture.textureHeight > this.maxHeight) {
                    this.maxHeight = texture.textureHeight;
                }
            }
        }
    }

    public setCompleteAction(complateAction: Function, completeActionObj: any): void {
        this.completeAction = complateAction;
        this.completeActionObj = completeActionObj;
    }

    private adjustPosition() {
        let texture: egret.Texture = this.mcData.getTextureByFrame(this.currFrame);
        let x = this.maxWidth / 2;
        let y = this.maxHeight / 2;
        this.anchorOffsetX = texture.textureWidth / 2;
        this.anchorOffsetY = texture.textureHeight / 2;
        this.x = x;
        this.y = y;
    }

    public destroy(): void {
        if (this.parent) {
            let parent = this.parent;
            parent.removeChild(this);
        }

        // App.DisplayUtils.removeFromParent(this);
        // ObjectPool.push(this);

        this.texture = null;

        this.mcData = null;
        this.currFrame = null;
        this.endFrame = null;
        this.currFrameTime = null;
        this.totalPlayNum = 1;
        this.currPlayNum = null;
        this.completeAction = null;
        this.completeActionObj = null;
    }

    public getMaxWidth(): number {
        return this.maxWidth;
    }

    public getMaxHeight(): number {
        return this.maxHeight;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值