adobe官方方案,适用于比较老旧的页面加载,主要用到Htmlloader或者stagewebview类,如果要支持现代h5的页面,民间给出两个ane扩展解决方案,一个是https://github.com/RayGyoe/ANEWinWebView(待验证,缺点只支持win,而且需要提前安装Miniblink的轻量浏览器?根据经验在部分air sdk可能有崩溃现象,AIR29正常。AIR30、31多次添加会崩溃。貌似是air sdk问题?),还有一个https://github.com/tuarua/WebViewANE(已验证,代码下面给出,缺点窗口好像不能多开)
上代码:
//优点:支持h5,缺点:不能多开?
//github:https://github.com/tuarua/WebViewANE
import com.tuarua.FreSharp;
import com.tuarua.FreSwift;
import com.tuarua.WebView;
import com.tuarua.webview.Settings;
import flash.desktop.NativeApplication;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.utils.setTimeout;
import flash.events.MouseEvent;
import flash.text.TextField;
var freSharpANE:FreSharp = new FreSharp(); // must create before all others
var freSwiftANE:FreSwift = new FreSwift(); // must create before all others
var webView:WebView;
var hasActivated:Boolean;
//stage.displayState=StageDisplayState.FULL_SCREEN;//全屏显示
//stage.scaleMode=StageScaleMode.SHOW_ALL;//显示所有,不保证比例
gjbtn.addEventListener(MouseEvent.CLICK,doTest);
function doTest(e:MouseEvent):void
{
stage.displayState=StageDisplayState.FULL_SCREEN;//全屏显示
stage.scaleMode=StageScaleMode.SHOW_ALL;//显示所有,不保证比例
//newWindow.maximize();
newWindow.activate();
newWindow.restore();
newWindow.addEventListener(MouseEvent.CLICK,gotClick);
}
function gotClick(e:MouseEvent):void{
trace("click");
}
this.addEventListener(Event.ACTIVATE, onActivated);
/*stage.addEventListener(Event.RESIZE, onResize);
function onResize(e:flash.events.Event):void
{
if (newWindow){
newWindow.bounds = new Rectangle((this.stage.stageWidth-1920)/2, (this.stage.stageHeight-840)/2, 1920, 840);
}
}*/
function onActivated(event:Event):void {
if (hasActivated) return;
setTimeout(init, 0); // this is handle the HARMAN splash screen
hasActivated = true;
}
var newWindow:NativeWindow;
function doDrag(e:MouseEvent):void{
newWindow.startMove();
}
function init():void {
var windowOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
// windowOptions.transparent = true;
//windowOptions.systemChrome = NativeWindowSystemChrome.NONE;
windowOptions.systemChrome = NativeWindowSystemChrome.STANDARD;
windowOptions.type = NativeWindowType.NORMAL;
newWindow = new NativeWindow(windowOptions);
//newWindow.stage.scaleMode = StageScaleMode.NO_SCALE;
//newWindow.stage.align =StageAlign.;// StageAlign.TOP_LEFT;
//newWindow.title = "title name";
newWindow.bounds = new Rectangle((this.stage.stageWidth-1920)/2, (this.stage.stageHeight-840)/2, 1920, 840);
newWindow.stage.color=0x000066;
newWindow.stage.align=StageAlign.TOP_LEFT;
newWindow.activate();
var titlebar:Wintitle = new Wintitle();
titlebar.addEventListener(MouseEvent.MOUSE_DOWN,doDrag);
newWindow.stage.addChild(titlebar);
var x1:Number = 0;//(this.stage.stageWidth-1920)/2;
var y1:Number = 40;//(this.stage.stageHeight-840)/2;
NativeApplication.nativeApplication.addEventListener(Event.EXITING, onExiting);
var viewport:Rectangle = new Rectangle(x1,y1, 1920, 800);
// var settings:Settings = new Settings();
webView = WebView.shared();
webView.init(newWindow.stage, viewport, new URLRequest("http://baidu.com"), null, 1, 0xFFF1F1F1);
webView.visible = true;
newWindow.minimize();
}
function onExiting(event:Event):void
{
WebView.dispose();
FreSwift.dispose();
FreSharp.dispose();
}
//只支持非h5页面
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.LocationChangeEvent;
import flash.geom.Rectangle;
import flash.media.StageWebView;
import flash.ui.Keyboard;
import flash.events.Event;
public class TestStageWebView extends Sprite
{
private var webView:StageWebView = new StageWebView();
public function TestStageWebView()
{
addEventListener(Event.ADDED_TO_STAGE, onStageHandler);
}
private function onStageHandler(e:Event):void
{
webView.stage = this.stage;
webView.viewPort = new Rectangle( 0, 0, stage.stageWidth, stage.stageHeight );
//webView.loadURL( "http://www.baidu.com" );
webView.loadURL( "http://31.15.5.4:9080/#/pageList/pageViewPreview?id=712083a1-0e5a-42c4-bf88-d4c2c79105bd&mode=share" );
webView.addEventListener( Event.COMPLETE, htmlLoaderComplete );
webView.addEventListener( LocationChangeEvent.LOCATION_CHANGING, locationChanging);
}
private function htmlLoaderComplete ( eve : Event ) : void
{
trace( "网页加载完成!" );
}
private function locationChanging ( eve : Event ) : void
{
trace( "网页加载!" );
}
}
}
//只支持非h5页面
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.LocationChangeEvent;
import flash.events.MouseEvent;
import flash.html.HTMLLoader;
import flash.net.URLRequest;
public class TestHtmlLoader extends Sprite
{
private var host:CustomHost;
private var htmlLoader: HTMLLoader;
public function TestHtmlLoader()
{
super();
addEventListener(Event.ADDED_TO_STAGE, onStageHandler);
}
private function onStageHandler(e:Event):void
{
this.stage.addEventListener(Event.RESIZE,onResizeHandler);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
htmlLoader = new HTMLLoader();
htmlLoader2 = new HTMLLoader();
setHTMLLoader();
}
private function onResizeHandler(e:Event):void
{
htmlLoader.width = stage.stageWidth/2;
htmlLoader.height = stage.stageHeight;
htmlLoader2.width = stage.stageWidth/2;
htmlLoader2.height = stage.stageHeight;
htmlLoader2.x = stage.stageWidth/2;
}
private function setHTMLLoader () : void
{
htmlLoader.addEventListener( Event.COMPLETE, htmlLoaderComplete );
htmlLoader.addEventListener( LocationChangeEvent.LOCATION_CHANGING, locationChanging);
htmlLoader.width = 1080;
htmlLoader.height = 670;
htmlLoader.load( new URLRequest("https://fwdt.gzw.sh.cegn.cn/r/w?cmd=com.awspaas.user.apps.datamanager_html&appId=com.awspaas.user.apps.fundplatform&html=proShow_td1.html&sid=ck") );
addChild( htmlLoader );
host = new CustomHost();
htmlLoader.htmlHost = host;
htmlLoader2.x = 1920;
htmlLoader2.width = 1920;
htmlLoader2.height = 670;
htmlLoader2.load( new URLRequest("http://www.bing.com/") );
addChild(htmlLoader2);
//host2 = new CustomHost();
//htmlLoader2.htmlHost = host;
}
private function htmlLoaderComplete ( eve : Event ) : void
{
trace( "网页加载完成!" );
}
private function locationChanging ( eve : Event ) : void
{
trace( "网页加载!" );
}
private var htmlLoader2: HTMLLoader;
}
}
//只支持非h5页面
package
{
import flash.html.HTMLHost;
import flash.html.HTMLLoader;
import flash.display.NativeWindow;
import flash.display.NativeWindowInitOptions;
import flash.display.StageScaleMode;
import flash.geom.Rectangle;
import flash.text.TextField;
public class CustomHost extends HTMLHost
{
import flash.html.*;
public var statusField:TextField;
public function CustomHost(defaultBehaviors:Boolean=true)
{
super(defaultBehaviors);
}
override public function windowClose():void
{
// htmlLoader.stage.window.close();
}
override public function createWindow(windowCreateOptions:HTMLWindowCreateOptions):HTMLLoader
{
var initOptions:NativeWindowInitOptions = new NativeWindowInitOptions();
var window:NativeWindow = new NativeWindow(initOptions);
window.visible = true;
var htmlLoader2:HTMLLoader = new HTMLLoader();
htmlLoader2.width = window.width;
htmlLoader2.height = window.height;
window.stage.scaleMode = StageScaleMode.NO_SCALE;
window.stage.addChild(htmlLoader2);
return htmlLoader2;
}
override public function updateLocation(locationURL:String):void
{
trace(locationURL);
}
override public function set windowRect(value:Rectangle):void
{
htmlLoader.stage.nativeWindow.bounds = value;
}
override public function updateStatus(status:String):void
{
statusField.text = status;
}
override public function updateTitle(title:String):void
{
htmlLoader.stage.nativeWindow.title = title + "- Example Application";
}
override public function windowBlur():void
{
htmlLoader.alpha = 0.5;
}
override public function windowFocus():void
{
htmlLoader.alpha = 1;
}
}
}