AnimationとVisivilityの組み合わせ

693 views
Skip to first unread message

awwa

unread,
Jun 5, 2010, 8:01:23 AM6/5/10
to Android-SDK-Japan
あわと申します。

AnimationとVisivilityの組み合わせの動作でちょっと悩んでいます。
良い解決方法ないでしょうか?

◇やりたいこと
Button01を押下することでButton01の背後(Z order behind)からButton02をAnimationを利用して下方向
にtranslateしたい。

◇問題
下記の実験コードを作って動作させると、概ね期待通りの動作をするのですが、
button02がVISIBLEになってからAnimationされるため、
一瞬Button01の下にButton02がチラッと表示されしまいます。
この「チラッ」が見えないようにしたいと思っています。
Visibilityを制御しているのは、画面表示時にはbutton02は見えない状態にしておき、
button01が押下された場合のみbutton02を表示したいと考えているためです。

◇実験コード
layout.xml
RelativeLayout内に下記の2つのViewを配置。
----
<!-- 前面に表示されているボタン -->
<Button
android:id="@+id/Button01"
android:text="Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<!-- 下にスライドするボタン(初期状態はGONE) -->
<Button
android:id="@+id/Button02"
android:text="Button02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Button01"
android:visibility="gone"
/>
----

Activity側実装
----
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

anim = AnimationUtils.loadAnimation(this, R.anim.translate);

button01 = (Button)findViewById(R.id.Button01);
button02 = (Button)findViewById(R.id.Button02);
button01.setOnClickListener(this);

// Control Z order
button01.bringToFront();
}

@Override
public void onClick(View v) {

if ( v == button01 )
{
// btn1クリックで下にスライドするアニメーション開始
button02.startAnimation(anim);
button02.setVisibility(View.VISIBLE);
}
}
----

translate定義
----
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillBefore="false"
android:fillAfter="false" >
<translate
android:interpolator="@android:anim/accelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="-100%"
android:toYDelta="0"
android:duration="1000"
android:fillBefore="false"
android:fillAfter="false"
/>
</set>
----

Ishikawa Hiromi

unread,
Jun 6, 2010, 10:42:25 PM6/6/10
to android-...@googlegroups.com
石川と申します。

AlpahaAnimationを使用してはいかがでしょうか?

2010/6/5 awwa <aww...@gmail.com>

--
このメールは Google グループのグループ「Android-SDK-Japan」の登録者に送られています。
このグループに投稿するには、android-...@googlegroups.com にメールを送信してください。
このグループから退会するには、android-sdk-ja...@googlegroups.com にメールを送信してください。
詳細については、http://groups.google.com/group/android-sdk-japan?hl=ja からこのグループにアクセスしてください。


awwa

unread,
Jun 7, 2010, 9:04:26 AM6/7/10
to android-...@googlegroups.com
石川さま

ありがとうございます。
確かにAlphaだと機能的には問題ない動作をしました。
ただ、今回は見せ方的にもうちょっとTranslateに拘りたいと考えています。
まぁ、どうしてもダメならAlphaにするか、、、

それで、もう少し試行錯誤してみました。
試した内容としては、AnimationListenerのonAnimationStart()で、
button02.setVisibility(View.VISIBLE);
するように変更してみました。

ActivityのonCreate()に、
anim.setAnimationListener(new AnimListener());
を追加。

button01クリック時のbutton02.setVisibility(View.VISIBLE);を削除

インナークラスAnimListenerを追加。
private class AnimListener implements AnimationListener {
    @Override
    public void onAnimationStart(Animation a) {
        if ( a == anim )
        {
            button02.setVisibility(View.VISIBLE);    // アニメーション開始時にVISIBLEして欲しい。。。
        }
    }
}

結果は、Button02がアニメーションせずいきなりVisibleになりました。
ログ埋め込んでみてみると確かにonAnimationStart()内でVISIBLEにしているハズなのに。。。

もうちょっと粘ってみます。

2010/6/7 Ishikawa Hiromi <ishi...@gmail.com>

awwa

unread,
Jun 7, 2010, 10:59:38 AM6/7/10
to android-...@googlegroups.com
あわです。

自己解決しました。
全然納得いってませんが、とりあえず目的は達成されました。

こんな感じです。
layoutのButton02部分を下記のようにしてLinearLayout継承クラスで囲んで、
SlidingPanelを定義してあげます。
----
<!-- 下にスライドする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:visibility="gone"
>
    <Button 
        android:id="@+id/Button02" 
        android:text="Button02" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        />
</com.awwa.animsample2.SlidingPanel>
----
public class SlidingPanel extends LinearLayout {
public void slide() {
Animation anim;
setVisibility(View.VISIBLE);
anim = AnimationUtils.loadAnimation(context, R.anim.translate);
startAnimation(anim);
}
}
----

以上、ご報告まで。

2010/6/7 awwa <aww...@gmail.com>
Reply all
Reply to author
Forward
0 new messages