`
xchangei
  • 浏览: 2780 次
社区版块
存档分类
最新评论

Flex 4 舞台尺寸随浏览器窗口大小改变

    博客分类:
  • flex
阅读更多
首先创建项目修改启动html文件 (html-template 文件夹里的 index.template.html 文件)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
    <!-- 
    Smart developers always View Source. 
    
    This application was built using Adobe Flex, an open source framework
    for building rich Internet applications that get delivered via the
    Flash Player or to desktops via Adobe AIR. 
    
    Learn more about Flex at http://flex.org
    // -->
    <head>
        <title>${title}</title>
        <meta name="google" value="notranslate">         
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <!-- Include CSS to eliminate any default margins/padding and set the height of the html element and 
       the body element to 100%, because Firefox, or any Gecko based browser, interprets percentage as 
    the percentage of the height of its parent container, which has to be set explicitly.  Fix for
    Firefox 3.6 focus border issues.  Initially, don't display flashContent div so it won't show 
    if JavaScript disabled.
  -->
        <style type="text/css" media="screen"> 
   html, body { height:100%; width:100%; }
   body { margin:0; padding:0; overflow:auto; text-align:center; 
          background-color: ${bgcolor}; }   
   object:focus { outline:none; }
   #flashContent { display:none; }
        </style>
  
  <!-- Enable Browser History by replacing useBrowserHistory tokens with two hyphens -->
        <!-- BEGIN Browser History required section ${useBrowserHistory}>
        <link rel="stylesheet" type="text/css" href="history/history.css" />
        <script type="text/javascript" src="history/history.js"></script>
        <!${useBrowserHistory} END Browser History required section -->  
      
        <script type="text/javascript" src="swfobject.js"></script>
        <script type="text/javascript">
         
         <!-- scwidth or scheight -->
         var winWidth = 0;   
   var winHeight = 0; 
   function findDimensions() {   
    //获取窗口宽度  
    if (window.innerWidth) {
     winWidth = window.innerWidth;
    } 
    else if ((document.body) && (document.body.clientWidth)) {
     winWidth = document.body.clientWidth; //获取窗口高度  
    }
    
    if (window.innerHeight)
    { 
     winHeight = window.innerHeight;  
    }
    else if ((document.body) && (document.body.clientHeight)){
     winHeight = document.body.clientHeight;   
    }
    //通过深入Document内部对body进行检测,获取窗口大小  
    if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth)
    {  
    winHeight = document.documentElement.clientHeight;  
    winWidth = document.documentElement.clientWidth;  
    }
    if(isReady()){
      var str = winWidth+","+winHeight;
      sendToActionScript(str);
     }   
   }  
   window.onresize=findDimensions;
   var jsReady = false;
   function isReady() {
      return jsReady;
   }
   function pageInit() {
      jsReady = true;
      //调用函数,获取数值  
     findDimensions();  
   }
   function thisMovie(movieName) {
      if (navigator.appName.indexOf("Microsoft") != -1) {
          return window[movieName];
      } else {
          return document[movieName];
      }
   }
   function sendToActionScript(value) {
      thisMovie("${application}").sendToActionScript(value);
   }
   function sendToJavaScript(value) {
     findDimensions();
   }
         <!---------------------------------------------- -->
            <!-- For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. --> 
            var swfVersionStr = "${version_major}.${version_minor}.${version_revision}";
            <!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
            var xiSwfUrlStr = "${expressInstallSwf}";
            var flashvars = {};
            var params = {};
            params.quality = "high";
            params.bgcolor = "${bgcolor}";
            params.allowscriptaccess = "sameDomain";
            params.allowfullscreen = "true";
            var attributes = {};
            attributes.id = "${application}";
            attributes.name = "${application}";
            attributes.align = "middle";
            swfobject.embedSWF(
                "${swf}.swf", "flashContent", 
                "100%", "100%", 
                swfVersionStr, xiSwfUrlStr, 
                flashvars, params, attributes);
   <!-- JavaScript enabled so display the flashContent div in case it is not replaced with a swf object. -->
   swfobject.createCSS("#flashContent", "display:block;text-align:left;");
        </script>
    </head>
    <body scroll="no" onload="pageInit();">
        <!-- SWFObject's dynamic embed method replaces this alternative HTML content with Flash content when enough 
    JavaScript and Flash plug-in support is available. The div is initially hidden so that it doesn't show
    when JavaScript is disabled.
  -->
        <div id="flashContent">
         <p>
          To view this page ensure that Adobe Flash Player version 
    ${version_major}.${version_minor}.${version_revision} or greater is installed. 
   </p>
   <script type="text/javascript"> 
    var pageHost = ((document.location.protocol == "https:") ? "https://" : "http://"); 
    document.write("<a href='http://www.adobe.com/go/getflashplayer'><img src='" 
        + pageHost + "www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>" ); 
   </script> 
        </div>
     
        <noscript>
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="100%" height="100%" id="${application}">
                <param name="movie" value="${swf}.swf" />
                <param name="quality" value="high" />
                <param name="bgcolor" value="${bgcolor}" />
                <param name="allowScriptAccess" value="sameDomain" />
                <param name="allowFullScreen" value="true" />
                <!--[if !IE]>-->
                <object type="application/x-shockwave-flash" data="${swf}.swf" width="100%" height="100%">
                    <param name="quality" value="high" />
                    <param name="bgcolor" value="${bgcolor}" />
                    <param name="allowScriptAccess" value="sameDomain" />
                    <param name="allowFullScreen" value="true" />
                <!--<![endif]-->
                <!--[if gte IE 6]>-->
                 <p> 
                  Either scripts and active content are not permitted to run or Adobe Flash Player version
                  ${version_major}.${version_minor}.${version_revision} or greater is not installed.
                 </p>
                <!--<![endif]-->
                    <a href="http://www.adobe.com/go/getflashplayer">
                        <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash Player" />
                    </a>
                <!--[if !IE]>-->
                </object>
                <!--<![endif]-->
            </object>
     </noscript>  
   </body>
</html>

增加的 代码是 (用来获取浏览器的尺寸 和 发送数据到 flex)
<!-- scwidth or scheight -->
         var winWidth = 0;   
   var winHeight = 0; 
   function findDimensions() {   
    //获取窗口宽度  
    if (window.innerWidth) {
     winWidth = window.innerWidth;
    } 
    else if ((document.body) && (document.body.clientWidth)) {
     winWidth = document.body.clientWidth; //获取窗口高度  
    }
    
    if (window.innerHeight)
    { 
     winHeight = window.innerHeight;  
    }
    else if ((document.body) && (document.body.clientHeight)){
     winHeight = document.body.clientHeight;   
    }
    //通过深入Document内部对body进行检测,获取窗口大小  
    if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth)
    {  
    winHeight = document.documentElement.clientHeight;  
    winWidth = document.documentElement.clientWidth;  
    }
    if(isReady()){
      var str = winWidth+","+winHeight;
      sendToActionScript(str);
     }   
   }  
   window.onresize=findDimensions;
   var jsReady = false;
   function isReady() {
      return jsReady;
   }
   function pageInit() {
      jsReady = true;
      //调用函数,获取数值  
     findDimensions();  
   }
   function thisMovie(movieName) {
      if (navigator.appName.indexOf("Microsoft") != -1) {
          return window[movieName];
      } else {
          return document[movieName];
      }
   }
   function sendToActionScript(value) {
      thisMovie("${application}").sendToActionScript(value);
   }
   function sendToJavaScript(value) {
     findDimensions();
   }
         


修改 样式 增加宽度100%设定  html, body { height:100%; width:100%; }

修改body 标签 监听加载完成事件  <body scroll="no" onload="pageInit();">
之后的宽度高度 都改成 100% 就好了

在看 flex 代码 如下
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
      preloader="Preloader" 
      xmlns:sRGroup="sRGroup.*"
      creationComplete="application1_creationCompleteHandler(event)"
      addedToStage="application1_addedToStageHandler(event)">
 <fx:Declarations>
  <!-- 将非可视元素(例如服务、值对象)放在此处 -->
 </fx:Declarations>
 <fx:Script>
  <![CDATA[
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   import mx.events.ResizeEvent;
   
   import org.puremvc.as3.patterns.facade.Facade;
   
   import spark.components.mediaClasses.VolumeBar;
   
   public static var main:ShowerRoom;
   protected function application1_creationCompleteHandler(event:FlexEvent):void
   {
    if (ExternalInterface.available) {
     try {
      ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
      if (checkJavaScriptReady()) {
       ExternalInterface.call("sendToJavaScript","getWH");
      } else {
       var readyTimer:Timer = new Timer(100, 0);
       readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
       readyTimer.start();
      }
     } catch (error:SecurityError) {
      Alert.show("A SecurityError occurred: " + error.message);
     } catch (error:Error) {
      Alert.show("An Error occurred: " + error.message);
     }
    }
    //注册
    Facade.getInstance().registerCommand(ShowerRoomNotify.CMD_SHOWERROOM,ShowerRoomCommand);
    Facade.getInstance().sendNotification(ShowerRoomNotify.CMD_SHOWERROOM);
    main = this;
   }
   /**网页js 发来的数据接收函数*/
   private function receivedFromJavaScript(value:String):void {
    var array:Array = value.split(",");
    this.width = array[0];
    this.height = array[1];
//    Alert.show("舞台尺寸改变"+this.width+"------"+this.height);
   }
   /**检测网页是否加载完毕*/
   private function checkJavaScriptReady():Boolean {
    var isReady:Boolean = ExternalInterface.call("isReady");
    return isReady;
   }
   /**循环检测网页是否加载完毕*/
   private function timerHandler(event:TimerEvent):void {
    var isReady:Boolean = checkJavaScriptReady();
    if (isReady) {
     //停止timer
     var str:String = ExternalInterface.call("sendToJavaScript","getWH");
     Alert.show(str);
     Timer(event.target).stop();
    }
   }

   protected function application1_addedToStageHandler(event:Event):void
   {
    //添加舞台尺寸改变数据
    this.addEventListener(ResizeEvent.RESIZE,onResize);
   }
   
   private function onResize(evt:Event):void
   {
    //Alert.show("舞台尺寸改变"+stage.width+"------"+stage.height);
   }

  ]]>
 </fx:Script>
 <sRGroup:MenuListGroup x="38" y="70">
 </sRGroup:MenuListGroup>
 <s:Button x="0" y="0" label="按钮"/>
 <s:Button right="0" bottom="0" label="按钮" id="btn2"/>
</s:Application>



flex 的就不说了 那几个和js通讯的函数 和 application1_creationCompleteHandler 方法里 对通讯的启动

好了 可以启动浏览起 拖拽浏览器改变窗口大小 看看 按钮会不会自适应尺寸了

做个tag保留
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics