动态图像大小调整

时间:2009-11-11 16:02:46

标签: flex actionscript-3 mxml

在我的flex应用程序中,有一个我正在使用的TileList的项呈示器。 我在运行时将图像加载到项呈示器中。我使用以下代码来调整图像的宽度和高度。但它似乎没有奏效。 我调试了flex应用程序,发现实际的宽度和高度值已经分配。但图像看起来不合适。 这是我的代码:

    private function Init() :void
{
    tileImg = new Image();
    tileImg.source = data.ICON;
    tileImg.toolTip = data.TITLE;
    tileImg.buttonMode = true;
    tileImg.addEventListener(Event.COMPLETE, AdjustImageDimensions); 
    this.addChild(tileImg);
} 

private function AdjustImageDimensions(e:Event):void
{ 
    tileImg.width = (e.currentTarget as Image).contentWidth; 
    tileImg.height = (e.currentTarget as Image).contentHeight;
}

2 个答案:

答案 0 :(得分:1)

触发contentHeight事件时,contentWidthcomplete属性的值不是最终值。您可以在触发updateComplete事件后获取值。

答案 1 :(得分:0)

好的,前几天我遇到了这样的问题,我使用Loader类修复了它。您可以在加载文件后获取信息。您还需要一个附加到加载器的事件监听器。从哪里获取图像? URL或文件系统?

这是一个简单的例子:

package 

{     import flash.display.Loader;     import flash.events.Event;     import flash.net.FileReference;

import mx.controls.Image;

public class MyItemRenderer
{
    private var fileRef:FileReference = new FileReference();
    private var myLoader:Loader = new Loader();
    public var myDynamicImage:Image;

    public function MyItemRenderer()
    {
        fileRef.addEventListener(Event.SELECT, loadImageToCache);
        fileRef.addEventListener(Event.COMPLETE, sendToLoader);

        fileRef.browse( /* put file types in this array */ );
    }

    private function loadImageToCache(e:Event):void
    {
        fileRef.load();
    }

    private function sendToLoader(e:Event):void
    {
        myLoader.loadBytes(fileRef.data);
        myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, createImage);
    }

    private function createImage(e:Event):void
    {
        var file:Loader = Loader(e.target);

        myDynamicImage = new Image();
        myDynamicImage.source = file;
        myDynamicImage.width = file.width;
        myDynamicImage.height = file.height;
    }
    //now that would be great if you can control the width and height 
    //of these images at runtime, BUT, 
    //chances are if their dynamically loaded, you can't so here...

    private function getImgAR(file:Loader):Number
    {
        return file.width/file.height;
    }

    //then use this createImage function instead...

    /*

    private function createImage(e:Event):void
    {
        var file:Loader = Loader(e.target);
        myDynamicImage = new Image();
        myDynamicImage.source = file;
        myDynamicImage.width = 120*getImgAR(file); //you can substitue 120 for your required size
        myDynamicImage.height = 120;//same here
    }

    */
}

}

希望有所帮助