Archive for the ‘Adobe AIR’ Category
AIR 跟 JS…
Sunday, July 26th, 2009當兩者糾纏在一起的時候,如果由AIR呼叫Javascript 會掛掉的話,可先試著用setTimeout,通常都可以解決。因為HTML DOM需要時間初始化,Javascript 才能被呼叫。
像是 setTimeout( domWindow.myJsFunction , 1000 );
關於Native Menu(Mac) 2
Thursday, May 14th, 2009上次提到的Mac Native Menu 監聽不到自訂menu item之問題有解答了。
問題關鍵在我的 ApplicationUpdaterUI :ApplicationUpdaterUI 在初始化完成後,會摧毀自訂item的事件發送,所以無法選擇,但是系統預設item依舊運作良好。換句話說,在 ApplicationUpdater 發出 UpdateEvent.INITIALIZED 之前,建立的native menu都會失效(看得到選不到)。 因此必須針對 ApplicationUpdaterUI 監聽事件,之後才加上自訂的menu item,如此才能圓滿達成自定menu item與native menu item 共存的成果。
以下例子: 簡單地在 Mac 的第一個menu item下加入一個”myItem”
ui.addEventListener( UpdaterEvent.INITIALIZED , uiInited );
ui.initalize();
function uiInited( e:UpdaterEvent ):void
{
//取得 native menu, 唯有mac os才會有預設的native menu;
//windows下自己建立一個NativeMenu 實體即可
var m:NativeMenu = NativeApplication.nativeApplication.menu;
//items[0] = 第1個menu
m.items[0].addItem( new NativeMenuItem("myItem") ).addEventListener( Event.SELECT , onSelect );
}
function onSelect( e:Event ):void
{
trace("hello there");
}
AIR 程式效能調校技巧
Thursday, May 7th, 2009更新@2009/5/11:
Adobe 有個驅動Garbage Collection的方法:
flash.system.System.gc();
flash.system.System.gc();
但是必須連續呼叫兩次,第一次呼叫: 對物件加上記號;第二次:清除記憶體
更多詳情請見:
http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/
原文:
http://blogs.adobe.com/air/2009/05/performance_tips_for_adobe_air.html
在處理Grant Skinner發現的CPU效能問題之前,有幾個方法可以先改善AIR程式的效能:
- 盡可能地減少frameRate
- Grant Skinner提供了一個小程式,監聽 AIREvent.APPLICATION_DEACTIVE事件(該事件在AIR的native window失去focus時會觸發; 重新得到focus時,會觸發 APPLICATION_ACTIVE),自動將frameRate降至 1 ,可以看他的文章: http://www.gskinner.com/blog/archives/2009/05/idle_cpu_usage.html
- 盡可能用Timer; 而不是監聽EVENT.ENTER_FRAME做處理
- 用profiler 查看Flex app吧
更多文章:
關於AIR Native Menu(for Mac)
Tuesday, May 5th, 2009用Adobe Air 製作 Mac用的native menu時,發生了一個狀況: 在預設的submenu下加上自己的NativeMenuItem,監聽menu的Event.SELECT事件無法成功,很奇怪,但是只要整個menuItem是自己建立的instance就不會出現該問題。換言之,必須手動抽掉 File, Logo 類型的menu item,然後重建。挺麻煩的。
還有一個地方,監聽Event.SELECT的事件處理程式必須加上 Event#stopImmediatePropagation() ,否則會出現重複聽到事件的狀況。
要小心呢!
少用File#nativePath, 用File#url
Sunday, March 22nd, 2009File.nativePath 是完整的絕對路徑,如果直接用nativePath的值當作路徑,在mac上有時會出現IOError。然而,File.url 會把applicationStorage的路徑用app-storage: 替換掉,參照的時候再轉換,對於不同的系統結構,用File.url是比較保險的作法。
trace(docs.nativePath); // C:\Documents and Settings\user\Application Data\my_air\Local Store\
trace(docs.url); // app-storage:/
Drag and Drop on AIR.HTML Using NativeDragManager
Wednesday, March 4th, 2009DOM object in HTML ( HTML.domWindow ) stop the Drag events propagating so that dragging over HTML ui will not succeed, which means you cannot see an accept-drop-icon near mouse cursor. To solve this, you have to register a handler to prevent DOM doing something behind but accept drop action, as following sample code:
function onDomDragOver( e:* ):void
{
e.preventDefault();
NativeDragManager.acceptDragDrop( this );
}
試圖用NativeDragManager拖拉外部物件到Air時,如果遇到HTML元件,會出現無法acceptDragDrop的情況,此時,必須針對HTML#htmlLoader底下的DOM 物件監聽 dragover/dragenter 等javascript事件,阻止DOM自己的事件處理,並在此時,命令HTML接受dragDrop,請見上方原始碼。
PS: Javascript drag event types:
dragover
dragenter
drop
ex: getElementById( “myBox” ).addEventListener( “dragover” , onOverBox );
Table-like List (類似表格的List)
Monday, February 23rd, 2009List displays scrollbar if containing too many items. What if we just need it expand as high as possible no matter how many items here? Some solutions are available :
If variableRowHeight is false, please assign explicit height as number of height multiplies rowHeight, as following code:
Otherwise, if you get variableRowHeigh ture, there is a handy function for you:
Please see List#measureHeightOfItems() for more information.
要讓List元件自己長到顯示所有資料的高度,像是表格一樣,有以下方法:
如果variableRowHeight 為 true , 表示每個資料物件所需欄高不同,可以用 List#measureHeightOfItems(index,count); 得出總高度。 反之,欄高都相同的情況,只要把欄高乘於資料數,得出總高就是了。
消失的BarSeries標籤文字 - Missing Labels on Bar Series
Thursday, December 11th, 2008[更新@2008.12.16]
關於更改Series上的label,修改InstanceCache宣告會更快更好:
*To manipulate label instances on series, overridding the instanceCache object is a better approach.
**/
labelCache = new InstanceCache( Label , labelContainer )
labelCache.property = { truncateToFit : false };
使用Bar Chart 的時候,有個現象:
當Bar上頭的標籤文字太長,以至於無法放進畫面中,BarChart 會清除掉裡面的文字內容:
label.text = “”; (請見BarChart.as line 662)
好在,它只是清掉renderData的cache,並非真正的dataProvider內容(如果是,就糟了),也因此,我們有機會把這些文字回覆,逼它無論如何都要顯示,方法很簡單,你可以自訂一個BarSeries,請見如下程式碼。走訪dataprovider,把值塞回去給Label 實體即可,但請確保文字框夠大,或是把truncateToFit改成false,這樣就不會被裁切掉了。
[English version]
A Bar-Chart renders several series where those labels show up. If you have read source of BarChart.as, you would aware of one thing: If by checking the TextField width against remaining space, there is no enough space to place the label , the label text will be eliminated. ( see source: BarChart.as Line 662 ) However, I think Adobe team might do too much here so that even if you set explicit textField width with a very large number, it won’t work. Fortunately, BarChart itself clear the renderCache instead of dataprovider so that we could recover the text of each label. Here is an example:
//override updateDisplayList()
override protected function updateDisplayList( w:Number , h:Number )
{
super.udpateDisplayList( w , h );
//here we get all created *Label* instances
var labels:Array = labelCache.instances;
// cursor is a IViewCursor, iterating the dataprovider so that cursor#view is just dataprovider object.
var dataList:Array = IList( cursor.view ).toArray();
var len:int = labels.length;
//iterate all label instance, if the text is removed, re-assign it and validate as well.
//one more thing, the length of labels and dataprovider should be the same.
var label:Label;
for(var i:int = 0 ; i < len ; i++ )
{
label = Label( labels[i] );
if( label )
{
label.truncateToFit = false; //optional
label.width = Math.max(w,300); //optional
if( label.text == "" && labelFunction != null )
{
var c:ChartItem = ChartItem( items[i] );
c.item = v[i];
var txt:String = this.labelFunction( c , this )
label.text = txt;
}
label.validateNow();
}
}
Google怎麼搜尋Flash(swf)檔案?
Friday, October 31st, 2008這是老新聞了,不過還是值得一看。
原文: http://googlewebmastercentral.blogspot.com/2008/06/improved-flash-indexing.html
以下是我的翻譯, 將就看吧
Q: 現在,哪種Flash檔案可被Google搜尋加索引呢?
我們(google)提升了對Flash裡『文字』的搜尋能力;包含Flash小工具、小玩具的文字內容,還有選單等等。
Q: 這些Flash檔案的內容,哪些比較容易被找到呢?
所有使用者看得到的文字都可以,會被Google拿來產生標記,也會被用來比對搜尋字眼。
此外,我們也會深入Flash裡面出現的網址(URL)做搜尋,就像我們對待一般網頁一樣。舉例來說,如果你的Flash有許多網站本身的連結網址,Google 會更容易地搜尋你的網站。
Q: 那些不是文字的內容怎麼辦? 比如圖片。
目前來說,我們只會探尋和索引純文字。如果你的Flash全部都是圖片,那就抱歉了。同理可證,我們不會針對Flash裡的按鈕作標記。也請注意,我們也不會索引Flv(Flash影音格式, YouTube 影片就是這個),因為裡面根本沒有文字呀。
Q: Google 是如何看到這些內容的?
我們研發了一個特殊的演算法,它運作起來就如人們操作Flash內容一樣;會按按鈕、會打字等等,之後它會記住輸入過的文字或動作和得到的資訊,藉此作索引依據。嗯,我們不能告訴你全部的細節(小氣!! Orz)但是可以跟你說,這是改良Adobe的Swf搜尋函式庫而來的。
Q: 我要怎麼做,Google 在我的Flash上作索引?
基本上,你啥都別做。 這個改良不需要網站設計師或工程師做任何特別的動作,如果你的網站有Flash,我們會全力地(見下題)去索引它。
承上,你必須了解Google是以『訪客』的腳色進入Flash,如果你想要Google略過某些文字,像是著作權宣告、讀取中等等,你可以考慮把它們換成圖片顯示,如此一來,這些內容對我們來說就像隱形了。
Q: 現在Google的Flash搜尋瓶頸?
有如下幾點,我們正積極地解決:
1. Google搜尋機器人不會執行任何網站上的javascript,如果你的Flash內容是由Javascript 載入的,Google會看不到。
2. 我們不會察覺由Flash動態載入的內容的關連,如果你的Flash讀入HTML, XML, 或是其他Swf,Google會分開索引。
3. 我們幾乎可以索引任何語系的文字,但是,雙向文字不能,像是西伯來文和阿拉伯文。
