Material 登錄注冊過渡動畫實踐

在網(wǎng)上看見一個不錯的登錄注冊效果媚狰,嘗試著寫了一遍,主要使用TextInputLayout做輸入框妄痪,F(xiàn)loatingActionButton做頁面切換按鈕哈雏。
遵循Material design色彩樣式楞件。源碼在我的Github Widget中衫生,并長期更新一些有意思的小組件或功能。
新年開工大吉 土浸!

效果
loginGIf
實現(xiàn)
  • 登錄界面布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    >

    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="350dp"
        android:layout_height="420dp"
        android:layout_centerInParent="true"
        app:cardCornerRadius="7dp"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <View
                    android:layout_width="8dp"
                    android:layout_height="50dp"
                    android:layout_marginTop="15dp"
                    android:background="#039be5" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="40dp"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:text="@string/login"
                    android:textColor="#03a9f4"
                    android:textSize="20sp"
                    android:textStyle="bold"/>
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <EditText
                        android:id="@+id/login_edname"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center_vertical"
                        android:hint="@string/login_edaccount"
                        android:paddingLeft="10dp"
                        android:textColorHint="#9e9e9e"
                        android:textSize="15sp" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp">

                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <EditText
                        android:id="@+id/login_edpwd"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center_vertical"
                        android:hint="@string/login_edpwd"
                        android:paddingLeft="10dp"
                        android:textColorHint="#9e9e9e"
                        android:inputType="textPassword"
                        android:textSize="15sp" />
                </android.support.design.widget.TextInputLayout>
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="30dp">

                <Button
                    android:id="@+id/login_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="@drawable/btn_shape"
                    android:paddingBottom="20dp"
                    android:paddingLeft="60dp"
                    android:paddingRight="60dp"
                    android:paddingTop="20dp"
                    android:text="@string/login_btnok"
                    android:textColor="#03a9f4"
                    android:textSize="18sp" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_horizontal">

                <TextView
                    android:id="@+id/login_tvfogot"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/login_fogotpwd" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center_horizontal">

                <ImageView
                    android:id="@+id/login_qq"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@mipmap/qq" />

                <ImageView
                    android:id="@+id/login_wechat"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:src="@mipmap/wechat" />
            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/cardview"
        android:layout_marginTop="25dp"
        android:src="@mipmap/register"
        android:transitionName="login"
        app:fabSize="normal" />

</RelativeLayout>

利用CardView居中登錄框罪针,登錄卡片略小于注冊卡片,這樣能體現(xiàn)一點層次效果黄伊。FloatingActionButton繼承自ImageButton泪酱,我們使用它主要是為了做出切換效果。
如果要修改FloatingActionButton的背景色,要使用FloatingActionButton的自定義屬性app:backgroundTint="#03a9f4"墓阀,使用Android的background是沒有作用的毡惜。

  • 切換效果

登錄切換到注冊,這是兩個界面斯撮。一般startActivity跳轉(zhuǎn)會有默認的過渡動畫经伙,首先我們要在點擊FloatingActionButton事件中取消掉默認的過渡動畫:

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fab:
                getWindow().setEnterTransition(null);
                getWindow().setExitTransition(null);
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                    ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,fab,fab.getTransitionName());
                    startActivity(new Intent(LoginActivity.this,RegisterActivity.class),options.toBundle());
                }else{
                    startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
                }
                break;
        }
    }

要注意的是需要判斷一下當(dāng)前的系統(tǒng)版本是不是高于21,不然我們自己定義的過渡效果可能是無效的勿锅。
如果系統(tǒng)版本高于21帕膜,因為我們的過渡動畫是涉及到兩個布局的,我們就需要在View上使用Transition框架溢十。
我們在登錄和注冊布局中都給button添加了一個android:transitionName="login"垮刹,這樣我們根據(jù)相同的transitionname做出連貫的過渡動畫。

取消默認的切換動畫张弛,就這兩行代碼:getWindow().setEnterTransition(null);荒典,getWindow().setExitTransition(null);

我們拆分一下過渡動畫:

  1. 按鈕FloatingActionButton從右側(cè)滑行到頂部中間
  2. 布局首先是隱藏的,在按鈕動畫過渡完成以后吞鸭,從按鈕處擴散至完整种蝶。

按鈕FloatingActionButton動畫:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:interpolator="@android:interpolator/linear_out_slow_in"
    android:duration="300">
    <changeBounds>
        <arcMotion
            android:maximumAngle="0"
            android:minimumHorizontalAngle="90"
            android:minimumVerticalAngle="60"/>
    </changeBounds>
</transitionSet>

布局顯示動畫:

    Animator animator = ViewAnimationUtils.createCircularReveal(cv, cv.getWidth() / 2, 0, fab.getWidth() / 2, cv.getHeight());
    animator.setInterpolator(new AccelerateInterpolator());
    animator.setDuration(500);

我們看一下createCircularReveal(View view,int centerX, int centerY, float startRadius, float endRadius)
參數(shù)比較明顯,中心點在頂部中間瞒大,即cardView的寬度/2,高度為0螃征,起始和結(jié)束半徑為button的一半(因為button出去了一部分)和整個cardView的高度
在動畫開始時,就開始顯示注冊布局透敌,所以我們需要一個動畫監(jiān)聽:

    animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                cv.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });
    animator.start();

至此盯滚,主要步驟便實現(xiàn)完成了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末酗电,一起剝皮案震驚了整個濱河市魄藕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌撵术,老刑警劉巖背率,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異嫩与,居然都是意外死亡寝姿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門划滋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來饵筑,“玉大人,你說我怎么就攤上這事处坪「剩” “怎么了架专?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長玄帕。 經(jīng)常有香客問我部脚,道長,這世上最難降的妖魔是什么裤纹? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任睛低,我火速辦了婚禮,結(jié)果婚禮上服傍,老公的妹妹穿的比我還像新娘钱雷。我一直安慰自己,他們只是感情好吹零,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布罩抗。 她就那樣靜靜地躺著,像睡著了一般灿椅。 火紅的嫁衣襯著肌膚如雪套蒂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天茫蛹,我揣著相機與錄音操刀,去河邊找鬼。 笑死婴洼,一個胖子當(dāng)著我的面吹牛骨坑,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播柬采,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼欢唾,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了粉捻?” 一聲冷哼從身側(cè)響起礁遣,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎肩刃,沒想到半個月后祟霍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡盈包,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年沸呐,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片续语。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡垂谢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出疮茄,到底是詐尸還是另有隱情滥朱,我是刑警寧澤,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布力试,位于F島的核電站徙邻,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏畸裳。R本人自食惡果不足惜缰犁,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望怖糊。 院中可真熱鬧帅容,春花似錦、人聲如沸伍伤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽扰魂。三九已至麦乞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間劝评,已是汗流浹背姐直。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蒋畜,地道東北人声畏。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像姻成,于是被迫代替她去往敵國和親砰识。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容