MENU
コンテンツ再構築中

Android: ImageView をアニメーションさせる(Property Animation)

Android アプリを開発時にアニメーションが必要になったので、そのときのメモ。
今回は Android3.X 系 以降で使用できる Property Animation のサンプルコードを掲載しました。

INDEX

ImageView をアニメーションさせる(Property Animation)

target に対象の ImageView、duration にアニメーション時間(ミリ秒)、from(変更前の値)、to(変更後の値)を渡します。

透過アニメーション

[code]
private void animateAlpha( ImageView target, Long duration, Float from, Float to ) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat( target, “alpha”, from, to );
objectAnimator.setDuration( duration );
objectAnimator.start();
}
[/code]

[code]
// myView の Alpha プロパティを 3秒かけて 0% から 100% に変更
animateAlpha(myView, 300, 0f, 1f);
[/code]

移動アニメーション

[code]
private void animateTranslationY( ImageView target, Long duration, Float from, Float to ) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat( target, “translationY”, 0f, 200f );
objectAnimator.setDuration( duration );
objectAnimator.start();
}
[/code]

[code]
// myView の translationY プロパティを 3秒かけて 0f から 100f に変更
animateTranslationY(myView, 300, 0f, 100f);
[/code]

回転アニメーション

[code]
private void animateRotation(( ImageView target, Long duration, Float from, Float to ) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat( target, “rotation”, from, to );
objectAnimator.setDuration( duration );
objectAnimator.start();
}
[/code]

[code]
// myView の translationY プロパティを 3秒かけて 0f から 360f に変更
animateRotation((myView, 300, 0f, 360f);
[/code]

まとめ

Android のアニメーションはいくつか種類があるみたいですが、互換性や汎用性を考慮すれば、オブジェクトそのもののプロパティが変更できる property animation が使いやすいみたいです。
今後は iOS だけではなく Android の記事も追加していきたいと思います。

この記事がみなさんのお役に立ちましたら、下記「Share it」よりブックマークやSNSで共有していただければ幸いです。

Please share it!
  • URLをコピーしました!
  • URLをコピーしました!
INDEX