LoginSignup
7
7

More than 5 years have passed since last update.

ListViewにAnimationを設定する方法

Posted at

今回は、AndroidでListViewにアニメーションを付与する方法を載せます。

まずはじめに、アニメーション動作を作成しましょう。

アニメーション動作の定義は、res/animフォルダ 下に作成したxmlに行います。
プロジェクト作成時にはanimフォルダが無いと思いますので、マニュアルで作成してください。
animフォルダ内に、item_motion.xmlを作成します。

item_motion.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="-100%"
        android:fromYDelta="50%"
        android:toXDelta="0"
        android:toYDelta="0"
        android:duration="700"  />
</set>

fromXDelta、toXDeltaで横の動きを、fromXDelta、toXDeltaで縦の動きを定義できます。
durationで、動作時間を設定できます。

次に、このアニメーションをListViewに追加したいと思います。

ArrayAdapterを継承した中で、アニメーションを付けたいlayoutをinflateしたViewオブジェクトに対して、下記のようにAnimation オブジェクトを作成したものをstartAnimationで与えます。

filename
// create a new view
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.message_item, viewGroup, false);

//Animation objectを作成
Animation anim = AnimationUtils.loadAnimation(viewGroup.getContext(), R.anim.item_motion);


v.startAnimation(anim);

以上です。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7