我正在用Flex 4.6和AIR构建一个应用程序。它将在Android和iPhone上发布,但目前我正在Android设备上进行测试。
我已经有了基本的流程工作与不同的屏幕,甚至一个flash组件。
但是,在屏幕之间切换时,即使部署到实际设备上,我的所有s:Image对象也需要很长时间才能加载。我说的长时间是指大约半秒。这通常不会太糟糕,但屏幕上的文字立即显示,而所有的图像保持白色半秒,然后他们加载。
这只是一个AIR/Flex的东西吗?还有其他人遇到过这个问题吗,有解决方案吗?
如果你将图片嵌入到Flex应用程序中,它们的加载速度会更快。这背后的代码看起来像这样:
[Embed(source="logo.gif")]
[Bindable]
public var imgCls:Class;
然后你可以像这样使用这个类:
<s:Image id="myImageRaw" source="{imgCls}"/>
[从文档中复制的代码]
如果你需要多次使用相同的图像,你应该考虑使用BitMapImage类;并克隆第一张图像的bitMapData。这里是一个快速的实用程序类,我"借用和修改"从精灵中获取BitMapData,反之亦然。[Flex Image类类扩展了sprite,所以你应该能够发送一个图像作为输入]
package com.natejc.utils.display
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;
// **********************************************************************************
// **********************************************************************************
// borrowed from http://www.natejc.com/source/com/natejc/utils/display/DisplayConverter.as
/**
* Provides convenience conversion methods for Sprites and Bitmaps.
*
* Open source. Free to use. Licensed under the MIT License.
*
* @author Nate Chatellier
* @see http://blog.natejc.com
*/
public class DisplayConverter
{
// **********************************************************************************
/**
* Constructs the DisplayConverter object.
*/
public function DisplayConverter()
{
trace("DisplayConverter is a static class and should not be instantiated");
} // END CONSTRUCTOR
// **********************************************************************************
/**
* Converts a Bitmap to a Sprite.
*
* @param bitmap The Bitmap that should be converted.
* @param smoothing Whether or not the bitmap is smoothed when scaled.
* @return The converted Sprite object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Bitmap.html#smoothing
*/
public static function bitmapToSprite(bitmap:Bitmap, smoothing:Boolean = false):Sprite
{
var sprite:Sprite = new Sprite();
sprite.addChild( new Bitmap(bitmap.bitmapData.clone(), "auto", smoothing) );
return sprite;
} // END FUNCTION bitmapToSprite
// **********************************************************************************
/**
* Converts a Sprite to a Bitmap.
*
* @param sprite The Sprite that should be converted.
* @param smoothing Whether or not the bitmap is smoothed when scaled.
* @return The converted Bitmap object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#draw()
*/
public static function spriteToBitmap(sprite:Sprite, smoothing:Boolean = false):Bitmap
{
var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);
bitmapData.draw(sprite);
return new Bitmap(bitmapData, "auto", smoothing);
} // END FUNCTION spriteToBitmap
/**
* JH DotComIT added 11/19/2011
* Converts a Sprite to a BitmapData.
*
* @param sprite The Sprite that should be converted.
* @return The converted Bitmap object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#draw()
*/
public static function spriteToBitmapData(sprite:Sprite):BitmapData
{
var bitmapData:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x00FFFFFF);
bitmapData.draw(sprite);
return bitmapData;
} // END FUNCTION spriteToBitmapData
/**
* Converts BitmapData to a Sprite.
*
* @param bitmap The Bitmap that should be converted.
* @param smoothing Whether or not the bitmap is smoothed when scaled.
* @return The converted Sprite object.
*
* @see http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Bitmap.html#smoothing
*/
public static function bitmapDataToSprite(bitmapData:BitmapData, smoothing:Boolean = false):Sprite
{
var sprite:Sprite = new Sprite();
sprite.addChild( new Bitmap(bitmapData.clone(), "auto", smoothing) );
return sprite;
} // END FUNCTION bitmapToSprite
// **********************************************************************************
// **********************************************************************************
} // END CLASS DisplayConverter
} // END PACKAGE
一旦你有了BitMapData,你可以调用clone来获得它的副本,并创建同一图像的多个实例。[做一些关于比特的研究;这是游戏开发者所使用的技术。Spark Image标签也将接受BitMapData作为源。