用几十行代码写一个可以在PC Web,PC桌面,安卓,iOS上运行的程序
有一种撤退不叫撤退,叫战略转移。
如果你想尽快的开发一款应用,并且将它发布到尽量多的用户手中,Flash是目前的最佳选择。
很多人对Flash的认识仅限于网络动画、视频播放,却不知道,不知不觉之间,Flash已经成为了一个软件开发平台。下面,演示一下用这个软件开发平台开发一个demo应用,并将它以多种形式发布。
这个演示只是给读者一个直观认识——Flash的优势是什么。
====
开发一个等待的动画(一个圈,转啊转),这个动画要能够在PC机浏览器上看,也可以下载下来到本地看,也可以在Android平台上看,也可以在iOS平台上看。(Linux等由于用户群太小,忽略了,虽然,也能做到)。
下面是我写的动画控件(AppleStylePreloader,用的我自己的控件解决方案):
View Code
1 <?xml version="1.0" encoding="utf-8"?>
2 <common:BaseContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
3 xmlns:mx="library://ns.adobe.com/flex/mx"
4 xmlns:common="geb.common.*"
5 width="0" height="0"
6 xmlns:controls="geb.controls.*"
7 xmlns:shapes="geb.shapes.*" inited="onInited()"
8 >
9 <fx:Script>
10 <![CDATA[
11 private var timer:Timer;
12 private var slices:int = 12;
13 public var radius:int = 11;
14 public var timerDelay:int = 100;
15 public var sliceColor:uint = 0x333333;
16
17 private function onInited():void
18 {
19 var i:int = slices;
20 var degrees:int = 360 / slices;
21 while (i--)
22 {
23 var slice:Shape = getSlice();
24 slice.alpha = Math.max(0.2, 1 - (0.1 * i));
25 var radianAngle:Number = (degrees * i) * Math.PI / 180;
26 slice.rotation = -degrees * i;
27 slice.x = Math.sin(radianAngle) * radius;
28 slice.y = Math.cos(radianAngle) * radius;
29 box.addChild(slice);
30 }
31 timer = new Timer(timerDelay);
32 start();
33 }
34
35 public function start():void
36 {
37 stop();
38 timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
39 timer.start();
40 }
41
42 private function getSlice():Shape
43 {
44 var slice:Shape = new Shape();
45 slice.graphics.beginFill(sliceColor);
46 slice.graphics.drawRoundRect(-1, 0, 4, 14, 4, 4);
47 slice.graphics.endFill();
48 return slice;
49 }
50
51 private function onTimer(event:TimerEvent):void
52 {
53 if(box != null)
54 box.rotation = (box.rotation + (360 / slices)) % 360;
55 }
56
57 public function stop():void
58 {
59 if(timer != null)
60 {
61 timer.removeEventListener(TimerEvent.TIMER, onTimer);
62 timer.stop();
63 }
64 }
65
66 public override function close():void
67 {
68 stop();
69 super.close();
70 }
71 ]]>
72 </fx:Script>
73 <common:BaseContainer id="box" x="{width * 0.5}" y="{height*0.5}" />
74 </common:BaseContainer>
2 <common:BaseContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
3 xmlns:mx="library://ns.adobe.com/flex/mx"
4 xmlns:common="geb.common.*"
5 width="0" height="0"
6 xmlns:controls="geb.controls.*"
7 xmlns:shapes="geb.shapes.*" inited="onInited()"
8 >
9 <fx:Script>
10 <![CDATA[
11 private var timer:Timer;
12 private var slices:int = 12;
13 public var radius:int = 11;
14 public var timerDelay:int = 100;
15 public var sliceColor:uint = 0x333333;
16
17 private function onInited():void
18 {
19 var i:int = slices;
20 var degrees:int = 360 / slices;
21 while (i--)
22 {
23 var slice:Shape = getSlice();
24 slice.alpha = Math.max(0.2, 1 - (0.1 * i));
25 var radianAngle:Number = (degrees * i) * Math.PI / 180;
26 slice.rotation = -degrees * i;
27 slice.x = Math.sin(radianAngle) * radius;
28 slice.y = Math.cos(radianAngle) * radius;
29 box.addChild(slice);
30 }
31 timer = new Timer(timerDelay);
32 start();
33 }
34
35 public function start():void
36 {
37 stop();
38 timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
39 timer.start();
40 }
41
42 private function getSlice():Shape
43 {
44 var slice:Shape = new Shape();
45 slice.graphics.beginFill(sliceColor);
46 slice.graphics.drawRoundRect(-1, 0, 4, 14, 4, 4);
47 slice.graphics.endFill();
48 return slice;
49 }
50
51 private function onTimer(event:TimerEvent):void
52 {
53 if(box != null)
54 box.rotation = (box.rotation + (360 / slices)) % 360;
55 }
56
57 public function stop():void
58 {
59 if(timer != null)
60 {
61 timer.removeEventListener(TimerEvent.TIMER, onTimer);
62 timer.stop();
63 }
64 }
65
66 public override function close():void
67 {
68 stop();
69 super.close();
70 }
71 ]]>
72 </fx:Script>
73 <common:BaseContainer id="box" x="{width * 0.5}" y="{height*0.5}" />
74 </common:BaseContainer>
1:PC Web发布
建立一个Flex Web项目,代码:
<?xml version="1.0" encoding="utf-8"?>
<common:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:common="geb.common.*" width="300" height="300"
xmlns:preloaders="geb.preloaders.*"
>
<preloaders:ApplyStylePreloader x="{width*0.5}" y="{height*0.5}" />
</common:Application>
<common:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:common="geb.common.*" width="300" height="300"
xmlns:preloaders="geb.preloaders.*"
>
<preloaders:ApplyStylePreloader x="{width*0.5}" y="{height*0.5}" />
</common:Application>
编译得到的Flash不到20K大小。演示如下:
2:桌面发布
建立一个Flex AIR项目,代码:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:containers="geb.containers.*"
xmlns:preloaders="geb.preloaders.*"
width="300" height="300">
<containers:FlexUI width="{width}" height="{height}">
<preloaders:AppleStylePreloader x="{width*0.5}" y="{height*0.5}" />
</containers:FlexUI>
</s:WindowedApplication>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:containers="geb.containers.*"
xmlns:preloaders="geb.preloaders.*"
width="300" height="300">
<containers:FlexUI width="{width}" height="{height}">
<preloaders:AppleStylePreloader x="{width*0.5}" y="{height*0.5}" />
</containers:FlexUI>
</s:WindowedApplication>
编译,发布一下,直接把AIR集成进去,就得到一个绿色软件:
全部文件大小30多M,压缩成zip后为17.8M。运行:
3:Android平台和iOS平台
建一个Flex 移动项目,代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:containers="geb.containers.*"
xmlns:preloaders="geb.preloaders.*"
applicationDPI="160"
>
<containers:FlexUI width="{width}" height="{height}" >
<preloaders:AppleStylePreloader x="{width*0.5}" y="{height*0.5}" />
</containers:FlexUI>
</s:Application>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:containers="geb.containers.*"
xmlns:preloaders="geb.preloaders.*"
applicationDPI="160"
>
<containers:FlexUI width="{width}" height="{height}" >
<preloaders:AppleStylePreloader x="{width*0.5}" y="{height*0.5}" />
</containers:FlexUI>
</s:Application>
Debug,可以看到模拟器,iOS的,Android的,BlackBerry的,五花八门的, 随便选一个iPhone 3GS,运行:
导出安装包,iOS的我没帐号,就只导出Android的了,导出后的文件大小为9M(把AIR打包进去了)。
演示完毕。
====
少年们,知道什么是Flash了吧。就不再总结了,优点,上面Demo已经一目了然了。缺点吗,也不说了,会有人补充的。
版权所有,欢迎转载