この4月から私もマカーになったので、CoverFlowのインタフェースにも慣れてきた(と、言いつつほとんど仮想マシンのubuntu使っているけど)。また、iPodもCoverFlowなインタフェースを持っているので、ちょっと作ってみるのがいいんじゃない?ということで、簡単にFlexで作ってみた。
ネタは、写真をCoverFlowで表示するというもの。Flex CoverFlowで遊んでみた - l4lを参考にして、作ってみた。なんかsearchResultのループで回すところがなんか納得できなかったので、そこだけは自分で簡単にやってみた。
しかし、元ネタのサンプルFlex CoverFlow performance improvement, Flex Carousel Component, and Vertical CoverFlowを見ると、Carouselの方がイカしているじゃない?というわけでこちらを採用。Carouselという言葉の訳では、回転木馬らしいのだけども、「回転木馬」って言われてもしっくりこないので、とりあえずCoverFlowの亜種と言ったらいいのかしらん。
で、こんな感じ。例によって、写真は、私のホンジュラスのダイビングの成果。
サーバサイドのソースは、こう。単純にディレクトリの中のファイルを適当なXMLで出力しているだけ。
-
<?php
-
$dir = 'diving';
-
$doc = new DomDocument('1.0', 'UTF-8');
-
$doc->formatOutput = true;
-
-
$path = 'http://' . $_SERVER['HTTP_HOST'] . '/playgrounds/coverflow';
-
$root = $doc->createElement('images');
-
$root = $doc->appendChild($root);
-
if ($file === '..' or $file === '.') {
-
continue;
-
}
-
$image = $doc->createElement('image', "$path/$dir/$file");
-
$root->appendChild($image);
-
}
-
}
-
}
そして、Flex側のコードは、こんな感じ。Doug McCuneさんのswcファイルはインクルードしておく。
-
<?xml version="1.0" encoding="utf-8"?>
-
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
-
xmlns:coverflow="com.dougmccune.coverflow.*"
-
layout="vertical" horizontalAlign="center" verticalAlign="middle" width="600" height="400"
-
xmlns:containers="com.dougmccune.containers.*"
-
initialize="searchService.send()">
-
<mx:Script>
-
<![CDATA[
-
import mx.controls.Alert;
-
import mx.collections.XMLListCollection;
-
[SWF(frameRate="24")]
-
import mx.core.Container;
-
import mx.events.FlexEvent;
-
import mx.controls.Image;
-
import mx.rpc.events.ResultEvent
-
[Bindable]
-
private var serverURL:String = "images.php";
-
[Bindable]
-
private var xml:XMLListCollection;
-
private function getResult(event:ResultEvent):void
-
{
-
xml = new XMLListCollection(XML(event.result)..image);
-
coverflow.selectedIndex=int(xml.length/2);
-
}
-
private function test(event:Event):void
-
{
-
mx.controls.Alert.show(event.target.parent.source);
-
}
-
]]>
-
</mx:Script>
-
<mx:HTTPService id="searchService" url="{serverURL}" resultFormat="e4x" result="getResult(event)" />
-
<mx:VBox id="box" verticalGap="0" height="100%" width="100%" maxWidth="600" maxHeight="400">
-
<containers:CarouselContainer id="coverflow" width="100%" height="100%"
-
horizontalGap="10" borderStyle="solid" backgroundColor="0x000000"
-
reflectionEnabled="true" autoUpdateFlexMaterials="true">
-
<mx:Repeater id="rp" dataProvider="{xml}">
-
<mx:Box name="p{rp.currentItem}" width="300" height="225" horizontalScrollPolicy="off" verticalScrollPolicy="off" paddingTop="20">
-
<mx:Image horizontalAlign="center" name="img{rp.currentItem}"
-
width="250" height="225" useHandCursor="true" buttonMode="true"
-
source="{rp.currentItem}" click="test(event)" />
-
</mx:Box>
-
</mx:Repeater>
-
</containers:CarouselContainer>
-
</mx:VBox>
-
</mx:Application>
画像をクリックするとtestという関数を呼び出してalertを出すようにしているのだが、その関数で画像を大きくするとかの処理を書いたら、もう少し使える仕組みになるかな。
うーむ。しかし、いい写真だなぁ。




