<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Android實(shí)現(xiàn)登錄注冊(cè)動(dòng)畫切換功能

          共 6526字,需瀏覽 14分鐘

           ·

          2021-08-13 19:38

          登錄、注冊(cè)功能相信大家都很常見了,今天我們來給它添加點(diǎn)動(dòng)畫。

          老規(guī)矩,先上圖:


          這里我使用了背景漸變,平移,旋轉(zhuǎn)動(dòng)畫。把它們組合起來就能實(shí)現(xiàn)上圖的效果了。

          這里我使用了3個(gè)頁面,一個(gè)Activity和2個(gè)Fragment。

          動(dòng)畫邏輯主要在Activity中,下面咱們看看到底該怎么寫:
          public class LoginMainActivity extends AppCompatActivity {    //判斷是登錄還是注冊(cè)    private boolean isLogin = true;    private Fragment[] mFragments;    private Fragment mLastFragment;    private RelativeLayout mRl;    private Button mBtn;
          @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_main_layout); findView(); } //查詢控件 private void findView() { mRl = findViewById(R.id.id_rl); mBtn = findViewById(R.id.id_btn); mFragments = new Fragment[]{new LoginInFragment(), new SignUpFragment()}; //默認(rèn)登錄動(dòng)畫 switchLogin(); //點(diǎn)擊切換登錄或注冊(cè) mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isLogin(); } }); } //"去注冊(cè)"按鈕從左邊平移出來 private void btnTranslateLeft(){ ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mBtn, "translationX",-Px2DpUtil.dp2px(this,100) , Px2DpUtil.dp2px(this,20)); objectAnimator.setDuration(getResources().getInteger(R.integer.anim_short)); objectAnimator.setInterpolator(new AccelerateInterpolator()); objectAnimator.start(); } //“去登錄”按鈕從右邊平移出來 private void btnTranslateRight(){ ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mBtn, "translationX", ScreenUtil.getScreenWidth(), ScreenUtil.getScreenWidth()-Px2DpUtil.dp2px(this,120)); objectAnimator.setDuration(getResources().getInteger(R.integer.anim_short)); objectAnimator.setInterpolator(new AccelerateInterpolator()); objectAnimator.start(); } //背景色漸變 private void animColor(int colorTo) { ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), R.color.white, colorTo); colorAnimation.setDuration(getResources().getInteger(R.integer.anim_short)); colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animator) { int color = (int) animator.getAnimatedValue(); mRl.setBackgroundColor(color); } }); colorAnimation.start(); } //執(zhí)行登錄頁面動(dòng)畫 private void switchLogin() { isLogin = true; switchFragment(mFragments[0], R.anim.rotate_fg_enter_left, R.anim.rotate_fg_exit_left); animColor(ContextCompat.getColor(this, R.color.c_499AF7)); btnTranslateLeft(); mBtn.setText("去注冊(cè)"); } //執(zhí)行注冊(cè)頁面動(dòng)畫 private void switchSignUp(){ isLogin = false; switchFragment(mFragments[1], R.anim.rotate_fg_enter_right, R.anim.rotate_fg_exit_right); animColor(ContextCompat.getColor(this, R.color.c_3ec88e)); btnTranslateRight(); mBtn.setText("去登錄"); }
          private void isLogin() { if (isLogin) { switchSignUp(); } else { switchLogin(); } } /** * Fragment切換 * * @param fragment */ public void switchFragment(Fragment fragment, int enter, int exit) { try { if (fragment == null) { fragment = mFragments[0]; } if (fragment.equals(mLastFragment)) { return; }
          FragmentManager mFragmentManager = getSupportFragmentManager(); FragmentTransaction mTransaction = mFragmentManager.beginTransaction(); //執(zhí)行fragment切換動(dòng)畫 mTransaction.setCustomAnimations(enter, exit);
          if (mLastFragment != null) { mTransaction.hide(mLastFragment); } if (!fragment.isAdded()) { mTransaction.add(R.id.id_fl_login_in, fragment); } else { mTransaction.show(fragment); } mTransaction.commitAllowingStateLoss(); } catch (Exception e) { e.printStackTrace(); } finally { mLastFragment = fragment; } }}

          activity.xml

          <?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/id_rl"    android:layout_width="match_parent"    android:layout_height="match_parent">
          <FrameLayout android:id="@+id/id_fl_login_in" android:layout_width="match_parent" android:layout_height="match_parent" />
          <Button android:id="@+id/id_btn" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginBottom="50dp" android:background="@drawable/shape_btn_login_in" android:text="去注冊(cè)" android:textColor="#95ffffff" android:textSize="20sp"/>
          </RelativeLayout>


          rotate_fg_enter_left.xml
          <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <rotate        android:duration="@integer/anim_short"        android:toDegrees="0"        android:fromDegrees="180"        android:interpolator="@android:anim/bounce_interpolator"        /></set>

          rotate_fg_exit_left.xml

          <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <rotate        android:interpolator="@android:anim/accelerate_interpolator"        android:duration="@integer/anim_short"        android:fromDegrees="0"        android:toDegrees="-180"/></set>


          rotate_fg_enter_right.xml
          <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <rotate        android:interpolator="@android:anim/bounce_interpolator"        android:duration="@integer/anim_short"        android:toDegrees="0"        android:fromDegrees="-180"        /></set>

          rotate_fg_exit_right.xml
          <?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <rotate        android:interpolator="@android:anim/accelerate_interpolator"        android:duration="@integer/anim_short"        android:fromDegrees="0"        android:toDegrees="180"/></set>

          2個(gè)fragment的布局就不貼了,比較簡單,動(dòng)畫邏輯和動(dòng)效都給出了,小伙伴可以根據(jù)自己的需求去添加不同的動(dòng)畫,實(shí)現(xiàn)更酷炫的效果。


          到這里就結(jié)束啦。
          瀏覽 55
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  男女www | 无码免费一区二区三区在线 | a黄色网| 国产性色AV | 被操逼视频 |