将图像适配到Android屏幕
无法正确执行最简单的任务-将图像调整到屏幕。图片从画廊中加载,这是展示,但尝试了许多方法来理解其中的一些奇怪之处。 我使用的Flex ViewNavigator应用程序没有ActionBar和全屏。横向屏幕方向。最好的选择,以适应最大尺寸的图像。所有图像的大小和比例相等,为3264 x 2448 这样做:将图像适配到Android屏幕,android,actionscript-3,image,apache-flex,air,Android,Actionscript 3,Image,Apache Flex,Air,无法正确执行最简单的任务-将图像调整到屏幕。图片从画廊中加载,这是展示,但尝试了许多方法来理解其中的一些奇怪之处。 我使用的Flex ViewNavigator应用程序没有ActionBar和全屏。横向屏幕方向。最好的选择,以适应最大尺寸的图像。所有图像的大小和比例相等,为3264 x 2448 这样做: protected var roll:CameraRoll; protected var loader:Loader; if(CameraRoll.supportsBrowseForImag
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);
}
这些问题在评论的痕迹。我不明白为什么会发生这种情况。既然您使用的是Flex,您是否尝试过使用Spark Image组件而不是纯actionscript?它具有内置的缩放功能,具有
scaleMode
和fillMode
属性。加载图像后,您可以尝试以下操作:
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"
/>
私有函数onImageLoaded(事件:事件):void{
变量位图:位图=位图(event.currentTarget.content);
imageComponent.source=位图;
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onImageLoaded);
loader.contentLoaderInfo.removeEventListener(ErrorEvent.ERROR,OneError);
}