LZXでは画面に表示される部品、
座標について
画面の座標
画面の横方向にX座標、
X座標は原点から右方向に正の値、
<canvas proxied="false" bgcolor="0xffffcc">
<view width="200" height="50" bgcolor="#ff0000"/>
<button>ボタン</button>
<text>こんにちは</text>
</canvas>
リスト1のサンプル
座標指定
リスト1ではボタンと文字列が重なってしまっているので、
リスト2は、
<canvas proxied="false" bgcolor="0xffffcc">
<view width="200" height="50" bgcolor="#ff0000"/>
<button>ボタン</button>
<text x="100" y="20">こんにちは</text>
</canvas>
リスト2のサンプル
前後関係
Z座標
リスト3のサンプルでは赤黄青というソースの順番に奥から手前に向かって表示されています。
<canvas proxied="false" bgcolor="0xffffcc">
<!--赤--><view width="100" height="50" bgcolor="#ff0000"/>
<!--黄--><view x="20" y="20" width="100" height="50" bgcolor="#ffff00"/>
<!--青--><view x="40" y="40" width="100" height="50" bgcolor="#0000ff"/>
</canvas>
リスト3のサンプル
この前後関係は動的に変更することができます。たとえばビューをクリックしたときに前面に表示させるには、
onclickは該当ビューをクリックした時のイベントハンドラです。bringToFront()についているthisというのは自分自身つまりスクリプトが書かれているビュー自身を指します。
<canvas proxied="false" bgcolor="0xffffcc">
<view width="100" height="50" bgcolor="#ff0000" onclick="this.bringToFront()"/>
<view x="20" y="20" width="100" height="50" bgcolor="#ffff00" onclick="this.bringToFront()"/>
<view x="40" y="40" width="100" height="50" bgcolor="#0000ff" onclick="this.bringToFront()"/>
</canvas>
リスト4のサンプル 四角をクリックしてみてください。
親子関係
タグの親子関係とレイアウトには密接な関係があります。子タグの座標系は親タグが基準となります。
親の違いによる座標系の違い
ビューのデフォルト配置位置は原点(x,y)=(0,0)ですね。厳密に言うと親タグの(0,0)になります。
たとえばリスト5ではボタン1の親は<canvas>なので画面全体の左上が原点になります。一方、
<canvas proxied="false" bgcolor="0xffffcc">
<button>1</button>
<view x="50" y="20" width="100" height="50" bgcolor="#ffff00">
<button>2</button>
</view>
</canvas>
リスト5のサンプル
兄弟関係
兄弟関係とは同一階層にあるビュー同士のことです。リスト4でいうと赤黄青のビュー、
親子関係の場合は子が前面に、
自動配置の属性
簡単なセンタリングや右寄せなどは座標で指定しなくても属性で自動配置できます。
横センタリング、右寄せ、左寄せ
横方向の自動配置はalign属性で指定できます。左寄せはleft
<canvas proxied="false" bgcolor="0xffffcc">
<button align="left">左寄せ</button>
<button align="center">センタリング</button>
<button align="right">右寄せ</button>
</canvas>
リスト6のサンプル
縦センタリング、上寄せ、下寄せ
縦方向の自動配置はvalign属性で指定できます。上寄せはtop(デフォルト)、
<canvas proxied="false" bgcolor="0xffffcc">
<button valign="top">上寄せ</button>
<button valign="middle">センタリング</button>
<button valign="bottom">下寄せ</button>
</canvas>
リスト7のサンプル
画像の表示、スライドショーの作成
ビューの大きさも位置も同じ場合、
タイマーをセットするような感じでビューを自動で動かすにはアニメーション機能を使います。タグは<animator>を使います。複数の<animator>を同時あるいは連続して実行するには<animatorgroup>で囲みます。どんなタイミングで何をどう動かすかといった指示を与えるためのイベントハンドラというものも使います。
- ※アニメーション機能やイベントハンドラについては今後の回で詳しく解説します。ここでは背伸び的な応用ということでサンプルソースを眺めておいてください。
ビューによるスライドショー
bringToFront()だけだとチカチカと切り替わるだけで見た目おしゃれじゃないので、
リスト8ではテストとしてとりあえず赤黄青のビューを用意しました。フェードインさせるには各ビューの透明度(opacity)を順番に
設定の解説はリスト8のソースのコメントをご参照ください。
<canvas proxied="false" bgcolor="0xffffcc">
<animatorgroup repeat="Infinity"><!--永久にリピートする-->
<animator duration="2000"/><!--2秒待つ-->
<animator attribute="opacity" from="0" to="1" duration="1000" target="a"/><!--aを1秒かけて透明から不透明に-->
<animator duration="2000"/><!--2秒待つ-->
<animator attribute="opacity" from="0" to="1" duration="1000" target="b"/><!--bを1秒かけて透明から不透明に-->
<animator duration="2000"/><!--2秒待つ-->
<animator attribute="opacity" from="0" to="1" duration="1000" target="c"/><!--cを1秒かけて透明から不透明に-->
</animatorgroup>
<!--それぞれのビューにid属性で名前をつける。透明度が変化した瞬間(onopacity)に最前面表示する-->
<view id="a" width="200" height="150" bgcolor="#ff0000" onopacity="this.bringToFront()"/>
<view id="b" width="200" height="150" bgcolor="#ffff00" onopacity="this.bringToFront()"/>
<view id="c" width="200" height="150" bgcolor="#0000ff" onopacity="this.bringToFront()"/>
</canvas>
リスト8のサンプル
ビューで画像の表示
LZXではjpg、
リスト9はlzxファイルと同じフォルダ内にあるa.
<canvas proxied="false" bgcolor="0xffffcc">
<view resource="a.jpg"/>
</canvas>
リスト9のサンプル
画像スライドショー完成品その1
リスト8とリスト9のコードを元に作ったフェードイン型スライドショーです。動作サンプルはFlashですが、
<canvas proxied="false" bgcolor="0xffffcc">
<animatorgroup repeat="Infinity">
<animator duration="2000"/>
<animator attribute="opacity" from="0" to="1" duration="1000" target="a"/>
<animator duration="2000"/>
<animator attribute="opacity" from="0" to="1" duration="1000" target="b"/>
<animator duration="2000"/>
<animator attribute="opacity" from="0" to="1" duration="1000" target="c"/>
</animatorgroup>
<view id="a" resource="a.jpg" onopacity="this.bringToFront()"/>
<view id="b" resource="b.jpg" onopacity="this.bringToFront()"/>
<view id="c" resource="c.jpg" onopacity="this.bringToFront()"/>
</canvas>
リスト10のサンプル
このサンプルはダウンロードしてFlash版とHTML5版の動作を試していただけます。
リスト10のサンプルのダウンロード
画像スライドショー完成品その2
次のサンプルはリスト10のコードを少し改造して画像を左から右にスライドさせるスライドショーにしてみたものです。
<animator>のattribute属性をxにしてx値を-200から0に移動させています。x値が-200というのは原点から200ピクセル左のことで、
<canvas proxied="false" bgcolor="0xffffcc">
<animatorgroup repeat="Infinity">
<animator duration="2000"/>
<animator attribute="x" from="-200" to="0" duration="1000" target="a"/>
<animator duration="2000"/>
<animator attribute="x" from="-200" to="0" duration="1000" target="b"/>
<animator duration="2000"/>
<animator attribute="x" from="-200" to="0" duration="1000" target="c"/>
</animatorgroup>
<view id="a" resource="a.jpg" onx="this.bringToFront()"/>
<view id="b" resource="b.jpg" onx="this.bringToFront()"/>
<view id="c" resource="c.jpg" onx="this.bringToFront()"/>
</canvas>
リスト11のサンプル