Tips

PropertyAnimationを用いた簡単なAndroidサンプルアプリ

PropertyAnimationを使ってみました。

※はじめに
この記事はAndroidアプリの開発が、初心者であるという方のための記事です。
そのため、なるべく複雑な説明は避け、コピー&ペイストですぐに動くものをご紹介します。
JavaやAndroidを理解されている方で細かい説明が必要な方は、当ブログ内の連載記事である「Android Tips」をご覧ください。

さて、突然ですが今回はボタンを動かしてみます。笑
上のボタンを押すと、下のボタンが右側に移動し、消えます。

アプリ実行図

1

2

3


サンプルコード

  • activity_main.xml(画面レイアウトファイル)
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="15dp"
        android:orientation="vertical"
        tools:context=".MainActivity" >
     
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Touch Me." />
     
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello!!" />
     
    </LinearLayout>


  • MainActivity.java(Activityファイル)
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    package com.example.propertyanimation2;
     
    import android.animation.Animator;
    import android.animation.Animator.AnimatorListener;
    import android.animation.ObjectAnimator;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
     
    public class MainActivity extends Activity implements OnClickListener,
            AnimatorListener {
     
        private Button b1;
        private Button b2;
        private ObjectAnimator oa;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
     
        @Override
        protected void onResume() {
            super.onResume();
     
            b1 = (Button) findViewById(R.id.button1);
            b1.setOnClickListener(this);
     
            b2 = (Button) findViewById(R.id.button2);
     
            // 第一引数:Object target:
            // 第二引数:String propertyName
            // 第三引数:float... values(可変長配列)
            oa = ObjectAnimator.ofFloat(b2, "x", 0f, 400f);
     
            // アニメーションの再生時間を設定
            oa.setDuration(4000);
     
            // アニメーション中のイベントを受け取るリスナーを追加
            oa.addListener(this);
        }
     
        @Override
        public void onClick(View v) {
     
            // アニメーションの開始
            oa.start();
        }
     
        // アニメーションの開始時に呼び出されるコールバックメソッド
        @Override
        public void onAnimationStart(Animator animation) {
        }
     
        // アニメーションのキャンセル時に呼び出されるコールバックメソッド
        @Override
        public void onAnimationCancel(Animator animation) {
        }
     
        // アニメーションの終了時に呼び出されるメソッド
        @Override
        public void onAnimationEnd(Animator animation) {
     
            // レイアウトを非表示
            b2.setVisibility(View.GONE);
        }
     
        // アニメーションが繰り返された時に呼び出されるコールバックメソッド
        @Override
        public void onAnimationRepeat(Animator animation) {
        }
     
    }


以上でOKです。

Androidアプリ開発の必須知識!JAVAプログラミングを学べる連載リンク

はじめてのJAVA 連載

Recent News

Recent Tips

Tag Search