投稿

6月, 2010の投稿を表示しています

サイズの異なる文字列をボタン上に配置する

ヒントは ここ にあったけど、惜しい、XMLからstrings.xmlのリソースを指定したいのにこの方法だとダメ。あと一歩足りない。 というわけでやり方はこちら。 MultiTextViewというカスタムViewを作成します。 仕様的にはmainTextとsubTextを指定可能とし、それぞれ、テキスト、文字サイズ、文字色を指定可能とします。 まずは、XMLから指定可能な拡張attrを定義します。 valuesフォルダ配下にattrs.xmlファイルを配置して中身をこんな感じに。 -attrs.xml 次に、MultiTextButton.javaで実装。 -MultiTextButton.java public class MultiTextButton extends Button { public MultiTextButton(Context context, AttributeSet attrs) { super(context, attrs); initAttribute(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiTextButton); CharSequence mt = a.getText(R.styleable.MultiTextButton_mainText); CharSequence st = a.getText(R.styleable.MultiTextButton_subText); if ( mt != null ) mainText = mt.toString(); if ( st != null ) subText = st.toString(); mainTextSize = a.getDimension(R.styleable.MultiTextButton_mainTextSize, 10.0f); subTextSize = a.getDimensi

うちの子Ver.1.1

NewbornクラスにSensorListenerを追加しました。 眠い時はZ軸方向加速度を与えてやると寝ます。 値の絶対値が閾値より小さいと例外発生します。 @Override public void onSensorChanged(SensorEvent event) { // 眠い時のみ発動 if ( this.state == STATE_SLEEPY ) { // 加速度センサー if ( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER ) { // Z軸方向のみ対象です float z = event.values[SensorManager.DATA_Z]; // 閾値を超えないとやり直し if ( Math.abs(z) < THRESHOLD ) { sleepy = 0; throw new HumanException(OUTPUT_CRY); } else { // 一定回数閾値を超え続けると寝ます sleepy++; if ( sleepy > SLEEPY_THRESHOLD ) { setState(STATE_SLEEPED); } } } 結構感度高いです。

うちの子

生後35日目です。 10分で実装したのでちょっと雑な動作をします。 未試験です。 package com.awwa.newborn; import Human; public class Newborn extends Human { public static final int OUTPUT_NO_CRY = 0; public static final int OUTPUT_CRY = 1; public static final int STATE_HUNGRY = 0; public static final int STATE_WET = 1; public static final int STATE_SLEEPY = 2; private int state = STATE_HUNGRY; private int outputState = OUTPUT_CRY; @Override private static void life() { while( true ) { try { switch(state) { case STATE_HUNGRY: throw new HumanException(OUTPUT_CRY); break; case STATE_WET: throw new HumanException(OUTPUT_CRY); break; case STATE_SLEEPY: throw new HumanException(OUTPUT_CRY); break; } Thread.Sleep(10800000); } catch (HumanException ex) { outputState = ex.getMessage(); publishState(outputState); } finally { continue; } } } }

ViewをTranslateして表示する

イメージ
Button01の背後からButton02をスライドアニメーションさせて表示する。 なんでこんなの実装しなきゃいけないんだろう、とか思いつつ。 個人的には趣味じゃないですが、やらなきゃいけない状況だったのでやってみました。 結構ハマりつつ。 ◇完成イメージ こんな感じでButton02がアニメーションする。 → → ◇やり方 「layout.xml」はこんな感じ。 ポイントはButton02を乗せたSlidingPanelクラスをButton01の下に配置します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 前面に表示されているボタン --> <Button android:id="@+id/Button01" android:text="Button01" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- 下にスライドするLayout(初期状態はGONE) --> <com.awwa.animsample2.SlidingPanel android:id="@+id/panel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/Button01" android: