读书人

用Flex AIR 做一个相仿QQ消息提示在系

发布时间: 2012-10-26 10:30:59 作者: rapoo

用Flex AIR 做一个类似QQ消息提示在系统托盘闪烁图标
这是我在国外一个论坛上找的例子,我不是很懂,希望各位能帮忙解释下!
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
width="200" height="100"
initialize="testAlert()">

<mx:Script>
<![CDATA[
import com.everythingflex.air.managers.IconManager;

[Embed(source="images/no_red.png")]
private var Icon16:Class;
private var bitmap16:Bitmap = new Icon16();

private var defaultSysDockIconBitmaps:Array = [bitmap16.bitmapData];

private var i:IconManager = new IconManager(defaultSysDockIconBitmaps);

private var timer:Timer;

private function testAlert():void{
timer = new Timer(3000,1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE,timerComplete);
timer.start();
}

private function timerComplete(event:TimerEvent):void{
/**
* 设置提示信息 (Windows)
*/
i.startAlert('消息提示')
}
]]>
</mx:Script>
<mx:Button click="testAlert()" label="开始提示"
horizontalCenter="0" verticalCenter="0"/>
</mx:WindowedApplication>
这个例子的IconManager封装在了一个SWC里,下面是它的Class源代码:
[font=Tahoma]/*
Copyright (c) 2008 EverythingFlex.com.
http://code.google.com/p/everythingflexairlib/
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package
{
import flash.desktop.NativeApplication;
import flash.desktop.NotificationType;
import flash.desktop.SystemTrayIcon;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.TimerEvent;
import flash.filters.ColorMatrixFilter;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.utils.Timer;

public class IconManagertest
{
/**
* @private
* stores the Array of bitmaps tobe used as icons
*/
private var sysDockIconBitmaps:Array = new Array();
/**
* @private
* stores the Array of altered bitmaps tobe used as icons
*/
private var alteredSysDockIconBitmaps:Array = new Array();
/**
* @private
* stores the alertType, defaults to NotificationType.CRITICAL
*/
private var alertType:String = NotificationType.CRITICAL;
/**
* @private
* used as an indicator to determine which state of icon to display
*/
private var _count:int;
/**
* @private
* timer used to alternate between sysDockIcon and alteredSysDockIcon
*/
private static var ALERT_TIMER:Timer;
/**
* Constructor.
*/
public function IconManagertest(sysDockIconBitmaps:Array,
alteredSysDockIconBitmaps:Array=null,
alertType:String="critical") {
this.sysDockIconBitmaps = sysDockIconBitmaps;
this.alteredSysDockIconBitmaps = alteredSysDockIconBitmaps;
this.alertType = alertType;
handleIcons();
}
/**
* @private
* called by constructor to initialize the icons sets
*/
private function handleIcons():void{
stopAlert();
if(NativeApplication.supportsDockIcon || NativeApplication.supportsSystemTrayIcon){
if(sysDockIconBitmaps.length > 0){
NativeApplication.nativeApplication.icon.bitmaps = sysDockIconBitmaps;
if(alteredSysDockIconBitmaps == null){
alteredSysDockIconBitmaps = new Array();
for (var i:int=0; i<sysDockIconBitmaps.length;i++){
alteredSysDockIconBitmaps.push(applyAlertFilter(sysDockIconBitmaps.clone(),i));
}
}
}
}
}
/**
* @private
* alters icon to reverse of original using a filter
*/
private function applyAlertFilter(bitmapData:BitmapData,i:int):BitmapData {
var matrix:Array = new Array();
matrix = matrix.concat([-1, 0, 0, 0, 255]);
matrix = matrix.concat([0, -1, 0, 0, 255]);
matrix = matrix.concat([0, 0, -1, 0, 255]);
matrix = matrix.concat([0, 0, 0, 1, 0]);
var r:Rectangle;
if(i == 0)r= new Rectangle(0,0,16,16);
if(i == 1)r= new Rectangle(0,0,32,32);
if(i == 2)r= new Rectangle(0,0,48,48);
if(i == 3)r= new Rectangle(0,0,128,128);
bitmapData.applyFilter(bitmapData,r,new Point(),new ColorMatrixFilter(matrix));
return new Bitmap(bitmapData).bitmapData;
}
/**
* @public
* starts an alert and shows toolTip message (Windows Only)
*/
public function startAlert(message:String="Alert"):void{
IconManager.ALERT_TIMER = new Timer(500,0);
IconManager.ALERT_TIMER.addEventListener(TimerEvent.TIMER,changeIcon)
IconManager.ALERT_TIMER.start();
if(NativeApplication.supportsSystemTrayIcon){
SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = message;
}
}
/**
* @public
* stops an alert
*/
public function stopAlert():void{
if(NativeApplication.supportsSystemTrayIcon){
SystemTrayIcon(NativeApplication.nativeApplication.icon).tooltip = "";
}
try{
IconManager.ALERT_TIMER.stop();
} catch (e:Error){

}
}
/**
* @private
* timer handler, alters state of icon
*/
private function changeIcon(event:TimerEvent):void{
if(_count == 0){
if(sysDockIconBitmaps.length){
NativeApplication.nativeApplication.icon.bitmaps = sysDockIconBitmaps;
}
_count = 1;
} else {
if(alteredSysDockIconBitmaps.length){
NativeApplication.nativeApplication.icon.bitmaps = alteredSysDockIconBitmaps;
}
_count = 0;
}
}
}
}[/font]

这是SWC附件:

读书人网 >网络基础

热点推荐