hi,
The Image claas require a texture to create.But I want a lazy loading image.Then I do some changes to let it work. Follow is the changing, and I want to know a better way to implement;
Quad.as
public function Quad(width:Number=NaN, height:Number=NaN, color:uint=0xffffff, premultipliedAlpha:Boolean=true) { if(!isNaN(width) && !isNaN(height)) { init(width, height, color, premultipliedAlpha); } } private var _inited:Boolean = false; public function get inited():Boolean { return _inited; } protected function init(width:Number, height:Number, color:uint=0xffffff, premultipliedAlpha:Boolean=true):void { mTinted = color != 0xffffff; mVertexData = new VertexData(4, premultipliedAlpha); mVertexData.setPosition(0, 0.0, 0.0); mVertexData.setPosition(1, width, 0.0); mVertexData.setPosition(2, 0.0, height); mVertexData.setPosition(3, width, height); mVertexData.setUniformColor(color); onVertexDataChanged(); _inited = true; } override public function hitTest(localPoint:Point, forTouch:Boolean=false):DisplayObject { if( !inited ) return null; return super.hitTest(localPoint, forTouch); } public override function render(support:RenderSupport, parentAlpha:Number):void { if(!inited) return ; support.batchQuad(this, parentAlpha); }
Image.as
public function Image( texture:Texture=null ) { super(); if ( texture ) { this.texture = texture; } } public function set texture( value:Texture ):void { if ( value == null ) { throw new ArgumentError( "Texture cannot be null" ); } else { if ( !mTexture ) { var frame:Rectangle = value.frame; var width:Number = frame ? frame.width : value.width; var height:Number = frame ? frame.height : value.height; var pma:Boolean = value.premultipliedAlpha; init( width, height, 0xffffff, pma ); mVertexData.setTexCoords( 0, 0.0, 0.0 ); mVertexData.setTexCoords( 1, 1.0, 0.0 ); mVertexData.setTexCoords( 2, 0.0, 1.0 ); mVertexData.setTexCoords( 3, 1.0, 1.0 ); mTexture = value; mSmoothing = TextureSmoothing.BILINEAR; mVertexDataCache = new VertexData( 4, pma ); mVertexDataCacheInvalid = true; } else if ( value != mTexture ) { mTexture = value; mVertexData.setPremultipliedAlpha( mTexture.premultipliedAlpha ); onVertexDataChanged(); } } } public override function render( support:RenderSupport, parentAlpha:Number ):void { if(!inited) return ; support.batchQuad( this, parentAlpha, mTexture, mSmoothing ); }
MyImage.as
public class MyImage extends Image { /** * * @param src url/Bitmap/BitmapData/ByteArray */ public function MyImage(src:*) { super(); if( src ) { source = src; } } private var _source:String; private var _loader:ImageLoader; /** * @param val url/Bitmap/BitmapData/ByteArray */ public function set source( val:* ):void { if ( val is String ) { if( _source==val ) { return ; } _source = val; if ( !_loader ) { _loader = new ImageLoader(); _loader.addEventListener( LoaderEvent.ALL_COMPLETED, img_OnLoaded ); } // _loader.IsAnimation = IsAnimation; _loader.Load( val ); } else { _source = null; if ( val is BitmapData ) { texture = Texture.fromBitmapData( val ); } else if ( val is Bitmap ) { texture = Texture.fromBitmap( val ); } else if( val is ByteArray ) { texture = Texture.fromAtfData( val ); } } } private function img_OnLoaded( e:LoaderEvent ):void { source = e.bitmapData; } }
I can use MyImage like this:
var img:MyImage = new MyImage("img/myico.png"); img.x = 100; addChild( img );
I changed 2 classes of starling framework. I know that is not a good idea.
And the lazy loading is necessarily.
Somebody give me a suggest, pls.
Thanks!