프로그램/Android

안드로이드 애니메이션 코드[Android Animation]

잡식성초보 2017. 9. 12. 09:00

두번째 안드로이드 애니메이션 작업에 대한 글입니다.


이전 글에서 Xml 이용한 안드로이드 애니메이션 처리방법이었습니다. 이번에는 오직 코드로 작업하는 방법에 대해서 바로 설명 드리겠습니다.


Translate TranslateAnimation 이란 함수를 이용하고 Alpha AlphaAnimation 함수를 이용합니다. 샘플 코드로 설명드리겠습니다.


TranslateAnimation animation = new TranslateAnimation(0, fromX , 0, fromY);
animation.setDuration(3000);
animation.setFillAfter(false);
animation.setFillEnabled(true);
animation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});
btn2.startAnimation(animation);


Xml 코드와 별반 다르지 않습니다. AlphaAnimation 또한 아래처럼 코드로 간단하게 작업을 할수 있습니다.


AlphaAnimation anim = new AlphaAnimation(0.0f, 0.95f);
anim.setDuration(500);
rl_001.startAnimation(anim);


2가지 애니메이션을 동시에 실행시킨다면 AnimationSet을 이용하서셔 작업하셔도 됩니다.  물론 각각 애니메이션을 만들어서 실행시켜도 됩니다. AnimationSet적용법입니다.


AnimationSet set = new AnimationSet(true);

AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setDuration(500);

TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -50);
anim.setDuration(1000);

set.addAnimation(alpha);
set.addAnimation(anim);

set.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

testview.startAnimation(set);


그리고 이전글에서 말씀드렸던대로 뷰 이동후에 포인트를 변경해서 작업해 주셔야 한다고 말씀드렸는데요. 애니메이션 리스너에서 애니메이션이 끝났을 경우 그 view layout을 다시 잡아주시면 되겠습니다. 샘플 코드 풀로 붙여드릴테니 한번 작업해 보시면 좋을거같습니다.


Xml파일

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="2">

        <Button
            android:id="@+id/btn1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="60dp" />


    </LinearLayout>

    <ImageView
        android:id="@+id/btn2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@mipmap/ic_launcher"/>


</RelativeLayout>


Activity파일


public class AnimationActivity extends Activity implements View.OnClickListener {

    private String TAG = "AnimationActivity";

    private LinearLayout ll1;
    private Button btn1;
    private ImageView btn2;

    private float screenWidth;
    private float screenHeight;

    private float fromX = 0, fromY = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);

        init();
    }

    private void init(){



        ll1 = findViewById(R.id.ll1);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);

        Display display = getWindowManager().getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);

        Log.e(TAG, "Width : " + point.x + " , Height : " + point.y);

        screenWidth = point.x;
        screenHeight = point.y;
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.btn1 :

                fromY = screenHeight - ll1.getHeight() - btn2.getHeight();
                Log.e(TAG, "fromX : " + fromX + ",  fromY : " +fromY);

                TranslateAnimation animation = new TranslateAnimation(0, fromX , 0, fromY);
                animation.setDuration(3000);
                animation.setFillAfter(false);
                animation.setFillEnabled(true);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                        int[] pos = {btn2.getLeft(), btn2.getTop() + (int)fromY, btn2.getRight(), btn2.getBottom() + (int)fromY };
                        btn2.layout(pos[0], pos[1], pos[2], pos[3]);
                        Log.e(TAG, " 1: " + btn2.getLeft() + " , 2 : " + btn2.getTop() + "  , 3 : "  + btn2.getRight() + "  , 4 : " + btn2.getBottom());
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                btn2.startAnimation(animation);

                break;


            case R.id.btn3 :

                Log.e(TAG, "fromX : " + fromX + ",  fromY : " +fromY);

                TranslateAnimation animation3 = new TranslateAnimation(0,  -fromX, 0, -fromY);
                animation3.setDuration(3000);
                animation3.setFillAfter(false);
                animation3.setFillEnabled(true);
                animation3.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        btn2.layout(0, 0, btn2.getWidth(), btn2.getHeight());
                        Log.e(TAG, " 1: " + btn2.getLeft() + " , 2 : " + btn2.getTop() + "  , 3 : "  + btn2.getRight() + "  , 4 : " + btn2.getBottom());
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                btn2.startAnimation(animation3);

                fromX = 0;
                fromY = 0;

                break;

            default:

                break;
        }
    }
}
반응형