Androidアニメーション(超詳細)
転入先http://www.360doc.com/content/13/0102/22/6541311_257754535.shtml
Animations
一、Animations紹介
Animationsはandroid UIインタフェースのアニメーション効果を実現するAPIであり、Animationsは一連のアニメーション効果を提供し、回転、スケール、フェードアウトなどを行うことができ、これらの効果はほとんどのコントロールに適用することができる.
二、Animationsの分類
Animationsは全体的に2つの種類に分けることができます.
1.Tweened Animations:この種のAnimationsは、回転、移動、伸張、フェードアウトなどの効果を提供します.Alpha-フェードアウト、Scale-ズーム効果、Rotate-回転、Translate-移動効果.
2.Frame-by-frame Animations:このようなAnimationsは、指定された時間に1つずつ間欠的に表示できるDrawableシーケンスを作成することができます.
三、Animationsの使用方法(コードで使用)
Animations extends Object implements Cloneable
TweenedAnimationsを使用するには:
1.AnimationSetオブジェクト(Animationサブクラス)を作成します.
2.適切なAnimationオブジェクトを作成する必要があります.
3.プロジェクトのニーズをさらに高め、Animationオブジェクトに相応のデータを設定する.
4.AnimatinオブジェクトをAnimationSetオブジェクトに追加する.
5.コントロールオブジェクトを使用してAnimationSetの実行を開始します.
Tweened Animationsの分類1、Alpha:フェードアウト効果2、Scale:スケーリング効果3、Rotate:回転効果4、Translate:移動効果
Animationの4つのサブクラス:AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
四、具体的な実現
2
、
.java
ファイル
Tween Animationsの一般的な方法
1、setDuration(long durationMills)アニメーション持続時間(単位:ミリ秒)の設定2、setFillAfter(Boolean fillAfter)fillAfterの値がtrueの場合、アニメーション実行後、コントロールは実行終了の状態3、setFillBefore(Boolean fillBefore)fillBeforeの値がtrueの場合、アニメーション実行後、コントロールは、アニメーション実行前の状態4、setStartOffSet(long startOffSet)アニメーション実行前の待ち時間5、setRepeatCount(int repeatCount)アニメーションの繰り返し実行回数を設定します
コードの中でAnimationsを使うと簡単にデバッグ、実行できますが、コードの再利用性が悪く、重複コードが多いです.同様にxmlファイルにAnimationsを構成することができ、メンテナンス性が高くなりますが、デバッグは容易ではありません.
一、xmlでAnimationsステップを使用する
1.resフォルダの下にanimフォルダを作成します.
2.xmlファイルを作成し、まずsetラベルを追加し、ラベルを次のように変更します.
3.このラベルにrotate,alpha,scaleまたはtranslateラベルを加える.
4.コードにAnimationUtilsを使用してxmlファイルをロードし、Animationオブジェクトを生成します.AnimationはAnimationSetのサブクラスであるため,アップシフトし,Animationオブジェクトで受信する.
二、具体的な実現
1、 alpha.xml
2、 rotate.xml
3、 scale.xml
4、 translate.xml
5、 .JAvaファイル
AnimationSetの具体的な使い方
1.AnimationSetはAnimationのサブクラスです.
2.AnimationSetには一連のAnimationが含まれています.
3.AnimationSetに対していくつかのAnimationの一般的な属性(startOffset,durationなど)を設定し、AnimationSetに含まれるAnimation統合を行うことができる.
例:1つのAnimationSetに2つのAnimationがあり、効果が重なっている
1つ目の方法:
doubleani.xml
.javaファイル
2つ目の方法:
.javaファイル
Interpolatorの具体的な使い方
Interpolatorはアニメーションの変化の速度を定義して、Animationsのフレームワークの中でいくつかのInterpolatorを定義しました
?AccelerateDecelerateInterpolator:アニメーションの開始と終了で速度の変化が遅く、中間で速度が速い.
?AccelerateInterpolator:アニメーション開始時の速度変化が遅くなり、加速し始める
?CycleInterpolator:アニメーションループが特定の回数再生され、速度が正弦波曲線に沿って変化します.
?DeselerateInterpolator:アニメーションの開始時に速度の変化が遅くなり、減速を開始します
?LinearInterpolator:アニメーションを均一な速度で変更する
次のように分類されます.
1、setラベルに
2、1つのsetラベルに複数のアニメーション効果が含まれている場合、これらのアニメーション効果を1つのInterpolatorで共有したい場合.
3、interpolatorを共有したくない場合はandroid:shareInterpolator="true"を設定し、各アニメーション効果にinterpolatorを追加する必要があります.
4、コード上で共有インターポレータを設定する場合は、AnimationSetでインターポレータを設定できます.
5、共有interpolatorを設定しない場合、各Animationオブジェクトの上にinterpolatorを設定できます
.
Frame-By-Frame Animationsの使い方
Frame-by-Frame Animationsは、1フレーム1フレームの形式でアニメーション効果を表示します.フィルム撮影に似た手法.
main.xml
3、anim.xml
4、.JAvaファイル
LayoutAnimationsController
1、LayoutAnimationsControllerとは
LayoutAnimationsControllerは、複数のコントロールを順番に1つずつ表示するために使用することができる.
1)LayoutAnimationsControllerは、layoutのコントロール、またはView Groupのコントロールに統一されたアニメーション効果を設定するために使用されます.
2)コントロールごとに同じアニメーション効果が得られます.
3)コントロールのアニメーション効果は異なる時間に表示できます.
4)LayoutAnimationsControllerはxmlファイルに設定でき,コードに設定できる.
2、xmlでLayoutAnimationControllerを使う
1)res/animフォルダの下にリスト_を作成するanim_layout.xmlファイル:
android:delay-アニメーション間隔時間;サブクラスアニメーションの時間間隔(遅延)70%は、「1.2」などの浮動小数点数であってもよい
android:animationOrder-アニメーション実行の順序付け(normal:順序、random:ランダム、reverse:逆方向表示)
android:animation–アニメーション効果ファイルを参照
2)list_の作成anim.xmlファイル、アニメーション効果の設定
3)レイアウトファイルmain.xmlではListViewに次の構成を追加します.
4)プログラム構造
5)list_anim_layout.xml
6)list_anim.xml
7)main.xml
8)item.xml
9)javaファイル
メモ:アニメーション効果全体をLinerLayoutに設定するには、次のように設定します.
3、コードにLayoutAnimationControllerを使う
1)mainを削除する.xmlのandroid:layoutAnimation="@anim/list_anim_layout"/>
2)Animationオブジェクトを作成する:xmlファイルをマウントするか、または直接Animationの構築方法を使用してAnimationオブジェクトを作成することができます.
3)LayoutAnimationControllerオブジェクトの作成:
4)コントロールの表示順序及び遅延時間の設定
5)ListViewのLayoutAnimationControlプロパティを設定するには:
完全なコード:
AnimationListener
1、AnimationListenerとは
1).AnimationListenerは、アニメーション実行の各段階で通知され、対応するメソッドを呼び出すリスナーです.
2).AnimationListenerは主に以下の3つの方法を含む.
n・onAnimationEnd-アニメーション終了時に呼び出す
n・onAnimationRepeat(Animation animation)-アニメーションが繰り返されると呼び出されます
n・onAniamtionStart(Animation animation)-アニメーションが開始されると呼び出されます
2、具体的な実現
1)main.xml
2).JAvaファイル
3、まとめて
Activityでは、次のようにコントロールを動的に追加および削除できます.
1)そのLayoutを取る
2)追加時にオブジェクトを作成してから追加
3)削除する場合は、そのまま削除します.
Animations
一、Animations紹介
Animationsはandroid UIインタフェースのアニメーション効果を実現するAPIであり、Animationsは一連のアニメーション効果を提供し、回転、スケール、フェードアウトなどを行うことができ、これらの効果はほとんどのコントロールに適用することができる.
二、Animationsの分類
Animationsは全体的に2つの種類に分けることができます.
1.Tweened Animations:この種のAnimationsは、回転、移動、伸張、フェードアウトなどの効果を提供します.Alpha-フェードアウト、Scale-ズーム効果、Rotate-回転、Translate-移動効果.
2.Frame-by-frame Animations:このようなAnimationsは、指定された時間に1つずつ間欠的に表示できるDrawableシーケンスを作成することができます.
三、Animationsの使用方法(コードで使用)
Animations extends Object implements Cloneable
TweenedAnimationsを使用するには:
1.AnimationSetオブジェクト(Animationサブクラス)を作成します.
2.適切なAnimationオブジェクトを作成する必要があります.
3.プロジェクトのニーズをさらに高め、Animationオブジェクトに相応のデータを設定する.
4.AnimatinオブジェクトをAnimationSetオブジェクトに追加する.
5.コントロールオブジェクトを使用してAnimationSetの実行を開始します.
Tweened Animationsの分類1、Alpha:フェードアウト効果2、Scale:スケーリング効果3、Rotate:回転効果4、Translate:移動効果
Animationの4つのサブクラス:AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
四、具体的な実現
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/rotateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<Button
android:id="@+id/scaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<Button
android:id="@+id/alphaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
<Button
android:id="@+id/translateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/an" />
</LinearLayout>
</LinearLayout>
2
、
.java
ファイル
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
importandroid.view.animation.AnimationSet;
importandroid.view.animation.RotateAnimation;
importandroid.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
importandroid.widget.Button;
importandroid.widget.ImageView;
public class Animation1Activity extends Activity {
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
private ImageView image = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rotateButton = (Button)findViewById(R.id.rotateButton);
scaleButton = (Button)findViewById(R.id.scaleButton);
alphaButton = (Button)findViewById(R.id.alphaButton);
translateButton = (Button)findViewById(R.id.translateButton);
image = (ImageView)findViewById(R.id.image);
rotateButton.setOnClickListener(newRotateButtonListener());
scaleButton.setOnClickListener(newScaleButtonListener());
alphaButton.setOnClickListener(newAlphaButtonListener());
translateButton.setOnClickListener(
new TranslateButtonListener());
}
class AlphaButtonListener implementsOnClickListener{
public void onClick(View v) {
// AnimationSet , Boolean ,
//true Animation interpolator,false
AnimationSet animationSet = new AnimationSet(true);
// AlphaAnimation , ,
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//
alphaAnimation.setDuration(500);
// alphaAnimation AnimationSet
animationSet.addAnimation(alphaAnimation);
// ImageView startAnimation
image.startAnimation(animationSet);
}
}
class RotateButtonListener implementsOnClickListener{
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
// 1:
// 2:
// 4
// 3: x , ABSOLUT 、RELATIVE_TO_SELF 、RELATIVE_TO_PARENT
// 4:x ,0.5f x
// 5: y
// 6:y ,0.5f x
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
}
class ScaleButtonListener implementsOnClickListener{
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
// 1:x
// 2:x
// 3:y
// 4:y
// 5: x
// 6:x ,0.5f x
// 7: y
// 8:y ,0.5f x
ScaleAnimation scaleAnimation = new ScaleAnimation(
0, 0.1f,0,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
}
}
class TranslateButtonListener implementsOnClickListener{
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
// 1~2:x
// 3~4:y
// 5~6:x
// 7~8:x
TranslateAnimation translateAnimation =
new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
}
}
}
Tween Animationsの一般的な方法
1、setDuration(long durationMills)アニメーション持続時間(単位:ミリ秒)の設定2、setFillAfter(Boolean fillAfter)fillAfterの値がtrueの場合、アニメーション実行後、コントロールは実行終了の状態3、setFillBefore(Boolean fillBefore)fillBeforeの値がtrueの場合、アニメーション実行後、コントロールは、アニメーション実行前の状態4、setStartOffSet(long startOffSet)アニメーション実行前の待ち時間5、setRepeatCount(int repeatCount)アニメーションの繰り返し実行回数を設定します
コードの中でAnimationsを使うと簡単にデバッグ、実行できますが、コードの再利用性が悪く、重複コードが多いです.同様にxmlファイルにAnimationsを構成することができ、メンテナンス性が高くなりますが、デバッグは容易ではありません.
一、xmlでAnimationsステップを使用する
1.resフォルダの下にanimフォルダを作成します.
2.xmlファイルを作成し、まずsetラベルを追加し、ラベルを次のように変更します.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
</set>
3.このラベルにrotate,alpha,scaleまたはtranslateラベルを加える.
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
4.コードにAnimationUtilsを使用してxmlファイルをロードし、Animationオブジェクトを生成します.AnimationはAnimationSetのサブクラスであるため,アップシフトし,Animationオブジェクトで受信する.
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.alpha);
//
image.startAnimation(animation);
二、具体的な実現
1、 alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!-- fromAlpha toAlpha -->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
</set>
2、 rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!--
fromDegrees:
toDegrees: ,+
pivotX: x
1) "50",
2) "50%",
3) "50%p",
pivotY: y
-->
<rotate
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
3、 scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!--
x
x
y
y
-->
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
4、 translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!--
x
x
y
y
-->
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"/>
</set
5、 .JAvaファイル
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
import android.view.animation.Animation;
importandroid.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class Animation1Activity extends Activity {
private Button rotateButton = null;
private Button scaleButton = null;
private Button alphaButton = null;
private Button translateButton = null;
private ImageView image = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rotateButton = (Button) findViewById(R.id.rotateButton);
scaleButton = (Button) findViewById(R.id.scaleButton);
alphaButton = (Button) findViewById(R.id.alphaButton);
translateButton = (Button) findViewById(R.id.translateButton);
image = (ImageView) findViewById(R.id.image);
rotateButton.setOnClickListener(newRotateButtonListener());
scaleButton.setOnClickListener(newScaleButtonListener());
alphaButton.setOnClickListener(newAlphaButtonListener());
translateButton.setOnClickListener(newTranslateButtonListener());
}
class AlphaButtonListener implementsOnClickListener {
public void onClick(View v) {
// AnimationUtils
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.alpha);
//
image.startAnimation(animation);
}
}
class RotateButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.rotate);
image.startAnimation(animation);
}
}
class ScaleButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.scale);
image.startAnimation(animation);
}
}
class TranslateButtonListener implementsOnClickListener {
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.translate);
image.startAnimation(animation);
}
}
}
AnimationSetの具体的な使い方
1.AnimationSetはAnimationのサブクラスです.
2.AnimationSetには一連のAnimationが含まれています.
3.AnimationSetに対していくつかのAnimationの一般的な属性(startOffset,durationなど)を設定し、AnimationSetに含まれるAnimation統合を行うことができる.
例:1つのAnimationSetに2つのAnimationがあり、効果が重なっている
1つ目の方法:
doubleani.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<!-- fromAlpha toAlpha -->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
<translate
android:fromXDelta="0%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%"
android:duration="2000"/>
</set>
.javaファイル
classDoubleButtonListener implements OnClickListener {
public void onClick(View v) {
// AnimationUtils
Animation animation = AnimationUtils.loadAnimation(
Animation2Activity.this, R.anim. doubleani);
//
image.startAnimation(animation);
}
}
2つ目の方法:
.javaファイル
classDoubleButtonListener implements OnClickListener {
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
}
}
Interpolatorの具体的な使い方
Interpolatorはアニメーションの変化の速度を定義して、Animationsのフレームワークの中でいくつかのInterpolatorを定義しました
?AccelerateDecelerateInterpolator:アニメーションの開始と終了で速度の変化が遅く、中間で速度が速い.
?AccelerateInterpolator:アニメーション開始時の速度変化が遅くなり、加速し始める
?CycleInterpolator:アニメーションループが特定の回数再生され、速度が正弦波曲線に沿って変化します.
?DeselerateInterpolator:アニメーションの開始時に速度の変化が遅くなり、減速を開始します
?LinearInterpolator:アニメーションを均一な速度で変更する
次のように分類されます.
1、setラベルに
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"/>
2、1つのsetラベルに複数のアニメーション効果が含まれている場合、これらのアニメーション効果を1つのInterpolatorで共有したい場合.
android:shareInterpolator="true"
3、interpolatorを共有したくない場合はandroid:shareInterpolator="true"を設定し、各アニメーション効果にinterpolatorを追加する必要があります.
<alpha
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
4、コード上で共有インターポレータを設定する場合は、AnimationSetでインターポレータを設定できます.
AnimationSet animationSet = newAnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
5、共有interpolatorを設定しない場合、各Animationオブジェクトの上にinterpolatorを設定できます
.
AnimationSet animationSet = newAnimationSet(false);
alphaAnimation.setInterpolator(new AccelerateInterpolator());
rotateAnimation.setInterpolator(new DecelerateInterpolator());
Frame-By-Frame Animationsの使い方
Frame-by-Frame Animationsは、1フレーム1フレームの形式でアニメーション効果を表示します.フィルム撮影に似た手法.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</LinearLayout>
</LinearLayout>
3、anim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/a_01" android:duration="50"/>
<item android:drawable="@drawable/a_02" android:duration="50"/>
<item android:drawable="@drawable/a_03" android:duration="50"/>
<item android:drawable="@drawable/a_04" android:duration="50"/>
<item android:drawable="@drawable/a_05" android:duration="50"/>
<item android:drawable="@drawable/a_06" android:duration="50"/>
</animation-list>
4、.JAvaファイル
importandroid.app.Activity;
importandroid.graphics.drawable.AnimationDrawable;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.ImageView;
public class AnimationsActivity extends Activity {
private Button button = null;
private ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.image);
button.setOnClickListener(newButtonListener());
}
class ButtonListener implementsOnClickListener{
public void onClick(View v) {
imageView.setBackgroundResource(R.anim.anim);
AnimationDrawable animationDrawable = (AnimationDrawable)
imageView.getBackground();
animationDrawable.start();
}
}
}
LayoutAnimationsController
1、LayoutAnimationsControllerとは
LayoutAnimationsControllerは、複数のコントロールを順番に1つずつ表示するために使用することができる.
1)LayoutAnimationsControllerは、layoutのコントロール、またはView Groupのコントロールに統一されたアニメーション効果を設定するために使用されます.
2)コントロールごとに同じアニメーション効果が得られます.
3)コントロールのアニメーション効果は異なる時間に表示できます.
4)LayoutAnimationsControllerはxmlファイルに設定でき,コードに設定できる.
2、xmlでLayoutAnimationControllerを使う
1)res/animフォルダの下にリスト_を作成するanim_layout.xmlファイル:
android:delay-アニメーション間隔時間;サブクラスアニメーションの時間間隔(遅延)70%は、「1.2」などの浮動小数点数であってもよい
android:animationOrder-アニメーション実行の順序付け(normal:順序、random:ランダム、reverse:逆方向表示)
android:animation–アニメーション効果ファイルを参照
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"/>
2)list_の作成anim.xmlファイル、アニメーション効果の設定
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"/>
</set>
3)レイアウトファイルmain.xmlではListViewに次の構成を追加します.
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"/>
4)プログラム構造
5)list_anim_layout.xml
<layoutAnimation
xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="0.5"
android:animationOrder="normal"
android:animation="@anim/list_anim"/>
6)list_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"/>
</set>
7)main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:layoutAnimation="@anim/list_anim_layout"/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" "/>
</LinearLayout>
8)item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip"
android:paddingBottom="1dip">
<TextView android:id="@+id/name"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="5pt"
android:singleLine="true" />
<TextView android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="5pt"
android:singleLine="true"/>
</LinearLayout>
9)javaファイル
public class Animation2Activity extendsListActivity {
private Button button = null;
private ListView listView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = getListView();
button = (Button)findViewById(R.id.button);
button.setOnClickListener(newButtonListener());
}
private ListAdapter createListAdapter() {
List<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
HashMap<String,String> m1 = new HashMap<String,String>();
m1.put("name", "bauble");
m1.put("sex", "male");
HashMap<String,String> m2 = new HashMap<String,String>();
m2.put("name", "Allorry");
m2.put("sex", "male");
HashMap<String,String> m3 = new HashMap<String,String>();
m3.put("name", "Allotory");
m3.put("sex", "male");
HashMap<String,String> m4 = new HashMap<String,String>();
m4.put("name", "boolbe");
m4.put("sex", "male");
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
SimpleAdapter simpleAdapter = new SimpleAdapter(
this,list,R.layout.item,new String[]{"name","sex"},
new int[]{R.id.name,R.id.sex});
return simpleAdapter;
}
private class ButtonListener implementsOnClickListener{
public void onClick(View v) {
listView.setAdapter(createListAdapter());
}
}
}
メモ:アニメーション効果全体をLinerLayoutに設定するには、次のように設定します.
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layoutAnimation="@anim/list_anim_layout"
>
3、コードにLayoutAnimationControllerを使う
1)mainを削除する.xmlのandroid:layoutAnimation="@anim/list_anim_layout"/>
2)Animationオブジェクトを作成する:xmlファイルをマウントするか、または直接Animationの構築方法を使用してAnimationオブジェクトを作成することができます.
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this, R.anim.list_anim);
3)LayoutAnimationControllerオブジェクトの作成:
LayoutAnimationController controller = new LayoutAnimationController(animation);
4)コントロールの表示順序及び遅延時間の設定
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
5)ListViewのLayoutAnimationControlプロパティを設定するには:
listView.setLayoutAnimation(controller);
完全なコード:
private class ButtonListener implementsOnClickListener {
public void onClick(View v) {
listView.setAdapter(createListAdapter());
Animation animation = (Animation) AnimationUtils.loadAnimation(
Animation2Activity.this, R.anim.list_anim);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
controller.setDelay(0.5f);
listView.setLayoutAnimation(controller);
}
}
AnimationListener
1、AnimationListenerとは
1).AnimationListenerは、アニメーション実行の各段階で通知され、対応するメソッドを呼び出すリスナーです.
2).AnimationListenerは主に以下の3つの方法を含む.
n・onAnimationEnd-アニメーション終了時に呼び出す
n・onAnimationRepeat(Animation animation)-アニメーションが繰り返されると呼び出されます
n・onAniamtionStart(Animation animation)-アニメーションが開始されると呼び出されます
2、具体的な実現
1)main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/addButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text=" " />
<Button android:id="@+id/deleteButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/addButton"
android:text=" " />
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="100dip"
android:src="@drawable/an" />
</RelativeLayout>
2).JAvaファイル
public class Animation2Activity extends Activity {
private Button addButton = null;
private Button deleteButton = null;
private ImageView imageView = null;
private ViewGroup viewGroup = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton = (Button)findViewById(R.id.addButton);
deleteButton = (Button)findViewById(R.id.deleteButton);
imageView = (ImageView)findViewById(R.id.image);
//LinearLayout
viewGroup = (ViewGroup)findViewById(R.id.layout);
addButton.setOnClickListener(newAddButtonListener());
deleteButton.setOnClickListener(newDeleteButtonListener());
}
private class AddButtonListener implements OnClickListener{
public void onClick(View v) {
//
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
// ImageView
ImageView newImageView = new ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newImageView.startAnimation(animation);
}
}
private class DeleteButtonListener implements OnClickListener{
public void onClick(View v) {
//
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
// Aniamtion
animation.setAnimationListener(
new RemoveAnimationListener());
imageView.startAnimation(animation);
}
}
private class RemoveAnimationListener implements AnimationListener{
// remove
public void onAnimationEnd(Animation animation) {
System.out.println("onAnimationEnd");
viewGroup.removeView(imageView);
}
public void onAnimationRepeat(Animation animation) {
System.out.println("onAnimationRepeat");
}
public void onAnimationStart(Animation animation) {
System.out.println("onAnimationStart");
}
}
}
3、まとめて
Activityでは、次のようにコントロールを動的に追加および削除できます.
1)そのLayoutを取る
viewGroup = (ViewGroup)findViewById(R.id.layout);
2)追加時にオブジェクトを作成してから追加
ImageView newImageView = new ImageView(
Animation2Activity.this);
newImageView.setImageResource(R.drawable.an);
viewGroup.addView(newImageView,
new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
3)削除する場合は、そのまま削除します.
viewGroup.removeView(imageView);