[Android/Kotlin] ๊ฐ„๋‹จํ•œ Animation Transition ๊ตฌํ˜„ํ•˜๊ธฐ

2021. 1. 2. 17:31ใ†๐Ÿ“ฑAndroid/๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Android ๊ฐœ๋ฐœ ์ผ์ง€

๋ฐ˜์‘ํ˜•

์š”์ฆ˜ ๋‹ค์–‘ํ•œ ์ธํ„ฐ๋ ‰์…˜์— ๋Œ€ํ•ด ๊ด€์‹ฌ์ด ์ƒ๊ฒจ ์ฐพ์•„๋ณด๋˜ ๋„์ค‘ ๋‹ค์Œ๊ณผ ๊ฐ™์€ Animation์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค !

AcitivityOptions์˜ makeScenceTransitionAnimation() ์„ ์ด์šฉํ•˜์—ฌ Animation Transition์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ๊ฐ„๋‹จํ•œ Animation๋„ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ RecyclerView์—๋„ Animation Transition์„ ์ ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค : )

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ๋Š” RecyclerView์— Animation Transition ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์จ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค !

 

 

 

 

๐Ÿ“Œ Styles ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ

ํ•ด๋‹น Animation Transition์„ ์ ์šฉํ•˜๋ ค๋ฉด Style์— "android:windowActivityTransitions"์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowActivityTransitions">true</item>
    </style>

 

๐Ÿ“Œ  IntroActivity.xml 

Animation ์ „์˜ ํ™”๋ฉด์„ IntroActivity ๋กœ ํ•˜์—ฌ ๋งŒ๋“ค์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

CircleImageView ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ImageView ํ•˜๋‚˜์™€ TextView ํ•˜๋‚˜๋ฅผ ๋ฐฐ์น˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

ํ•œ ํ™”๋ฉด์— ๋„ˆ๋ฌด ๋งŽ์€ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ์ •์‹ ์—†์ด ๋ณด์ผ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ €๋Š” ImageView / TextView 1๊ฐœ๋งŒ ๋ฐฐ์น˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

โœ”๏ธ ๋™๊ทธ๋ž€ ์ด๋ฏธ์ง€ ์ปค์Šคํ…€ ๋ทฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๋ ค๋ฉด !?

 

hdodenhof/CircleImageView

A circular ImageView for Android. Contribute to hdodenhof/CircleImageView development by creating an account on GitHub.

github.com

- Gradle์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค : )

 //๋™๊ทธ๋ž€ ์ด๋ฏธ์ง€ ์ปค์Šคํ…€ ๋ทฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ : https://github.com/hdodenhof/CircleImageView
implementation 'de.hdodenhof:circleimageview:3.1.0'

 

 

โœ”๏ธ AnimationView์— android:transitionName = "" ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.IntroActivity"
    android:padding="30dp"
    android:background="@color/colorYellow">

    <RelativeLayout
        android:padding="10dp"
        android:id="@+id/rl_intro"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="140dp"
            android:layout_height="140dp"
            android:src="@drawable/profileimg"
            android:layout_centerHorizontal="true"
            android:transitionName="imgTransition"
            android:id="@+id/img_profile_intro"
            app:civ_border_width="1dp"
            app:civ_border_color="@color/colorYellow"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/txt_name_intro"
            android:transitionName="nameTransition"
            android:layout_below="@id/img_profile_intro"
            android:layout_centerHorizontal="true"
            android:text="yuna kim"
            android:textStyle="bold"
            android:textSize="20sp"
            android:textColor="@color/colorBlack"
            android:layout_marginTop="10dp"/>

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

๐Ÿ“Œ MainActivity.xml

Animation ํ›„์˜ ํ™”๋ฉด์ธ MainActivity์— transitionName๊ณผ ๋™์ผํ•œ View๋ฅผ ๊ตฌ์„ฑํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

์• ๋‹ˆ๋ฉ”์ด์…˜์ด ๋งค๋„๋Ÿฌ์šฐ๋ ค๋ฉด ๋™์ผํ•œ ํ™”๋ฉด๊ณผ๋‚ด์šฉ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด์•ผ ํ•  ๊ฑฐ ๊ฐ™์•„์„œ ๋™์ผํ•œ ImageView์™€ TextView๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ ์ฃผ์˜ํ•˜์‹ค์ ์€ IntroActivityxml ๊ณผ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ transitionName์„ ์ง€์ •ํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค!!

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/cl_profile"
        android:padding="10dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/profileimg"
            android:id="@+id/img_profile"
            app:civ_border_width="1dp"
            android:transitionName="imgTransition"
            app:civ_border_color="@color/colorYellow"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

        <TextView
            android:id="@+id/txt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="yuna kim"
            android:textColor="@color/colorBlack"
            android:textSize="20sp"
            android:textStyle="bold"
            android:transitionName="nameTransition"
            android:layout_marginLeft="10dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toRightOf="@id/img_profile"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

๐Ÿ“Œ AcitivityOptions.makeScenceTransitonAnimation() ์ ์šฉํ•˜๊ธฐ

์œ„์™€ ๊ฐ™์ด IntroActivity(์• ๋‹ˆ๋ฉ”์ด์…˜ ์ „ ํ™”๋ฉด) ๊ณผ MainActivity(์• ๋‹ˆ๋ฉ”์ด์…˜ ํ›„ ํ™”๋ฉด)์„ transitionName์„ ๋™์ผํ•˜๊ฒŒ View๋ฅผ ๊ตฌ์„ฑํ•ด์ฃผ์—ˆ๋‹ค๋ฉด, Intent๋ฅผ ํ†ตํ•ด IntroActivity -> MainActivity ํ™”๋ฉด ์ „ํ™˜์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค!

์ด๋•Œ AcitivityOptions.makeScenceTransitonAnimation()์„ ์ด์šฉํ•˜์—ฌ Intent์— Bundle๋กœ ๊ฐ™์ด ์ „๋‹ฌํ•ด์ค๋‹ˆ๋‹ค.

val Pair1 = UtilPair.create<View,String>(img_profile_intro,"imgTransition")
val Pair2 = UtilPair.create<View,String>(txt_name_intro,"nameTransition")

Pair๋ฅผ ๋งŒ๋“ค๋•Œ, (ImageView / TextView id , transitionName)์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค!

rl_intro.setOnClickListener {
       val intent = Intent(this, MainActivity::class.java)

       var options : ActivityOptions = ActivityOptions.makeSceneTransitionAnimation(this@IntroActivity,Pair1,Pair2)
       startActivity(intent, options.toBundle())
}

 

 

 

์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ Animation์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•ด๋“œ๋ ธ์Šต๋‹ˆ๋‹ค : )

๋‹ค์Œ ํฌ์ŠคํŒ…์—๋Š” TransitionAnimation์„ RecyclerView์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ํฌ์ŠคํŒ…ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค !

 

๋ฐ˜์‘ํ˜•