android屬性動(dòng)畫實(shí)現(xiàn)好看的登錄界面效果(登錄頁面editText標(biāo)題上下浮動(dòng)的動(dòng)畫)

前言

使用安卓objectAnimator實(shí)現(xiàn)登錄頁面editText標(biāo)題上下浮動(dòng)的動(dòng)畫

效果

在這里插入圖片描述

代碼

MainActivity.java監(jiān)聽動(dòng)畫關(guān)鍵的代碼

//編輯框點(diǎn)擊時(shí)的動(dòng)畫
    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        if(hasFocus){
            //獲得焦點(diǎn)
            if(v.getId()==R.id.editText_login_user_name){
                
                if(editText_login_user_name.getText().length()==0){
                    //動(dòng)畫用的是px單位 需自己定義工具轉(zhuǎn)換
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_user_name, "translationY", ViewUtil.dip2px(this,25), 0);
                    objectAnimator.setDuration(400);
                    objectAnimator.start();

                }
                //編輯框下的橫線的動(dòng)畫
                textView_line_1.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_line_1, "scaleX", 0, 1);
                objectAnimator.setDuration(400);
                objectAnimator.start();
            }
            if(v.getId()==R.id.editText_login_password){
                if(editText_login_password.getText().length()==0){
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_password, "translationY", ViewUtil.dip2px(this,25), 0);
                    objectAnimator.setDuration(400);
                    objectAnimator.start();
                }
                textView_line_2.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_line_2, "scaleX", 0, 1);
                objectAnimator.setDuration(400);
                objectAnimator.start();
            }

        }else{
            //取消焦點(diǎn)
            textView_line_2.setBackgroundColor(getResources().getColor(R.color.grey));
            textView_line_1.setBackgroundColor(getResources().getColor(R.color.grey));
            if(v.getId()==R.id.editText_login_user_name&&editText_login_user_name.getText().length()==0){
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_user_name, "translationY", 0, ViewUtil.dip2px(this,25));
                objectAnimator.setDuration(400);
                objectAnimator.start();

            }
            if(v.getId()==R.id.editText_login_password&&editText_login_password.getText().length()==0){
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_password, "translationY", 0, ViewUtil.dip2px(this,25));
                objectAnimator.setDuration(400);
                objectAnimator.start();
            }

        }

    }

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener,View.OnFocusChangeListener {

    private TextView textView_login_user_name; // 聲明一個(gè)文本視圖對(duì)象
    private TextView textView_login_password; // 聲明一個(gè)文本視圖對(duì)象
    private TextView textView_line_1; // 聲明一個(gè)文本視圖對(duì)象
    private TextView textView_line_2; // 聲明一個(gè)文本視圖對(duì)象
    private EditText editText_login_user_name; // 聲明一個(gè)編輯框?qū)ο?    private EditText editText_login_password; // 聲明一個(gè)編輯框?qū)ο?    private Button btn_forget; // 聲明一個(gè)按鈕控件對(duì)象
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView_login_user_name = findViewById(R.id.textView_login_user_name);
        textView_login_password = findViewById(R.id.textView_login_password);
        textView_line_1 = findViewById(R.id.textView_line_1);
        textView_line_2 = findViewById(R.id.textView_line_2);
        editText_login_user_name = findViewById(R.id.editText_login_user_name);
        editText_login_password = findViewById(R.id.editText_login_password);

        btn_forget = findViewById(R.id.button_forget_password);
        // 給editText_login_user_name添加文本變更監(jiān)聽器
        editText_login_user_name.addTextChangedListener(new HideTextWatcher(editText_login_user_name, 11));
        // 給editText_login_password添加文本變更監(jiān)聽器
        editText_login_password.addTextChangedListener(new HideTextWatcher(editText_login_password, 6));


        btn_forget.setOnClickListener(this);
        findViewById(R.id.button_login).setOnClickListener(this);
        
        //動(dòng)畫綁定
        editText_login_user_name.setOnFocusChangeListener(this);
        editText_login_password.setOnFocusChangeListener(this);
    }
   
    //編輯框點(diǎn)擊時(shí)的動(dòng)畫
    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        if(hasFocus){
            //獲得焦點(diǎn)
            if(v.getId()==R.id.editText_login_user_name){

                if(editText_login_user_name.getText().length()==0){
                    //動(dòng)畫用的是px單位 需自己定義工具轉(zhuǎn)換
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_user_name, "translationY", ViewUtil.dip2px(this,25), 0);
                    objectAnimator.setDuration(400);
                    objectAnimator.start();

                }
                //編輯框下的橫線的動(dòng)畫
                textView_line_1.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_line_1, "scaleX", 0, 1);
                objectAnimator.setDuration(400);
                objectAnimator.start();
            }
            if(v.getId()==R.id.editText_login_password){
                if(editText_login_password.getText().length()==0){
                    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_password, "translationY", ViewUtil.dip2px(this,25), 0);
                    objectAnimator.setDuration(400);
                    objectAnimator.start();
                }
                textView_line_2.setBackgroundColor(getResources().getColor(R.color.colorBlue));
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_line_2, "scaleX", 0, 1);
                objectAnimator.setDuration(400);
                objectAnimator.start();
            }

        }else{
            //取消焦點(diǎn)
            textView_line_2.setBackgroundColor(getResources().getColor(R.color.grey));
            textView_line_1.setBackgroundColor(getResources().getColor(R.color.grey));
            if(v.getId()==R.id.editText_login_user_name&&editText_login_user_name.getText().length()==0){
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_user_name, "translationY", 0, ViewUtil.dip2px(this,25));
                objectAnimator.setDuration(400);
                objectAnimator.start();

            }
            if(v.getId()==R.id.editText_login_password&&editText_login_password.getText().length()==0){
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView_login_password, "translationY", 0, ViewUtil.dip2px(this,25));
                objectAnimator.setDuration(400);
                objectAnimator.start();
            }

        }

    }

}




activity_main.xml

<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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView_login_wellcome"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:padding="10dp"
        android:text="歡迎登錄"
        android:textAlignment="viewStart"
        android:textColor="@color/black"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_login_wellcome">

        <TextView
            android:id="@+id/textView_login_user_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="-20dp"
            android:background="@null"
            android:text="用戶名"
            android:textSize="20sp"
            android:translationY="25dp"
            app:layout_constraintBottom_toTopOf="@+id/editText_login_user_name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/editText_login_user_name"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/editText_login_user_name"
            android:layout_width="0dp"
            android:layout_height="25dp"
            android:layout_marginTop="2dp"
            android:background="@android:color/transparent"
            android:ems="10"
            android:inputType="textPersonName"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_login_user_name" />

        <TextView
            android:id="@+id/textView_line_1"
            android:layout_width="0dp"
            android:layout_height="2px"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="8dp"
            android:background="@color/grey"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText_login_user_name" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout2">

        <TextView
            android:id="@+id/textView_login_password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="100dp"
            android:text="密碼"
            android:textSize="20sp"
            android:translationY="25dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/editText_login_password"
            android:layout_width="0dp"
            android:layout_height="25dp"
            android:layout_marginTop="2dp"
            android:background="@android:color/transparent"
            android:ems="10"
            android:inputType="textPassword"
            app:layout_constraintEnd_toStartOf="@+id/button_forget_password"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_login_password" />

        <Button
            android:id="@+id/button_forget_password"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="8dp"
            android:background="@null"
            android:text="忘記密碼"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/editText_login_password"
            app:layout_constraintTop_toBottomOf="@+id/textView_login_password" />

        <TextView
            android:id="@+id/textView_line_2"
            android:layout_width="0dp"
            android:layout_height="2px"
            android:layout_marginTop="2dp"
            android:layout_marginBottom="8dp"
            android:background="@color/grey"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button_forget_password"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText_login_password" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:id="@+id/button_login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/button_style"
        android:text="登錄"
        android:textColor="@color/white"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout3" />


</androidx.constraintlayout.widget.ConstraintLayout>

其他樣式布局文件未給出有需要聯(lián)系作者

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末巩搏,一起剝皮案震驚了整個(gè)濱河市氨菇,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡兔毙,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門兄春,熙熙樓的掌柜王于貴愁眉苦臉地迎上來澎剥,“玉大人,你說我怎么就攤上這事赶舆⊙埔Γ” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵芜茵,是天一觀的道長蜻懦。 經(jīng)常有香客問我,道長夕晓,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任悠咱,我火速辦了婚禮蒸辆,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘析既。我一直安慰自己躬贡,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布眼坏。 她就那樣靜靜地躺著拂玻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上檐蚜,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天魄懂,我揣著相機(jī)與錄音,去河邊找鬼闯第。 笑死市栗,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的咳短。 我是一名探鬼主播填帽,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼咙好!你這毒婦竟也來了篡腌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤勾效,失蹤者是張志新(化名)和其女友劉穎嘹悼,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體葵第,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡绘迁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了卒密。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缀台。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖哮奇,靈堂內(nèi)的尸體忽然破棺而出膛腐,到底是詐尸還是另有隱情,我是刑警寧澤鼎俘,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布哲身,位于F島的核電站,受9級(jí)特大地震影響贸伐,放射性物質(zhì)發(fā)生泄漏勘天。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一捉邢、第九天 我趴在偏房一處隱蔽的房頂上張望脯丝。 院中可真熱鬧,春花似錦伏伐、人聲如沸宠进。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽材蹬。三九已至实幕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間堤器,已是汗流浹背昆庇。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留吼旧,地道東北人凰锡。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像圈暗,于是被迫代替她去往敵國和親掂为。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

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