写在开头:
本文主要资料参考来自mark的《Flexpaper二次开发入门教程》,在此鸣谢!该教程写得还是很详细,初次接触flexpaper的可以先看看该教程。
下面主要谈谈二次开发flexpaper的过程中的一些总结。
我的项目名称为Diyflexpaper,修改Diyflexpaper.mxml如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:fp="com.devaldi.controls.flexpaper.*"
layout="absolute" width="100%" height="100%"
applicationComplete="initApp();">
<mx:Metadata>
[ResourceBundle("FlexPaper")]
[Event(name="onDocumentLoaded", type="com.devaldi.events.DocumentLoadedEvent")]
[Event(name="onLoadingProgress", type="flash.events.ProgressEvent")]
[Event(name="onDocumentLoadedError", type="flash.events.ErrorEvent")]
[Event(name="onCurrPageChanged", type="com.devaldi.events.CurrentPageChangedEvent")]
[Event(name="onDocumentLoading", type="flash.events.Event")]
[Event(name="onExternalLinkClicked", type="com.devaldi.events.ExternalLinkClickedEvent")]
[Event(name="onDocumentPrinted", type="com.devaldi.events.DocumentPrintedEvent")]
[Event(name="onPageLoaded", type="com.devaldi.events.PageLoadedEvent")]
[Event(name="onPageLoading", type="com.devaldi.events.PageLoadingEvent")]
[Event(name="onErrorLoadingPage", type="com.devaldi.events.ErrorLoadingPageEvent")]
</mx:Metadata>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import com.devaldi.events.CurrentPageChangedEvent;
import com.devaldi.events.CursorModeChangedEvent;
import com.devaldi.events.DocumentLoadedEvent;
import com.devaldi.events.DocumentPrintedEvent;
import com.devaldi.events.ErrorLoadingPageEvent;
import com.devaldi.events.ExternalLinkClickedEvent;
import com.devaldi.events.FitModeChangedEvent;
import com.devaldi.events.ScaleChangedEvent;
import com.devaldi.events.ViewModeChangedEvent;
public var _aid:Number = 0;//文档ID
[Bindable]
public var _Scale:Number = 1;//缩放比例
[Bindable]
public var _EncodeURI:Boolean = true;
[Bindable]
public var _SwfFile:String = "";//SWF文件路径
[Bindable]
public var _ZoomTransition:String = "easeOut";
[Bindable]
public var _ZoomTime:Number = 0.6;
[Bindable]
public var _ZoomInterval:Number = 0.1;
[Bindable]
public var _MinZoomSize:Number = 0.3;
[Bindable]
public var _MaxZoomSize:Number = 5;
[Bindable]
public var _FitPageOnLoad:Boolean = false;//加载后适合高度
[Bindable]
public var _FitWidthOnLoad:Boolean = false;//加载后适合宽度
[Bindable]
public var _PrintEnabled:Boolean = true;//是否支持打印
[Bindable]
public var _FullScreenAsMaxWindow:Boolean = false;//是否支付全屏
[Bindable]
public var _ProgressiveLoading:Boolean = false;//是否延迟加载
[Bindable]
public var _localeChain:String = "zh_CN";//语言
private var isFocus:Boolean = false;
//------------------------
[Bindable]
public var _SearchMatchAll:Boolean = true;
[Bindable]
public var _PrintPaperAsBitmap:Boolean = false;
[Bindable]
public var _ViewModeToolsVisible:Boolean = true;
[Bindable]
public var _ZoomToolsVisible:Boolean = true;
[Bindable]
public var _NavToolsVisible:Boolean = true;
[Bindable]
public var _CursorToolsVisible:Boolean = true;
[Bindable]
public var _SearchToolsVisible:Boolean = true;
//初始化参数
private function initApp():void{
var params:Object = Application.application.parameters;
_Scale = getNumber(params, "Scale", 1);
_SwfFile = getString(params, "SwfFile", "Paper.swf");
_EncodeURI = getNumber(params, "EncodeURI", 1);
_ZoomTransition = getString(params, "ZoomTransition", "easeOut");
_ZoomTime = getNumber(params, "ZoomTime", 0.6);
_MinZoomSize = getNumber(params, "MinZoomSize", 0.2);
_MaxZoomSize = getNumber(params, "MaxZoomSize", 5);
_ZoomInterval = getNumber(params, "ZoomInterval", 0.1);
_FitPageOnLoad = getBoolean(params, "FitPageOnLoad", false);
_FitWidthOnLoad = getBoolean(params, "FitWidthOnLoad", false);
_PrintEnabled = getBoolean(params, "PrintEnabled", true);
_FullScreenAsMaxWindow = getBoolean(params, "FullScreenAsMaxWindow", false);
_ProgressiveLoading = getBoolean(params, "ProgressiveLoading", true);
_localeChain = params["localeChain"];
_SearchMatchAll=getBoolean(params, "SearchMatchAll", true);
_PrintPaperAsBitmap=getBoolean(params, "PrintPaperAsBitmap", false);
_ViewModeToolsVisible=getBoolean(params, "ViewModeToolsVisible", true);
_ZoomToolsVisible=getBoolean(params, "ZoomToolsVisible", true);
_NavToolsVisible=getBoolean(params, "NavToolsVisible", true);
_CursorToolsVisible=getBoolean(params, "CursorToolsVisible", true);
_SearchToolsVisible=getBoolean(params, "SearchToolsVisible", true);
//注册事件监听
this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
this.addEventListener(MouseEvent.CLICK,setFocusTo);
this.addEventListener("onPageLoaded",pageLoadedEventHandler);
//开放给外部(javascript)调用
ExternalInterface.addCallback("hasFocus", hasFocus);
ExternalInterface.addCallback("setViewerFocus", setViewerFocus);
ExternalInterface.addCallback("gotoPage", gotoPage);
ExternalInterface.addCallback("fitWidth", fitWidth);
ExternalInterface.addCallback("fitHeight", fitHeight);
ExternalInterface.addCallback("loadSwf", loadSwf);
ExternalInterface.addCallback("printPaperRange", printPaperRange);
ExternalInterface.addCallback("searchText", searchText);
ExternalInterface.addCallback("prevSearchMatch", prevSearchMatch);
ExternalInterface.addCallback("nextSearchMatch", nextSearchMatch);
ExternalInterface.addCallback("switchMode", switchMode);
ExternalInterface.addCallback("Zoom", Zoom);
ExternalInterface.addCallback("nextPage", nextPage);
ExternalInterface.addCallback("getCurrPage", getCurrPage);
ExternalInterface.addCallback("prevPage", prevPage);
ExternalInterface.addCallback("printPaper", printPaper);
ExternalInterface.addCallback("getTotalPages", getTotalPages);
}
//---------------------------事件------------------------------
protected function onLoadedError(event:ErrorEvent):void{
dispatchEvent(event);
}
private function setFocusTo(e:MouseEvent):void{
stage.stageFocusRect = false;
stage.focus = e.target as InteractiveObject;
}
private function onMouseOver(event:MouseEvent):void{
this.isFocus = true;
}
private function onMouseOut(event:MouseEvent):void{
this.isFocus = false;
}
protected function onCurrPageChanged(event:CurrentPageChangedEvent):void{
dispatchEvent(event);
}
protected function documentLoadedErrorHandler(event:Event):void{
dispatchEvent(event);
}
protected function onExternalLinkClickedHandler(event:ExternalLinkClickedEvent):void{
dispatchEvent(event);
}
protected function onDocumentLoadingProgressHandler(event:ProgressEvent):void{
dispatchEvent(event);
}
protected function papersLoadingHandler(event:Event):void{
dispatchEvent(event);
}
protected function pageLoadingEventHandler(event:Event):void{
dispatchEvent(event);
}
protected function documentPrintedHandler(event:DocumentPrintedEvent):void{
dispatchEvent(event);
}
protected function documentLoadedHandler(event:Event):void{
dispatchEvent(event);
}
protected function pageLoadedEventHandler(event:Event):void{
dispatchEvent(event);
}
protected function errorLoadingPageEventHandler(event:ErrorLoadingPageEvent):void{
dispatchEvent(event);
}
//----------------------------函数-----------------------------
public function getTotalPages():Number{
return FlexPaper.getTotalPages();
}
public function printPaper(e:Event):void{
FlexPaper.printPaper(e);
}
public function printPaperRange(range:String):void{
FlexPaper.printPaperRange(range);
}
public function searchText(text:String):void{
FlexPaper.searchText(text);
}
public function prevSearchMatch():void{
FlexPaper.prevSearchMatch();
}
public function nextSearchMatch(text:String):void{
FlexPaper.nextSearchMatch(text);
}
public function switchMode(mode:String=null):void{
FlexPaper.switchMode(mode);
}
public function Zoom(factor:Number):void{
FlexPaper.Zoom(factor);
}
public function nextPage():void{
FlexPaper.nextPage();
}
public function prevPage():void{
return FlexPaper.prevPage();
}
public function getCurrPage():Number{
return FlexPaper.getCurrPage();
}
public dynamic function loadSwf(s:String):void{
FlexPaper.SwfFile = s;
}
public function fitHeight():void{
FlexPaper.fitHeight();
}
public function fitWidth():void{
FlexPaper.fitWidth();
}
public function gotoPage(p:Number):void{
FlexPaper.gotoPage(p);
}
public function hasFocus():Boolean{
return isFocus;
}
public function setViewerFocus(isFocus:Boolean):void{
this.FlexPaper.setViewerFocus();
}
/**
*
* 获取String类型参数
* 如果没有,则返回默认值
**/
private function getString(params:Object, name:String, def:String):String{
if(params[name] != null){
return params[name];
}
return def;
}
private function getNumber(params:Object, name:String, def:Number):Number{
if(params[name] != null){
return params[name];
}
return def;
}
private function getBoolean(params:Object, name:String, def:Boolean):Boolean{
if(params[name] != null){
return params[name] == "true";
}
return def;
}
]]>
</mx:Script>
<fp:FlexPaperViewer id="FlexPaper"
width="100%"
height="100%"
onDocumentLoaded="documentLoadedHandler(event)"
onDocumentLoading="papersLoadingHandler(event)"
onDocumentLoadedError="documentLoadedErrorHandler(event)"
onLoadingProgress="onDocumentLoadingProgressHandler(event)"
onCurrPageChanged="onCurrPageChanged(event)"
onExternalLinkClicked="onExternalLinkClickedHandler(event)"
onDocumentPrinted="documentPrintedHandler(event)"
onPageLoaded="pageLoadedEventHandler(event)"
onPageLoading="pageLoadingEventHandler(event)"
onErrorLoadingPage="errorLoadingPageEventHandler(event)"
Scale="{_Scale}"
SwfFile="{_SwfFile}"
ZoomTransition="{_ZoomTransition}"
ZoomTime="{_ZoomTime}"
MinZoomSize="{_MinZoomSize}"
MaxZoomSize="{_MaxZoomSize}"
ZoomInterval="{_ZoomInterval}"
FitPageOnLoad="{_FitPageOnLoad}"
FitWidthOnLoad="{_FitWidthOnLoad}"
EncodeURI="{_EncodeURI}"
FullScreenAsMaxWindow="{_FullScreenAsMaxWindow}"
ProgressiveLoading="{_ProgressiveLoading}"
SearchMatchAll="{_SearchMatchAll}"
PrintPaperAsBitmap="{_PrintPaperAsBitmap}"
ViewModeToolsVisible="{_ViewModeToolsVisible}"
ZoomToolsVisible="{_ZoomToolsVisible}"
NavToolsVisible="{_NavToolsVisible}"
CursorToolsVisible="{_CursorToolsVisible}"
SearchToolsVisible="{_SearchToolsVisible}"
locale="{_localeChain}"
PrintEnabled="{_PrintEnabled}"
/>
</mx:Application>
注:如果上面代码出现<p></p>标签请删除,这是博客自动加上去的。
如果编译出现错误:
设置该项目属性,将附加的编译参数修改成如下所示:
-locale=en_US,fr_FR,zh_CN,es_ES,ru_RU,pt_BR,hu_HU,tr_TR,se_SE,pt_PT,el_EL,dn_DN,cz_CS,it_IT,de_DE,pl_PL,fi_FN,pv_FN,nl_NL,bg_BG -source-path=locale/{locale 如果语言切换只涉及中英文的话,只需要:-locale=en_US,zh_CN -source-path=locale/{locale} 即可。
主要总结:
1、mark文中没有添加多余可用的api接口,从Diyflexpaper.mxml代码中可以看到,已经添加的函数有:
//开放给外部(javascript)调用
ExternalInterface.addCallback("hasFocus", hasFocus);
ExternalInterface.addCallback("setViewerFocus", setViewerFocus);
ExternalInterface.addCallback("gotoPage", gotoPage);
ExternalInterface.addCallback("fitWidth", fitWidth);
ExternalInterface.addCallback("fitHeight", fitHeight);
ExternalInterface.addCallback("loadSwf", loadSwf);
ExternalInterface.addCallback("printPaperRange", printPaperRange);
ExternalInterface.addCallback("searchText", searchText);
ExternalInterface.addCallback("prevSearchMatch", prevSearchMatch);
ExternalInterface.addCallback("nextSearchMatch", nextSearchMatch);
ExternalInterface.addCallback("switchMode", switchMode);
ExternalInterface.addCallback("Zoom", Zoom);
ExternalInterface.addCallback("nextPage", nextPage);
ExternalInterface.addCallback("getCurrPage", getCurrPage);
ExternalInterface.addCallback("prevPage", prevPage);
ExternalInterface.addCallback("printPaper", printPaper);
ExternalInterface.addCallback("getTotalPages", getTotalPages);