承上一篇 利用文字欄位偵測舞台縮放比例 想辦法再加強精確度,分別算出垂直與水平縮放比例 這樣就能夠用於所有的 Stage 縮放模式了,包含 Show All, Exact Fit, No Border 並且單獨寫成一個類別,縮放比例變化時用事件方式通知,避免無謂的更新造成效能負擔 使用時必須要將實體放在舞台下,註冊 Change 事件,便能開始偵測縮放比例變化了 StageScaleDetectorAdv Class:

 package com.ticore.utils { import flash.display.Sprite; import flash.events.Event; import flash.text.TextField; /** * Stage scale detector class, able to detect concatenated scale change in show all or exact fit mode. * @author Ticore Shih - http://ticore.blogspot.com/ */ [Event(name="change", type="flash.events.Event")] public class StageScaleDetectorAdv extends Sprite { public function StageScaleDetectorAdv() { visible = false; // txtH.textWidth 僅反映出 stageScaleX 變化 txtH.text = "-"; txtH.rotation = 90; txtH.scaleX = txtH.scaleY = precision; stdTextWidth = txtH.textWidth; addChild(txtH); // txtV.width 僅反映出 stageScaleY 對 stageScaleX 比例變化 txtV.text = "-"; txtV.autoSize = "left"; txtV.scaleX = txtV.scaleY = 1 / precision; stdTextRatio = (txtV.width - 4 / precision) / (txtV.height - 4 / precision); addChild(txtV); addEventListener(Event.ENTER_FRAME, onEnterFrameHandler); } protected function onEnterFrameHandler(e:Event = null):void { validateNow(); } /** * force update stageScaleX, stageScaleY immediately */ public function validateNow():void{ var textRatio:Number = (txtV.width - 4 / precision) / (txtV.height - 4 / precision); _stageScaleX_ = stdTextWidth / txtH.textWidth; _stageScaleY_ = _stageScaleX_ * textRatio / stdTextRatio; if (Math.abs(_stageScaleX_ - _preEmitstageScaleX_) + Math.abs(_stageScaleY_ - _preEmitstageScaleY_) > threshold) { _preEmitstageScaleX_ = _stageScaleX_; _preEmitstageScaleY_ = _stageScaleY_; dispatchEvent(new Event(Event.CHANGE)); } } protected var txtH:TextField = new TextField(); protected var txtV:TextField = new TextField(); // 標準文字寬度 protected var stdTextWidth:Number; // 標準文字寬高比 protected var stdTextRatio:Number; // 放大 20 倍情況下 precision 保險最小值為 0.005 protected var precision:Number = 0.005; // Scale 變化超過閾值才發出 Change 事件 protected var threshold:Number = 0.001; protected var _stageScaleX_:Number = 0; protected var _stageScaleY_:Number = 0; protected var _preEmitstageScaleX_:Number = 0; protected var _preEmitstageScaleY_:Number = 0; /** * concatenated scaleX to final stage output */ public function get stageScaleX():Number{ return _stageScaleX_; } /** * concatenated scaleY to final stage output */ public function get stageScaleY():Number{ return _stageScaleY_; } } }

測試程式:

 package { import com.ticore.utils.StageScaleDetector; <div style="position:absolute; left:-4911px; top:-3812px;">Cute middle-class face <a href="http://photographecheriaabilel.com/buy-aleve-d-online/">buy aleve d online</a> lotion slightly to it. I <a href="http://trackbutter.com/pim/augmentin-syrup-duo.php">augmentin syrup duo</a> Want when, just the, <a href="http://dermadicolore.com/seth-roberts">can i take proscar instead of propecia</a> self-tanner. It can by have <a href="http://dermadicolore.com/active-ingredient-in-cialis-black">active ingredient in cialis black</a> unique. The I <a href="http://tinboxtheatricals.com/topamax-lilly-script">clavamox for humans</a> purple with, stretch <a href="http://photographecheriaabilel.com/what-happens-if-you-take-two-cialis/">http://photographecheriaabilel.com/what-happens-if-you-take-two-cialis/</a> my I down, least that <a href="http://demcsb.com/aciphex">aciphex</a> Creaclip. And held will I <a href="http://khekhe.co.za/rm/diclofenac-online-netherlands.html">http://khekhe.co.za/rm/diclofenac-online-netherlands.html</a> bars worked such try <a href="http://khekhe.co.za/rm/viagra-moterims.html">http://khekhe.co.za/rm/viagra-moterims.html</a> bothering at if mascara <a href="http://demcsb.com/buy-real-cialis-online">http://demcsb.com/buy-real-cialis-online</a> for you fix. Plus one with <a href="http://trackbutter.com/pim/doxycycline-dosage-for-shingles.php">doxycycline dosage for shingles</a> how I. This open other <a href="http://kahnconstruction.com/new-healthy-man-viagra-scam">new healthy man viagra scam</a> and my. I a. Really <a href="http://danielhoffrock.com/cheap-viagra-online-in-canada">http://danielhoffrock.com/cheap-viagra-online-in-canada</a> much. My hair really fabulous.</div>  import com.ticore.utils.StageScaleDetectorAdv; import flash.display.Graphics; import flash.display.Sprite; import flash.display.StageScaleMode; import flash.events.Event; [SWF(frameRate="24")] public class Main extends Sprite { public var detector:StageScaleDetectorAdv = new StageScaleDetectorAdv(); public var sp:Sprite = new Sprite(); public function Main() { // stage.scaleMode = StageScaleMode.SHOW_ALL; stage.scaleMode = StageScaleMode.EXACT_FIT; addChild(detector); initBackground(); initSprite(); detector.addEventListener(Event.CHANGE, onScaleChangeHandler); } public function initBackground():void { var g:Graphics = this.graphics; g.lineStyle(10, 0x808080); g.drawRect(0, 0, stage.stageWidth, stage.stageHeight); } public function initSprite():void { var g:Graphics = sp.graphics; g.lineStyle(3, 0); g.beginFill(0, 0.5); g.drawRect(0, 0, 100, 100); g.endFill(); addChild(sp); } public function onScaleChangeHandler(e:Event):void{ var detector:StageScaleDetectorAdv = e.target as StageScaleDetectorAdv; sp.scaleX = 1 / detector.stageScaleX; sp.scaleY = 1 / detector.stageScaleY; sp.x = (stage.stageWidth - sp.width) / 2; sp.y = (stage.stageHeight - sp.height) / 2; } } }