Archive

Archive for the ‘development’ Category

[面试最爱问的问题1]Flex/AS3的事件机制

July 19th, 2010 momoko8443 No comments

今天静下心来看了几篇as3事件机制的教程,终于整明白这个面试中最爱问的问题了,嘎嘎!

具体场景描述如下

容器深度:Application->Canvas->HBox->Button;

其中在HBox这个容器中有段代码

button.addEventListener(MouseEvent.Click,onButtonClickHandler);

阶段0:触发阶段,事件被触发,场景中的情况就是用户点击了Button,翻译为代码就是Button中执行了“ dispatch(new MouseEvent(MouseEvent.Click));”大家可以看下Event类中有个target的属性,这个属性就记录着Button这一事件发起者的对象

阶段1:捕获阶段,一旦有事件被触发,FP则会带着这个Event对象从整个程序的最上层开始往下遍历 ,顺序为Application->Canvas->HBox->Button,不停地匹配Event里面中的Target对象和当前遍历到的容器或是组件是不是为同一个对象

阶段2:目标阶段,终于在HBox这个容器中发现到了Button和Event中Target所指向的对象是同一个,ok,事件触发者Button终于被我挨家挨户的排查中找着了。

阶段3:冒泡阶段,从捕获到的目标Button,开始自下而上得往一级级parent走,看看到底是谁会关心Button被用户点击了。

于是再一级级往上HBox->Canvas->Application;果然在遍历到HBox的时候发现它曾经为Button的Click事件加过监听。

阶段4:执行阶段,事件的关心者HBox也找到了,那么执行onButtonClickHandler这个函数即可。

Categories: development Tags:

【转】Create Professional Flex Component

July 14th, 2010 momoko8443 No comments

Flex Builder has a great way to organize its components in tree mode, which is very a good way to organize things and make things clear to any user who are coming to Flex world.

By default, every component you create that is not part of default Flex components you will have placed in the [Custom] directory of Flex Components view in your Flex/Flash Builder, and no matter what properties you add to them, they will never be visible in the Flex Properties standard view.

But what if you want to customize that and create a component that have them all like flex components do? Well, it’s not that hard so let’s do that.

Create the Component

First, you have to create a new Flex Library Project. Do this by going to File->New and choose Flex Library Project. Give it a name, a location, choose whatever Flex SDK you wish to build this for and then click finish.

Now you have a blank library project in which you can create whatever components you want.
Is important to use packages correctly and namespaces and not just drop the component in the [src] folder (you’ll see later why). First create a package (eg. com.adm.component) in which you can add your custom component.

package com.adm.component
{
      import mx.containers.Canvas;

      public class mycomponent extends Canvas
      {
      }
}

Okay, now let’s create some methods to have something going. We’ll make this component be a big button with a method to enable the button and one to change it’s caption. We’ll use setters and getter’s for those properties because some other actions might be required when changing them. So:

package com.adm.component
{
     import mx.containers.Canvas;
     import mx.controls.Button;

      public class mycomponent extends Canvas
      {
            private var _title : String = 'Title';
            private var _active : Boolean = true;

            private var btn : Button = new Button();

            public function CustomComponent()
            {
                      super();
                      this.btn.width = 100;
                      this.btn.height = 100;
                      this.btn.label = this.label;
                      this.addChild(this.btn);
            }

            public function set title(val : String) : void
            {
                      this._title = val;
                      this.btn.label = val;
            }

            public function get title() : String
            {
                     return this._title;
            }

            public function set active(val : Boolean) : void
            {
                     this._active = val;
                     this.btn.enabled= val;
            }

            public function get active() : Boolean
            {
                    return this._active;
            }
      }
}

So, if you now build your project, a .swc file will be generated in your [bin] folder, which you can add in other projects and use. But now, the component will be placed in the [Custom] directory in the Components View and not the one you want. We’ll do that a bit later now let’s just….

Give it an icon

This is really simple. All you have to do is get a nice looking .png icon (16×16 pixels preferably) and place it next to your component. Then, use the IconFile metadata tag to link it to your component.

....
[IconFile("icon.png")]
public class mycomponent extends Canvas
{
       private var _title : String = 'Title';
       .......

Inspectable properties

If you have extra information about the property that will help code hints or the Property Inspector (such as enumeration values or that a String is actually a file path) then also add [Inspectable] metadata with that extra info. For our methods we have:

       ...
       [Inspectable(category="General", type="String", defaultValue="")]
       public function set title(val : String) : void
       .....
       .....
       [Inspectable(category="General", type="Boolean", defaultValue="true", enumeration="true,false")]
       public function set active(val : Boolean) : void

This will also help a lot when we’ll add this properties in the Flex Properties panel. For more informations about the [Inspectable] metadata tag visit http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001658.html

Create custom component folder

So, if you want to build a professional component, you can’t leave Flex add your component in the default [Custom] directory in the Components tree. So, in order to create your own folder, we must use few tricks.

First of all, you need two .xml files to describe the structure you want flex to use and overide it’s default behavior.
The first file is the manifest.xml which describes the components in the package and their namespaces. In our case we’ll have:

< ?xml version="1.0"?>
<componentpackage>
    <component id="mycomponent" class="org.adm.component.mycomponent"/>
</componentpackage>

Second, we need another xml to describe the way the designer will interpret all this.

< ?xml version="1.0" ?>
<design>
	<namespaces>
		<namespace prefix="adm" uri="http://www.adm.org" />
	</namespaces>
	<categories>
		<category id="Test" label="Test Panel" defaultExpand="true" />
	</categories>
	<components>
		<component name="mycomponent" namespace="adm" category="Test" displayName="Rename Me" />
	</components>
</design>

In the design.xml, you can specify the namespaces you used for the components, in this case adm will point to components in the org.adm folder.

Categories tag describes the folders you want added in the Components panel. Each category must have an id which you’ll use to tell components where they should reside, a label for the category to stand as the directory name in the Components panel. defaultExpand is an optional parameter which if set to true, the folder will be showed expanded by default.

In the components tag, you specify which component goes in what category and under what title. The name parameter must match the id of a component listed in the manifest.xml. All the other parameters are pretty self explanatory.

Next, you have to include this two files in .swc package. To do that, follow the steps:

* Right Click at your project and select Properties
* In the left choose Flex Library Build Path
* Select the assets tab and mark to include manifest.xml and design.xml files
* Now select the Flex Library Compiler and include your namespace URL (in this case http://www.adm.org)
* Include the manifest file .xml you’ve created
* Click apply and ok to finish

design Create professional Flex components
manifest Create professional Flex components

After this, you should end up with something like this

comppanel Create professional Flex components

Add properties to the Flex Properties view

As I said before, no matter what properties you add to your component, they will never be visible in the Flex Properties standard view, only in the category view and that only if you use the [Inspectable] metadata tag. But few more lines in the design.xml file should take care of that.

<component name="mycomponent" namespace="adm" category="Test" displayName="Rename Me">
		<mxmlproperties>
			<textfield id="title" name="Component Title:" />
			<combo id="active" name="Active:" />
		</mxmlproperties>
		<defaultattribute name="active" value="true" />
	</component>

The id of the mxmlProperties tag should be the function/variable names from your component you want to edit. You can also define default values for those properties using the defaultAttribute tag below. Here we’ve only used the textfield and the combo type but there are few more you can use.

<textfiled id="propertyOrStyle" name="Label:" [multiline="false"] />
<combo id="propertyOrStyle" name="Label:" />
<colorpicker id="propertyOrStyle" name="Label:" />
<filepicker id="propertyOrStyle" name="Label:" [wrapInEmbed="false"] />
<slider id="propertyOrStyle" name="Label:" min="0" max="10" increment="1" />

For combo boxes, if you use it for a Boolean property, it will automatically be populated with [true,false] values but if want something else, the [Inspectable] metadata tag has the enumeration property where you can define the properties from this combo.

flexprop Create professional Flex components

Another thing you must do in order to apply the values as soon as you change them from the properties view, is to set the functions/variables as [Bindable].

    ....
    [Inspectable(category="General", type="String", defaultValue="")]
    [Bindable]
    public function set title(val : String) : void
	....
	....
    [Inspectable(category="General", type="Boolean", defaultValue="true", enumeration="true,false")]
    [Bindable]
    public function set active(val : Boolean) : void
	....
	....

One more thing

Now, compiling this will give you a 300 something KB .swc file which is a bit large to distribute. The user will use this component inside a flex component so embedding all the libraries and SDK inside your .swc is useless. So the next step is to go to Properties->Flex Library Build Path->Library path tab, expand the build path library try and edit everything inside the SDK tree on Link Type and choose External

paths Create professional Flex components

Now the swc component only have 4KB. Big cut down, eh ?

Done

If this is to much trouble for you or you’ve missed something, I’ve attached the sources for this tutorial here. You can import this skeleton rename the package, namespace and the component and start building on this. I hope it all made sense and….happy coding.

Categories: development Tags:

使用RSL减少swf体积进阶篇

November 17th, 2009 momoko8443 No comments

国庆刚过,手头上没什么事,看看论坛,打打酱油,闲着也是闲着随便写点项目开发中小心得吧

市面上RSL文章很多,基础的我就不写了(一般都是把framework_3.x.x.xxxx.swz文件剥离出去),这里写几则平时不常见的RSL使用小技巧

技巧一:
datavisualization_3.x.x.xxxx.swz 还有rpc_3.x.x.xxxx.swz这两个文件也是可以剥离出去的

技巧二:
有些同学把datavisualization和rpc剥离出去后,发现除了runtimes exception,说什么tween effect有问题,这个时候就需要在Flex build path面板中进行加载顺序的设置了,把framework提升到最上面。

pic
技巧三:
如果你将你的项目发布到Portal环境下,相对路径的URL都会进行转发,也就是说所有使用相对路径加载外部资源的都会失效,那么你就需要把你的swz文件放在一台http server上进行远程加载

pic2

Read more…

Categories: development Tags: ,

在AS3中使用自定义Metadata tag

June 24th, 2009 momoko8443 No comments

最近闲来无事想弄个简单AS3版的Spring framework(支持Ioc和Aop),思路已经有了只差code。上次在team的knowledge sharing meeting上大致演示了一下之前开发的mvc framework——momvc,同事给的意见是新框架最好能做到零配置,不过他们说的零配置,是“约定大于配置”,我并不是特别赞同这一点,这样会使代码不够灵活,造成过多的hard code。不过传统的使用xml配置文件方式确实加大了程序员的工作量,以及协同开发的难度(可能多个程序员要维护同一份配置文件)。于是我想到了annotations,在as3上与之对应的好像也只有Metatag了,于是google了一些自定义metatag的资料,稍作整理分享给大家。

1、在Class中写上自定义Metadata tag

package testcode.test
{
	import testcode.interfaces.IUserService;

	public class UserAction
	{
		[Ioc(Autowired = "userService")]
		public var userService:IUserService;

		public var myProperty:Object ;
		public function UserAction()
		{
		}
	}
}

2、在编译参数中加入 -keep-as3-metadata+=customTagName

image

3、在runtime使用describeType(obj)就能获得之前配置的meta tag,然后你就随心所欲吧

Categories: development Tags:

Flash Builder 4 beta 版 试用感受

June 22nd, 2009 momoko8443 No comments

刚下好beta版,随便点了几分钟,说说几点比较明显的改变之处
1、多了几个namespace, s和mx好像是两种主题。fx感觉是非可视化组件

未命名

2、增加了更多的后台技术支持,可能和3中的import webservice帮你生成n多乱七八糟的代码差不多功能。我稍微试用了一下webservice,和以前一样鸡肋。

未命名1

 

Read more…

Categories: development Tags:

Flex Builder的性能调优

June 22nd, 2009 momoko8443 No comments

我们在开发过程中随着项目的不断壮大,经常会碰到编译速度过慢,IDE崩溃等令人头痛的问题,这里我总结了一下网上别人的经验,对FB进行了3点优化,效果明显

1、把 Build Auto 改成 手动 Build

2、project 右键 -> properties -> Flex Application 中,把不需要编译的mxml文件,移除

3、在安装路径下找到FlexBuilder.ini文件,进行编辑,加入如下参数

-vmargs
-Xms512m
-Xmx512m
-XX:MaxPermSize=512m
-XX:PermSize=512m
-XX:+UseParallelGC
-Djava.net.preferIPv4Stack=true

这3条经验中第2条可以明显加快编译速度数倍,第3条可以明显减低FB挂掉的几率。希望对你有所帮助

Flash Player (9,0,124,0) 版本升级后安全沙箱解决方案(http)

June 22nd, 2009 momoko8443 No comments

概述:由于2008年4月Adobe对Flash Player 进行了版本升级(version 9,0,124,0),这一升级对于安全沙箱有较为重大的影响,以前一些配置crossdomain.xml的方式在新的player中变为不可用。因此很多开发人员都遇到了新的问题,由于项目需要,我对http访问外部资源的过沙箱操作进行了一些研究,稍作整理与大家分享

Flash Player 9,0,124,0 以前老版本的过沙箱方法

例如:

你的swf存放位置: http://www.yourswf.com:8080/flexProjectName/test.swf

你想调用的外部资源位置: http://www.othersxml.com:8088/xmlProjectName/resource.xml

老的解决方案:

1、在 http://www.othersxml.com:8088/xmlProjectName/ ,外部资源项目上下文根目录下存放 crossdomain.xml. 这个xml文件中配置对 www.yourswf.com:8080 这一域名的许可。

2、在 test.swf 代码中加入 Security.loadPolicyFile(http://www.othersxml.com:8088/xmlProjectName/crossdomain.xml); 载入许可文件

版本升级后的改变:

即使在http://www.othersxml.com:8088/xmlProjectName/ 下存放了crossdomain.xml文件,并使用loadPolicyFile加载这个文件,也并不意味着 /xmlProjectName/ 下的所有资源对swf进行了开放。

新的策略要求,在http://www.othersxml.com:8088/ 这个服务器根路径上需要存放一份crossdomain.xml文件,这里称之为主策略文件。而如果是仅仅在 /xmlProjectName/ 下存放crossdomain.xml是没有什么作用的

新的解决方案1:

http://www.othersxml.com:8088/ 下添加crossdomain.xml

格式如下

<cross-domain-policy>
<site-control permitted-cross-domain-policies=”all” />
<allow-access-from domain=”www.yourswf.com” to-ports=”8080″ secure=”true” />
<allow-http-request-headers-from headers=”*” domain=”*” />
</cross-domain-policy>

然后在Swf代码中加载这个文件Security.loadPolicyFile(http://www.othersxml.com:8088/crossdomain.xml);

新的解决方案2:

对于因为某些原因无法在服务器根目录下存放crossdomain.xml文件,例如 swf需要访问http://www.othersxml.com:8088/xmlProjectName/ 下的资源,而你作为一个项目开发维护人员仅仅拥有对于 /xmlProjectName/ 下资源的操作权限,而没有对整个服务器资源配置的权限,因此你无法往http://www.othersxml.com:8088/下存放主策略文件crossdomain.xml。这种情况是经常发生的,对于租赁服务器空间来说这是很常见的现象,因此adobe给了另外一套解决方案。

后台开发人员需要在http://www.othersxml.com:8088/xmlProjectName/ 下建立一个servlet,这里我们访问的url取名为 /getPolicyFile.do

在这个servlet中我们需要实现代码级别的策略(crossdomain属于文件基本的策略)

关键代码如下

File file = new File(“D:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\othersxml\\crossdomain.xml”);

BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file));

byte[] buffer = new byte[4096];

response.addHeader(“Content-Type”, “text/xml; charset=UTF-8″);
response.addHeader(“X-Permitted-Cross-Domain-Policies”, “all”);

OutputStream os = response.getOutputStream();

int length = 0;
while ((length = bis.read(buffer)) != -1)

{
os.write(buffer, 0, length);
os.close();
}
bis.close();

通过代码的形式将policy file 打印出来,并加上http header X-Permitted-Cross-Domain-Policies = all

在swf代码段则使用 Security.loadPolicyFile(http://www.othersxml.com:8088/xmlProjectName/getPolicyFile.do);的形式来进行加载即可突破新的沙箱限制

参考文档:

http://www.riachina.com/showtopic-12813.aspx cimmicola 版主的回帖给予了很大帮助

http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security.html adobe官方的介绍,很详细

谈谈Flex(ActionScript3)中的深度拷贝

June 22nd, 2009 momoko8443 No comments

今天正好有个群友提出了这个问题,于是稍微研究了一下。API和Baidu一番得出了以下结论

public class AddressVBean
{
    public var country:String;
    public var city:String;
    public var address:String;
}
import mx.collections.ArrayCollection;
public class UserVBean
{
    public var name:String;
    public var sex:String;
    [ArrayElementType("com.ibm.vo.AddressVBean")]
    public var addresses:ArrayCollection;
}

以上定义了一个复杂数据类型,以供测试
接下去是深拷贝方法

Read more…

Flex性能优化之cpu占用率

June 22nd, 2009 momoko8443 No comments

先扯几句,team中有一个正在试运行的项目,最近被客户打了回来,问题在于flex的性能问题。症状如下,当as的service层去调用后台java servlet时,ui会弹出一个反复播放的loading动画,回调函数取得数据后进行解析,然后重绘ui。这是一个被大家普遍使用的loading+异步的客户端处理方式。但是问题在于,当执行回调函数时,本该重复执行的动画,出现了停顿状态(如果从调用到显示需要花费6秒,那么这个停顿可能就要占掉3秒),而且整个载入时间也偏长,严重影响了客户体验。

Read more…

利用flexunit进行单元测试二(异步测试)

June 22nd, 2009 momoko8443 No comments

flex和后台打交道几乎都是异步方式的,我最近就在team里面负责service层(一些mvc框架里称作proxy层)的编码工作,撇开传统的Alert,Trace等调试方式,决定引入正规的单元测试flexunit,但是对于异步操作来说,单元测试并非十分简单,网上找了些英文资料,实践后整理一份教材供大家参考

关于flexunit的一些基础知识可以参考我的上一篇文章”利用flexunit进行单元测试一(同步测试)“。

首先看一下我们的service类,这里为了便于演示代码使用HttpService方式

首先建一个xml作为访问资源


    192.168.1.100

随后建立一个方法使用httpService来访问这个xml资源

Read more…

Categories: development Tags: , ,