Wednesday, May 2, 2007

イメージビューアを作ってみよう

Apolloランタイムでは、ファイルシステムの参照が可能です。この機能を生かして、ハードディスク内にある画像を表示させるアプリケーションを作成してみよう。

イメージビューアを作成するには、以下のコンポーネントが必要です。

  • Image (ID="imageContainer")
  • Button
  • Label (ID="infoLabel")
  • FileSystemTree (ID="tree")


上にFileSystemTreeを含みました。今回は、ハードディスク内のイメージを表示させるので、FileSystemTreeというコンポーネントを使用して、エクスプローラーのようなツリー状のスタイルでファイルを選択してイメージを表示させたいと思います。

まず、スクリプト部分は、以下のようになります。


<mx:Script>
<![CDATA[
import flash.filesystem.File;
public function init( ):void {
tree.directory = File.documentsDirectory;
}
public function getDetails( ):void {
var node:File = tree.selectedItem as File;
if(node != null) {
if(node.isDirectory) {
logDirectoryDetails(node);
} else {
logFileDetails(node);
}
}
}
public function logFileDetails(file:File):void {
infoLabel.text = file.nativePath;
imageContainer.source = file.url;
}
public function logDirectoryDetails(dir:File):void {
infoLabel.text = dir.nativePath ;
}
]]>
</mx:Script>



以下の一行でファイルシステムにアクセスできるようにします。


import flash.filesystem.File;



以下の関数では、デフォルトでマイドキュメントのディレクトリを参照する指定を行います。


public function init( ):void {
tree.directory = File.documentsDirectory;
}



以下の関数でImageコンポーネントに画像のファイルのパスを渡す処理を行います。


public function logFileDetails(file:File):void {
infoLabel.text = file.nativePath; //Labelに選択したパスを表示する
imageContainer.source = file.url; //Imageコンポーネントのsourceに表示させるイメージのパスを渡す
}



file.nativePath;は、実際どのようなパスかを表示させます。Traceで
file.nativePath;の値を調べてみると、以下のような値を返します。


C:\Documents and Settings\username\My Documents\My Pictures\Picture\adobe1.jpg



imageContainer.sourceには、表示させたいイメージのパスを渡してあげるのですが、パスの渡し方がちょっと特殊なので、
imageContainer.source = file.nativePath;というようには行きません。イメージのパスを渡す場合は、file.urlを使います。

Traceでfile.urlがどのような値か調べてみると、以下のような値を返します。


file:///C:/Documents%20and%20Settings/username/My%20Documents/My%20Pictures/Picture/adobe1.jpg



イメージを表示させる際に、このパスの渡し方に気をつけましょう。





コード全文


<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
applicationComplete="init( )"
>
<mx:Script>
<![CDATA[
import flash.filesystem.File;
public function init( ):void {
tree.directory = File.documentsDirectory;
}
public function getDetails( ):void {
var node:File = tree.selectedItem as File;
if(node != null) {
if(node.isDirectory) {
logDirectoryDetails(node);
} else {
logFileDetails(node);
}
}
}
public function logFileDetails(file:File):void {
infoLabel.text = file.nativePath;
imageContainer.source = file.url;
trace("file.nativepath is: " + file.nativePath);
trace("file.url is: " + file.url);
}
public function logDirectoryDetails(dir:File):void {
infoLabel.text = dir.nativePath ;
}
]]>
</mx:Script>
<mx:Panel
title="Picture Viewr"
width="98%"
height="98%"
verticalCenter="0"
horizontalCenter="0"
>
<mx:HBox
height="100%"
width="100%">

<mx:VBox
width="20%"
height="100%">

<mx:FileSystemTree
id="tree"
height="90%"/>

<mx:Button
label="Load Image"
click="getDetails( )"/>
</mx:VBox>

<mx:VBox
height="100%"
width="80%">

<mx:Image
id="imageContainer"
styleName="picture"
maxWidth="500"
maxHeight="500"
scaleContent="true">
</mx:Image>
<mx:Label
id="infoLabel"
text="Info" />

</mx:VBox>
</mx:HBox>
</mx:Panel>
</mx:ApolloApplication>





リファレンスサイト

Apolloポケットガイド邦訳Wiki - ファイルブラウザを作る

Monday, April 23, 2007

TextInputに入力した文字をLabelに渡してみよう

今回は、TextInputコンポーネントに入力した値をLabelコンポーネントに渡してみたいと思います。
前回のマウスイベントの取得を応用して、ボタンをクリックした際にTextInputに入力してある値をLabelに表示させます。

用意するコンポーネントは、

  • TextInput
  • Label
  • Button


今回は、ボタンをクリックしたタイミングでTextInputのテキストをLabelのテキストに代入する処理を行います。この処理を行うには、以下のようなコードを記述します。


public function clickHandler(event:Event):void {
resultString.text = inputString.text;
}


前回同様にクリックのイベントが発生したら、inputString.textをresultString.textに代入します。

resultStringは、TextInputのIDです。
inputStringは、LabelのIDです。





コード全文


<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
width="300"
height="150"
layout="absolute"
backgroundColor="#334455"
creationComplete="createListener(event);"
>
<mx:Script>
<![CDATA[
import flash.events.MouseEvent;
import mx.events.FlexEvent;
public function createListener(event:FlexEvent):void {
myButton.addEventListener(MouseEvent.CLICK, clickHandler);
}
public function clickHandler(event:Event):void {
resultString.text = inputString.text;
}
]]>
</mx:Script>
<mx:Panel
title="TextInput to Label"
width="90%"
height="90%"
horizontalCenter="0"
verticalCenter="0"
paddingLeft="10"
paddingTop="10"
paddingRight="10"
paddingBottom="10"
layout="vertical"
backgroundAlpha="30"
>
<mx:HBox width="100%" height="100%">
<mx:Label
text="Input:"
/>
<mx:TextInput
id="inputString"
width="100%" text=""
/>
<mx:Button
id="myButton"
label="OK"
/>
</mx:HBox>
<mx:HBox width="100%" height="100%">
<mx:Label text="Result:" />
<mx:Label
id="resultString"
width="100%"
/>
</mx:HBox>
</mx:Panel>
</mx:ApolloApplication>

マウスイベントを取得してみよう

Apolloのアプリケーションと言っても記述するコードの中身はFlexのMXMLおよびActionScriptになります。今回は、各マウスのイベントを取得してみましょう。

マウスイベントを取得するにはいくつか方法があるかと思いますが、イベントについて、FlexのLiveDocsに説明が書いてあります。

イベントの使用

今回は、マウスをクリックした時、マウスをボタン上にロールオーバーした時、マウスをボタンからロールアウトした時のイベントを取得します。

まず、マウスをクリックした時のイベントを取得するには、

MouseEvent.CLICK



また、ロールオーバー、ロールアウトには、

MouseEvent.ROLL_OVER //ロールオーバー
MouseEvent.ROLL_OUT //ロールアウト



を使用します。

このマウスイベントを取得するには、イベントリスナーが必要となります。イベントリスナーはイベントが発生したことを監視するaddEventListenerという監視役が必要です。そこで、今回はボタンにイベントリスナーを適用するので、以下のような感じになります。

myButton.addEventListener (MouseEvent.CLICK, clickHandler);



また、発生したイベントに応じて、何らかの処理を実行したいので、addEventListenerの第二引数の名前の関数を以下のように用意します。


private function clickHandler ( event:Event ):void {
//ここに実行したい処理を記述
}



今回の場合は、マウスをクリックした際に、myLabelに「Mouse Click」という文字を表示させたいので、以下のような記述になります。


private function clickHandler ( event:Event ):void
{
myLabel.text = "Mouse Click";
}



さて、これらのコードですが、MXMLファイルのどこにでも記述して良いわけではないので、あるタグで囲ってあげる必要があります。スクリプトの処理を行う場合は、<mx:Script> から始まるタグの中にさらに<![CDATA[の中でスクリプトの処理を行う必要があります。また、]]></mx:Script>で閉じる必要があるのでお忘れなく。


<mx:Script>
<![CDATA[

]]>
</mx:Script>



実際、今回の処理をこのタグに入れると以下のような感じになります。


<mx:Script>
<![CDATA[

public function createListener():void {
myButton.addEventListener(MouseEvent.CLICK, clickHandler);
myButton.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
myButton.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
}

public function clickHandler(event:Event):void {
myLabel.text = "Mouse Click";
}
public function rollOverHandler(event:Event):void {
myLabel.text = "Roll Over";
}
public function rollOutHandler(event:Event):void {
myLabel.text = "Roll Out";
}
]]>
</mx:Script>





public function createListener():void



なお、上の関数を使用しているで、<mx:ApolloApplication内にcreationComplete="createListener();"を追加しておきましょう。

スクリプトの部分は上記で完了です。さて、スクリプトの処理を実行する元となるボタンなどの設定は、以下の通りです。


<mx:Panel
title="My Application"
horizontalCenter="0"
verticalCenter="0"
paddingTop="10"
paddingBottom="10"
paddingLeft="10"
paddingRight="10"
>

<mx:Label
id="myLabel"
width="230"
fontWeight="bold"
fontSize="24"
/>

<mx:Button
id="myButton"
label="Button"
/>

</mx:Panel>



スクリプト部分で、myButton.addEventListenerと記述してあります。この部分のmyButtonは、ボタンコンポーネントのIDを示しているので、<mx:Buttonでid="myButton"という感じでスクリプト内で使用している名前に統一しておきましょう。ラベルコンポーネントも同様です。




コード全文


<?xml version="1.0" encoding="utf-8"?>
<mx:ApolloApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
width="350" height="200"
creationComplete="createListener();"
>

<mx:Script>
<![CDATA[
public function createListener():void {
myButton.addEventListener(MouseEvent.CLICK, clickHandler);
myButton.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
myButton.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
}

public function clickHandler(event:Event):void {
myLabel.text = "Mouse Click";
}
public function rollOverHandler(event:Event):void {
myLabel.text = "Roll Over";
}
public function rollOutHandler(event:Event):void {
myLabel.text = "Roll Out";
}
]]>
</mx:Script>
<mx:Panel
title="My Application"
horizontalCenter="0"
verticalCenter="0"
paddingTop="10"
paddingBottom="10"
paddingLeft="10"
paddingRight="10"
>

<mx:Label
id="myLabel"
width="230"
fontWeight="bold"
fontSize="24"
/>

<mx:Button
id="myButton"
label="Button"
/>

</mx:Panel>
</mx:ApolloApplication>




リファレンスサイト

Class MouseEvent

Flex クイックスタート:イベントの処理