본문 바로가기

프로그래밍/Android 짜투리 지식

[androd] animation 적용

Interpolator 사용 시 두가지 애니메이션을 혼용하는 방법

1. animationSet.addAnimation

2. setStartOffset

1번의 경우 setInterpolator를 animationSet에다 해줘야됨


package sun.toy.animations;

import sun.toy.R;
import sun.toy.parents.ToyActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;

public class ToyAnimation extends ToyActivity implements OnClickListener {

	private FrameLayout mRoot;
	private Button btnAniamtion;
	private FrameLayout panel;
	private Animation anim;
	private Animation anim2;
	private AnimationSet animSet;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		setContentView(R.layout.animation);
		super.onCreate(savedInstanceState);

		initialize();
	}

	@Override
	protected void initialize() {

		init();
		initView();
		initControl();
	}

	@Override
	protected void init() {
		animSet = new AnimationSet(true);
	}

	@Override
	protected void initView() {

		mRoot = (FrameLayout) findViewById(R.id.animation);
		panel = (FrameLayout) findViewById(R.id.panel);
		btnAniamtion = (Button) findViewById(R.id.btn_animation);

	}

	@Override
	protected void initControl() {

		panel.setOnClickListener(this);
		initAnimation();
	}

	private void initAnimation() {
		animSet.setFillEnabled(true);
		animSet.setFillAfter(true);
		
		anim = new TranslateAnimation(0.0f, 50.0f, 0.0f, 0.0f);
		anim.setDuration(500);
		anim.setStartOffset(0);
		anim.setFillEnabled(true);
		anim.setFillAfter(true);
		anim.setInterpolator(AnimationUtils.loadInterpolator(this,
				android.R.anim.decelerate_interpolator)); 

		anim.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {

				panel.startAnimation(anim2);
			}
		});
		// panel.startAnimation(anim);
		//
		anim2 = new TranslateAnimation(50.0f, 0.0f, 0.0f, 0.0f);
		anim2.setDuration(1000);
		anim2.setStartOffset(0);
		anim2.setRepeatMode(Animation.RESTART);
		anim2.setFillEnabled(true);
		anim2.setFillAfter(true);
		anim2.setInterpolator(AnimationUtils.loadInterpolator(this,
				android.R.anim.bounce_interpolator));
		//
		// panel.startAnimation(anim2);
		
		animSet.addAnimation(anim);
		animSet.addAnimation(anim2); 
	}

	@Override
	public void onClick(View v) {

		panel.startAnimation(anim);
		
		panel.invalidate();
	}
}