2012-01-22 124 views
0

無法正確執行最簡單的任務 - 使圖像適合屏幕。圖片從畫廊加載,它是顯示,但嘗試了很多方法來了解有些古怪。 我使用Flex ViewNavigatorApplication,沒有ActionBar和全屏。橫向屏幕方向。適應圖像最大尺寸的最佳選擇。所有圖像都爲3264 X 2448。使圖像適合Android屏幕

大小相同的比例這樣做:

protected var roll:CameraRoll; 
protected var loader:Loader; 

if(CameraRoll.supportsBrowseForImage && !roll) 
{ 
roll = new CameraRoll(); 
roll.addEventListener(MediaEvent.SELECT, onSelect); 
} 

if(roll){ 
roll.browseForImage(); 
} 

protected function onSelect(e:MediaEvent) : void 
{ 
var data:MediaPromise = e.data; 
var promise:MediaPromise = e.data as MediaPromise; 
loader = new Loader(); 
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded); 
loader.contentLoaderInfo.addEventListener(ErrorEvent.ERROR, onError); 
loader.loadFilePromise(promise); 
} 
} 
private function onImageLoaded(event:Event):void { 

var bitmap:Bitmap = Bitmap(event.currentTarget.content); 
var ratio:Number = bitmap.width/bitmap.height 
trace (bitmap.width + '/' + bitmap.height + ' ratio: ' + ratio);//3264/2448 ratio: 1.3333333333333333 
bitmap.width = Capabilities.screenResolutionY; 
trace ('screenResolutionX: ' + Capabilities.screenResolutionY);//screenResolutionX: 800 
bitmap.height = bitmap.width/ratio; 
trace (bitmap.width + '/' + bitmap.height + ' ratio: ' + bitmap.width/bitmap.height);//799.9000000000001/599.95 ratio: 1.3332777731477623 
spr.addChild(bitmap); //container 
trace (spr.width + '/' + spr.height);//0/0 
trace (this.width + '/' + this.height);//533/320 - here is the first - why these data? 
trace (stage.width + '/' + stage.height);//1202.8500000000001/918.6500000000001 - 
//here is the second - why these data? 
//On the real phone in debug mode i see that image have about this dimensions, but trace above we see that the dimensions of 800 by 600. 
trace (stage.stageWidth + '/' + stage.stageHeight);//800/480 - all OK 
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onImageLoaded); 
loader.contentLoaderInfo.removeEventListener(ErrorEvent.ERROR, onError); 
} 

在痕跡的意見的問題。我不明白爲什麼會發生這種情況。

回答

1

由於您使用的是Flex,您是否嘗試過使用Spark Image組件而不是純動作?它具有與scaleModefillMode屬性的內置比例。一旦您的圖片加載完畢,您可能會嘗試這樣:

private function onImageLoaded(event:Event):void { 

    var bitmap:Bitmap = Bitmap(event.currentTarget.content); 
    imageComponent.source = bitmap; 
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onImageLoaded); 
    loader.contentLoaderInfo.removeEventListener(ErrorEvent.ERROR, onError); 
} 

<s:Image 
    id="imageComponent" 
    fillMode="scale" 
    scaleMode="stretch" 
/>