注意点・気がついた点
- Android emulatorのCPUはARMでなければならない。Androidはintel CPUでも動作してそちらのほうが動作速度が軽いのだが、Adobe AIR for AndroidはARMにしか対応してないのだ。intel CPUのAndroidにインストールはできるが実行できない。
- AndroidSDKのAVD ManagerでAVD作るときは、縦解像度はemu動作させるPCの縦解像度以下にしないとならないようだ。つまり、1920x1080のノートPCで、Androidに800x1280のNexus7解像度を指定してしまうと、縦解像度が 1080<1280 なので、そのAVDは動作しない。480x800のNexus Sなどをdeviceに選んで、Android versionだけ4.2にしてしまえばいい。
- ActionScript3.0のTextFieldで、htmlTextに <strong> タグ使うとinlineではなくblock level elementになるようで、勝手に改行してしまう。それを避けるため<span class="strong"> にした。
動作環境
- Windows7 64bit
- Adobe Flash Professional CS6 (AIR 3.4 for Android)
- FlashDevelop 4.4.0 + FlexSDK 4.6 + AIR SDK 3.7
- AndroidSDK (Android 4.2 emulator)
swfのコーディング+動作確認をFlashDevelopでおこない、apk生成はFlashCS6で。
adb install *.apkでemulatorにインストール+Androidでの動作確認。
手順
まず、swfを作成。FlashDevelopのほうが使い慣れてるので、こちらで開発。
Hello, Worldの文字だけではなんなので、ドラッグできる円形オブジェクトも置く。
package { import flash.display.GradientType; import flash.display.InterpolationMethod; import flash.display.SpreadMethod; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.MouseEvent; import flash.filters.DropShadowFilter; import flash.filters.GlowFilter; import flash.geom.Matrix; import flash.text.StyleSheet; import flash.text.TextField; import flash.text.TextFieldAutoSize; /** * ... * @author itouhiro */ [SWF(width="640",height="640",backgroundColor="0xdcdcfc",frameRate="30")] public class Main extends Sprite { private var circle:Sprite; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { stage.scaleMode = StageScaleMode.SHOW_ALL; stage.align = StageAlign.TOP_LEFT; removeEventListener(Event.ADDED_TO_STAGE, init); // entry point var tf:TextField = new TextField(); var css:StyleSheet = new StyleSheet(); css.setStyle('p', { fontSize:40, fontFamily:'sans-serif' } ); css.setStyle('.strong', { fontWeight:'bold', color: '#CC0000' } ); tf.styleSheet = css; tf.htmlText = '<p>Hello, <span class="strong">World</span> !</p>'; //tf.x = stage.stageWidth / 4; tf.x = 10; tf.width = stage.stageWidth - (10 * 2); tf.autoSize = TextFieldAutoSize.CENTER; tf.y = stage.stageHeight / 3; addChild(tf); circle = new Sprite(); var radius:int = 50; var mtx:Matrix = new Matrix(); mtx.createGradientBox(radius*1.8, radius*1.8, 0, -radius*1.2, -radius*1.2); var colors:Array = [0xddddff, 0x9999ee, 0x3333dd]; var alphas:Array = [0.6, 0.6, 0.6]; var ratios:Array = [0, 32, 255]; circle.graphics.beginGradientFill( GradientType.RADIAL, colors, alphas, ratios, mtx, SpreadMethod.PAD, InterpolationMethod.RGB, 0); circle.graphics.drawCircle(0,0,radius); circle.graphics.endFill(); circle.x = stage.stageWidth / 2; circle.y = stage.stageHeight / 2; circle.filters = [new DropShadowFilter(18,70,0x999999,0.3)]; addChild(circle); circle.addEventListener(MouseEvent.MOUSE_DOWN, onCirclePushed); circle.addEventListener(MouseEvent.MOUSE_UP, onCircleReleased); } private function onCircleReleased(e:MouseEvent):void { circle.stopDrag(); } private function onCirclePushed(e:MouseEvent):void { circle.startDrag(); } } }
これをAndroidパッケージ化する。FlashDevelopでも作成できるらしいが、まずはFlashCS6でやってみる。
DocumentClassに先ほど書いたMain.asを指定してもいいのだが、今回はタイムラインに「アクション」として書いた。
さきほどの function init()の中身とevent handlerをコピペ。「private」というkeywordを削除しておかないと文法エラーといわれる。
stage.scaleMode = StageScaleMode.SHOW_ALL; stage.align = StageAlign.TOP_LEFT; var tf:TextField = new TextField(); var css:StyleSheet = new StyleSheet(); css.setStyle('p', { fontSize:40, fontFamily:'sans-serif' } ); css.setStyle('.strong', { fontWeight:'bold', color: '#CC0000' } ); tf.styleSheet = css; tf.htmlText = '<p>Hello, <span class="strong">World</span> !</p>'; //tf.x = stage.stageWidth / 4; tf.x = 10; tf.width = stage.stageWidth - (10 * 2); tf.autoSize = TextFieldAutoSize.CENTER; tf.y = stage.stageHeight / 3; addChild(tf); var circle:Sprite; circle = new Sprite(); var radius:int = 50; var mtx:Matrix = new Matrix(); mtx.createGradientBox(radius*1.8, radius*1.8, 0, -radius*1.2, -radius*1.2); var colors:Array = [0xddddff,0x9999ee,0x3333dd]; var alphas:Array = [0.6,0.6,0.6]; var ratios:Array = [0,32,255]; circle.graphics.beginGradientFill( GradientType.RADIAL, colors, alphas, ratios, mtx, SpreadMethod.PAD, InterpolationMethod.RGB, 0); circle.graphics.drawCircle(0,0,radius); circle.graphics.endFill(); circle.x = stage.stageWidth / 2; circle.y = stage.stageHeight / 2; circle.filters = [new DropShadowFilter(18,70,0x999999,0.3)]; addChild(circle); circle.addEventListener(MouseEvent.MOUSE_DOWN, onCirclePushed); circle.addEventListener(MouseEvent.MOUSE_UP, onCircleReleased); function onCircleReleased(e:MouseEvent):void { circle.stopDrag(); } function onCirclePushed(e:MouseEvent):void { circle.startDrag(); }
[ファイル-パブリッシュ設定]でAIR 3.4を選ぶ。
AIRのバージョンは、
- Adobe AIR 3.3 の自動画面回転と Stage の AspectRatio に関する変更 http://cuaoar.jp/2012/08/adobe-air-33-stage-aspec.html
を読むとAIR3.2を選ぶ理由はない。
を読むと、AIR3.4はELS非互換問題があるらしいが、captive runtime(Adobe AIR runtimeを同梱)するなら問題ないだろう。
Adobe AIRの設定で、
[AIRランタイムを埋め込む]にして[パブリッシュ]。
apkファイルのサイズは、AIRランタイムを埋め込まなければ 44KBだが、埋め込むと8980KBになる。
これをAndroidにインストールする。インストールに待たされる‥‥ 2~3分くらい。
Android emulatorで動作確認。
オブジェクトのドラッグもできる。