.footnote[출처: https://developer.android.com/guide/topics/graphics/overview.html]
<animation-list>의 루트 요소 하위에 프레임을 위한 리소스와 프레임 기간을 정의하는 <item> 요소 정의
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true"|"false"] >
<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer" />
</animation-list>
예제 (res/drawable/frame_anim.xml)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" > [한번만 애니메이션 수행]
<item android:drawable="@drawable/countdown3" android:duration="500" />
<item android:drawable="@drawable/countdown2" android:duration="500" />
<item android:drawable="@drawable/countdown1" android:duration="500" />
<item android:drawable="@drawable/countdown0" android:duration="500" />
</animation-list>
ImageView mCountDown = (ImageView) findViewById(R.id.countdown);
private void startCountDownFrameAnimation() {
/*1*/ mCountDown.setBackgroundResource(R.drawable.frame_anim);
/*2*/ AnimationDrawable countdownAnim =
(AnimationDrawable)mCountDown.getBackground();
/*3*/ countdownAnim.start();
}
트윈 애니메이션 XML 리소스 형식
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"
android:shareInterpolator=["true" | "false"] >
<alpha
android:fromAlpha="float"
android:toAlpha="float" />
<scale
android:fromXScale="float"
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float" />
<translate
android:fromXDelta="float"
android:toXDelta="float"
android:fromYDelta="float"
android:toYDelta="float" />
<rotate
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float" />
</set>
트윈 애니메이션 XML 리소스 정의 예제 (res/anim/rocket.xml)
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true"
android:fillAfter="true"> [애니메이션 종료후 초기 상태로 돌아가지 않음]
<scale
android:startOffset="2000" android:duration="2000" |2초 후에 시작해서 2초동안 |
android:fromXScale="1" android:toXScale="0.1" |뷰의 중심을 기준으로 축소 됨|
android:fromYScale="1" android:toYScale="0.1"
android:pivotX="50%" android:pivotY="50%"/>
<translate |2초 후에 시작해서 2초동안 |
android:startOffset="2000" android:duration="2000" |뷰의 0픽셀 위치에서 부모 뷰 높이의 |
android:fromYDelta="0" android:toYDelta="-100%p" /> | -100%에 해당하는 크기의 위치 |
|(위로 부모 뷰 높이만큼 이동) |
</set>
ImageView mRocket = (ImageView) findViewById(R.id.rocket);
private void startRocketTweenAnimation() {
Animation rocket_anim = AnimationUtils.loadAnimation(this, R.anim.rocket);
mRocket.startAnimation(rocket_anim);
}
.footnote[참고자료 https://developer.android.com/guide/topics/graphics/prop-animation.html]
/*1*/ ValueAnimator positionAnimator = ValueAnimator.ofFloat(0,-mScreenHeight);
/*2*/ positionAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
positionAnimator.setDuration(2000);
positionAnimator.setStartDely(2000);
/*3*/ positionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator valueAnimator){
float value = (float) valueAnimator.getAnimatedValue(); // 애니메이션값 획득
mRocket.setTranslationY(value); // 적용대상에 값 적용
}
});
/*4*/ positionAnimator.start();
/*1*/ ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(
mRocket, // 애니메이션 적용 타겟객체
"translationY", // 변화시킬 프로퍼티 (Y 좌표 값)
0, -mScreenHeight); // 값의 범위
/*2*/ positionAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); // 가속후 감속
positionAnimator.setDuration(2000); // 애니메이션 기간
positionAnimator.setStartDely(2000); // 애니메이션 시작 시점
/*3*/ positionAnimator.start();
ObjectAnimator positionAnimator = ObjectAnimator.ofFloat(mRocket, "translationY",
0, -mScreenHeight);
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mRocket,"scaleX",1,0.1f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(mRocket,"scaleY",1,0.1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(positionAnimator,scaleXAnimator,scaleYAnimator);
...
animatorSet.start();
Animation.AnimationListener animationListener = new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {…}
public void onAnimationEnd(Animation animation) {
finish();
startActivity(new Intent(getApplicationContext(), SecondActivity.class));
}
public void onAnimationRepeat(Animation animation) {…}
};
private void startFireTweenAnimation() {
Animation fire_anim = AnimationUtils.loadAnimation(this, R.anim.fire);
mFirework.startAnimation(fire_anim);
fire_anim.setAnimationListener(animationListener);
}
https://github.com/kwanulee/Android/blob/master/examples/AnimationTest/app/src/main/java/com/example/kwanwoo/animationtest/MainActivity.java#L144-L161 https://github.com/kwanulee/Android/blob/master/examples/AnimationTest/app/src/main/java/com/example/kwanwoo/animationtest/MainActivity.java#L73-L77