본문 바로가기

안드로이드

[안드로이드] Custom View 커스텀 뷰 (4) - 애니메이션 추가 성공

[분류 전체보기] - [안드로이드] Custom View 커스텀 뷰 (3) - 애니메이션 추가 및 뻘 짓

 

[안드로이드] Custom View 커스텀 뷰 (3) - 애니메이션 추가 및 뻘 짓

[안드로이드] - [안드로이드] Custom View 커스텀 뷰 (2) - 화면 터치 시 생성 [안드로이드] Custom View 커스텀 뷰 (2) - 화면 터치 시 생성 [안드로이드] - [안드로이드] Custom View 커스텀 뷰 (1) - 만들기 [..

dev-genue.tistory.com

이거에 이어서 마무리를 하겠습니다. 하루 쉬었더니 머리가 맑아져서 그런지 바로 해결했어요

어제는 왜 생각을 못했지;;;

 

참고로, 제가 이해한 대로 설명할 것이라 조금 틀릴 수도 있습니다.


커스텀 뷰 레이아웃에 변화를 줬습니다.

[ my_custom_layout.xml ]

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/layoutBackground"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#55B57C35"
    >
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/layoutMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/white_border"
        android:layout_margin="20dp"
        android:padding="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        >
        <ImageView
            android:id="@+id/ivThumbnail"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/layoutLocation"
            app:layout_constraintTop_toTopOf="parent" 
            />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="제목"
            android:textColor="@color/white"
            app:layout_constraintBottom_toTopOf="@+id/layoutLocation"
            app:layout_constraintLeft_toLeftOf="@+id/layoutLocation"
            app:layout_constraintRight_toRightOf="@+id/layoutLocation"
            app:layout_constraintTop_toTopOf="parent" 
            />

        <LinearLayout
            android:id="@+id/layoutLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@+id/ivThumbnail"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tvTitle"
            >

            <TextView
                android:id="@+id/tvLocationX"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!"
                android:textColor="@color/white" 
                />

            <TextView
                android:id="@+id/tvLocationY"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!"
                android:textColor="@color/white" 
                />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

실제 컨텐츠 부분을 레이아웃 하나로 더 묶고, 이 것에 애니메이션을 주는 것이었습니다. (구분하기 쉽게 id로 부를게요)

여기서 봐야할 것은 layoutMainmargin 을 줬다는 것입니다. 이 margin 이 있어야 원하는 대로 보이더라고요. 

 

그리고 스케일 애니메이션은 AnimationListener을 이용해서 한 애니메이션이 끝나면 다음 애니메이션으로 넘어가도록 했습니다.

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

    @Override
    public void onAnimationEnd(Animation animation) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1.1f, 0.93f, 1.1f, 0.93f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(500);
        scaleAnimation.setFillEnabled(true);
        scaleAnimation.setFillAfter(false);
        
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                ScaleAnimation scaleAnimation = new ScaleAnimation(0.93f, 1.05f, 0.93f, 1.05f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(500);
                scaleAnimation.setFillEnabled(true);
                scaleAnimation.setFillAfter(false);
                scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {}

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        ScaleAnimation scaleAnimation = new ScaleAnimation(1.05f, 1.0f, 1.05f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
                        scaleAnimation.setDuration(500);
                        scaleAnimation.setFillEnabled(true);
                        scaleAnimation.setFillAfter(false);
                        mView.startAnimation(scaleAnimation);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {}
                });
                mView.startAnimation(scaleAnimation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        mView.startAnimation(scaleAnimation);
    }

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

 

스케일 애니메이션 작동 시 뷰 영역은 처음에 정해진 영역 이상으로 커지지가 않습니다. 일단 백마디 말보다, 직접 보시죠.

 

 

보면 스케일 1 까지는 배경 영역(네모 색칠 영역)도 넓어지다가 1이 넘어가면 안에 콘텐츠(흰 테두리 영역)만 커지는 것을 볼 수 있습니다. 이걸로 알 수 있는 것은 뷰의 영역은 정해져 있고, 0~1까지는 뷰 영역도 움직이지만, 그 이상은 안된다는 것입니다. 그렇기 때문에, 스케일을 1 이상으로 키우고 싶으면, 콘텐츠 영역에 margin을 줘서 배경 영역을 넓혀야 합니다. 그치만, margin도 고정이기 때문에, 만약 스케일을 너무 키우면 또 짤리는 현상이 발생할 수 있으니, 적당한 margin을 주면 됩니다. 

중요한 것은 컨텐츠 영역을 감싸는 영역에 빈 공간이 있어야 한다는 것입니다. 

 

 

되게 단순한 거였는데 생각을 못해서 하루를 날려먹었네요 ㅠㅜ