Android

[Android] ImageView Animation

HAINIX_X 2020. 6. 3. 13:40
반응형

ImageView의 drawable을 변경할 때 애니메이션 효과를 줄 수 있다.

 

일단 주고 싶은 애니메이션을 res/anim에 저장해두자

 

1. image_fade_in.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="1000"
        android:fromAlpha="0.5"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
</set>

 

2.image_fade_out.xml

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.5" />
</set>

 

 

이후 사용할 activity나 fragment에서 아래 코드를 작성한다.

3. MainActivity.java

private void changeImageWithFadeInAndOut(final ImageView imageView, final int resID) {
	final Animation fadeInAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.image_fade_in);
    Animation fadeOutAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.image_fade_out);
    
    fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
    	@Override
        public void onAnimationStart(Animation animation) { }

		@Override
		public void onAnimationEnd(Animation animation) {
        	imageView.setImageResource(resID);
            imageView.startAnimation(fadeInAnimation);
        }

		@Override
		public void onAnimationRepeat(Animation animation) { }
    }
}

 

 

애니메이션을 주고 싶은 부분에 호출

changeImageWithFadeInAndOut(mImageView, R.drawable.new_image);

 

반응형