开发者

What is the best way to implement the functionalities of BitmapImage without inheriting?

开发者 https://www.devze.com 2023-02-21 19:14 出处:网络
this.container.addElement(myCustomImageInstance as GraphicElement) must display the test image. package com.www

this.container.addElement(myCustomImageInstance as GraphicElement) must display the test image.

package com.www
{
    import spark.primitives.BitmapImage;
    import spark.primitives.supportClasses.GraphicElement;

    public class BaseTest extends GraphicElement
    {       
        public function BaseTest()
        {
            super();
        }

        /* stuff */
    }

    public class MyCustomImage extends BaseTest
    {
        private var _img:BitmapImage = null;

        public function MyCustomImage()
        {
            super();
        }

        public function get img():BitmapImage {
            if (!_img) {
                _img = new BitmapImage();
                _img.source = /* BitmapData of a test image */;
            }
            return _img;
        }

        /* It obviously does not work, any ideas?
        override public function createDisplayObject():DisplayObject {
            return img.displayObject;
        }

        override public function get displayObject():DisplayObject {
            return img.displayObject;
        }
        */

    }

}

Thank yo开发者_开发问答u very much


Unlike mx:Image, the spark BitmapImage class does not include member functions for loading bitmap data. Hence the source property you are trying to push your URL into is only intended to take actual bitmap data (something either returned from an SWFLoader or Loader, or an embedded image file). Please see the AS3 Documentation: s:BitmapImage#source

Although you can technically place a URL in the embed directive (eg. @Embed("http://...image.jpg")), I assume since you are using ActionScript instead of MXML, you intend to dynamically load your images.

In that case, you'll either need to use mx.controls.Image, or use a separate mx.controls.SWFLoader or flash.display.Loader to load the image that you need. You should note that the loading process is asynchronous.

You might find something like the following tutorial helpful, which shows how to use flash.display.Loader, together with s:BitmapImage, to function like mx:Image: http://polygeek.com/2452_flex_extending-spark-bitmapimage

0

精彩评论

暂无评论...
验证码 换一张
取 消