본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] bitmap 전환 fade in & fade out sample code

by 돼지왕 왕돼지 2013. 8. 19.
반응형


 안드로이드, bitmap 전환 fade in & fade out sample code

 

[android] bitmap 전환 fade in & fade out sample code


뭐 완벽하며 부드러운 구현은 아니지만, 그래도 image 가 그냥 "확" 바뀌는 어색함을 막기 위해서 fade in, fade out 을 어떻게 이용할 수 있을까 idea 를 제시한다.


public class MainActivity extends Activity {


private int[] resIds = new int[]{ R.drawable.black, R.drawable.blue };

private int index = 0;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViewById( R.id.btn1 ).setOnClickListener( new OnClickListener() {

@Override

public void onClick(View v) {

changeImageWithFadeInAndOut( MainActivity.this, (ImageView)findViewById( R.id.img1 ), resIds[ index ] );

if ( index == 0 )

index = 1;

else

index = 0;

}

});

}


private void changeImageWithFadeInAndOut( Context context, final ImageView imageView, final int resID ){

final Animation fadeInAnimation = AnimationUtils.loadAnimation( context, R.anim.fade_in );

Animation fadeOutAnimation = AnimationUtils.loadAnimation( context, R.anim.fade_out );

fadeOutAnimation.setAnimationListener( new AnimationListener() {

@Override

public void onAnimationStart(Animation animation) { }

@Override

public void onAnimationRepeat(Animation animation) { }

@Override

public void onAnimationEnd(Animation animation) {

imageView.setImageResource( resID );

imageView.startAnimation( fadeInAnimation );

}

});

imageView.startAnimation( fadeOutAnimation );

}

}





<fade_in.xml>
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>


<fade_out.xml>
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />

</set>










반응형

댓글