Question

I currently store embedded images in a flex library project (swc) like so:

public class BitmapAssets
{
    [Embed(source="/assets/icon_160.png")]
    public static const icon_160:Class;
    [Embed(source="/assets/icon_240.png")]
    public static const icon_240:Class;
    [Embed(source="/assets/icon_240.png")]
    public static const icon_240:Class;
}

And then i reference the images in other projects. For example:

<s:Button>
    <s:icon>
        <s:MultiDPIBitmapSource source160dpi="{BitmapAssets.icon160}"
                                source240dpi="{BitmapAssets.icon240}"
                                source320dpi="{BitmapAssets.icon320}" />
    </s:icon>
</s:Button>

What i would like to do is embed the MultiDPIBitmapSource in the library project; then i could reduce the code to this:

<s:Button icon="{MultiDPIBitmapAssets.icon}" />

However, i can't figure out how to embed a MultiDPIBitmapSource object with source160/240/320 values filled.

Any solutions gratefully received ;)

Edit:

As an alternative to 'true' embedding, i'm wondering whether mxml compiling could provide an answer. For instance, if i have an mxml declaration:

<fx:declarations>
    <s:MultiDPIBitmapSource id="icon"
                            source160dpi="{BitmapAssets.icon160}"
                            source240dpi="{BitmapAssets.icon240}"
                            source320dpi="{BitmapAssets.icon320}" />
</fx:declarations>

Then the mxml compiler will turn it into:

 var icon:MultiDPIBitmapSource = new MultiDPIBitmapSource();
 icon.source160dpi = BitmapAssets.icon160;
 icon.source240dpi = BitmapAssets.icon240;
 icon.source320dpi = BitmapAssets.icon320;

And i can then reference it as i'd like:

<s:Button icon="{MultiDPIBitmapAssets.icon}" />

Technically, its not embedded. But for practical purposes, it is (at least in my mind). The question is, where do i put the declarations tag? Can i define a non-visual class in mxml?

Was it helpful?

Solution

MultiDPIBitmapSource is an object created at runtime, not at compile-time. You won't be able to embed an instance of it in the app.

However, you could create a static reference to an object. It would require a bit more code, but it would be less code to write every time you need to use it.

public class Assets {
    private static var _asset:MultiDPIBitmapSource;

    public static function get asset():MultiDPIBitmapSource {
        if ( !_assets ) {
            _assets = new MultiDPIBitmapSource();
            _assets.source160 = "url";
            _assets.source240 = "url";
            _assets.source320 = "url";
            _assets.source480 = "url";
        }
        return _assets;
    }

}

And then to use it:

<s:Button icon="{Assets.asset}"/>

So it basically treats the source as a mini-Singleton. I'm personally not a fan of this method. The only gain you get is slightly less code in each class, but you lose flexibility and it goes against general OOP practices. But it should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top