Android login page imitation hook effect, you will always need it!

Android login page imitation hook effect, you will always need it!

[[187872]]

Haha, when you see this title, your JH tightens. You may say that you have never encountered it, but not encountering it now does not mean that you will never encounter it. After all, design is unpredictable. There is nothing you can't realize, only things you can't think of. What effect does it have? Yes, it is a small login page. Everyone has the Lagou app. The login page of Lagou is very smooth and has animation effects, so there is a login effect similar to Lagou, as shown in the figure:

Although it is a simple page, it covers quite a lot of things. I wonder why Google has not provided a simple, convenient and accurate keyboard monitoring event? So we can only monitor keyboard events from the side. We can monitor the changes of the outermost layout to determine whether the keyboard has popped up. Without further ado, let's get on the bus.

Layout file, everyone can understand it.

If we want to monitor keyboard events, we first want to know that we can do something when the keyboard pops up, and then do something else when the keyboard is searched. Knowing this is not enough, we also need to know how much the keyboard pops up and how much distance it needs to be translated. We all know that when a page pops up the keyboard, the root layout of this page will call back its listening method: addOnLayoutChangeListener(); when the keyboard pops up, our layout changes, so this callback method will be executed, but the premise is that the windowSoftInputMode property of our Activity must be set to adjustResize.

We want the layout to translate as a whole, which is the distance from the top of the view at the bottom when it pops up minus the height of our keyboard. Now we think that as long as the height of the control pushing the Activity up exceeds 1/3 of the screen height, the soft keyboard is considered to pop up.

  1. scrollView.addOnLayoutChangeListener(new ViewGroup.OnLayoutChangeListener() {
  2. @Override
  3. public void onLayoutChange( View v, int   left , int   top , int   right , int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
  4. /* old is the upper left and lower right coordinate point values ​​before the change, and the value without old is the upper left and lower right coordinate point values ​​after the change
  5. Now it is considered that as long as the control pushes the Activity upwards to a height exceeding 1/3 of the screen height, the soft keyboard is considered to pop up*/
  6. if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
  7. Log.e( "wenzhihao" , "up------>" +(oldBottom - bottom));
  8. int dist = btn_login.getBottom() - bottom;
  9. if (dist>0){
  10. ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(content, "translationY" , 0.0f, -dist);
  11. mAnimatorTranslateY.setDuration(300);
  12. mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
  13. mAnimatorTranslateY.start();
  14. zoomIn(logo, dist);
  15. }
  16. service.setVisibility( View .INVISIBLE);
  17.  
  18. } else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
  19. Log.e( "wenzhihao" , "down------>" +(bottom - oldBottom));
  20. if ((btn_login.getBottom() - oldBottom)>0){
  21. ObjectAnimator mAnimatorTranslateY = ObjectAnimator.ofFloat(content, "translationY" , content.getTranslationY(), 0);
  22. mAnimatorTranslateY.setDuration(300);
  23. mAnimatorTranslateY.setInterpolator(new LinearInterpolator());
  24. mAnimatorTranslateY.start();
  25. //After the keyboard is retracted, the logo returns to its original size and position
  26. zoomOut(logo);
  27. }
  28. service.setVisibility( View .VISIBLE);
  29. }
  30. }
  31. });
  32. /n_login is the login button

In this way, we found that the effect can be achieved, but I want to display it in full screen. I was confused and found that this method is not called back when it is in full screen. What should I do? I checked the information again and found that this is also a bug, but there is a solution, AndroidBug5497Workaround. It is also provided by Google? Copy it directly and you will find that its function is to make the root layout of the outermost layer of the Activity respond to this change when there is a layout change. mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener();

  1. package com.wzh.study.login;
  2.  
  3.  
  4. import android.app.Activity;
  5.  
  6. import android.graphics.Rect;
  7.  
  8. import android. view . View ;
  9.  
  10. import android.view.ViewTreeObserver ;
  11.  
  12. import android.widget.FrameLayout;
  13.  
  14.  
  15. public class AndroidBug5497Workaround {
  16.  
  17.  
  18. // For more information, see https://code.google.com/p/android/issues/detail?id=5497
  19.  
  20. // To use this class, simply invoke assistActivity() on an Activity that already has its content view   set .
  21.  
  22.  
  23. public   static void assistActivity (Activity activity) {
  24.  
  25. new AndroidBug5497Workaround(activity);
  26.  
  27. }
  28.  
  29.  
  30. private View mChildOfContent;
  31.  
  32. private int usableHeightPrevious;
  33.  
  34. private FrameLayout.LayoutParams frameLayoutParams;
  35.  
  36.  
  37. private AndroidBug5497Workaround(Activity activity) {
  38.  
  39. FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
  40.  
  41. mChildOfContent = content.getChildAt(0);
  42.  
  43. mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  44.  
  45. public void onGlobalLayout() {
  46.  
  47. possiblyResizeChildOfContent();
  48.  
  49. }
  50.  
  51. });
  52.  
  53. frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
  54.  
  55. }
  56.  
  57.  
  58. private void possiblyResizeChildOfContent() {
  59.  
  60. int usableHeightNow = computeUsableHeight();
  61.  
  62. if (usableHeightNow != usableHeightPrevious) {
  63.  
  64. int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
  65.  
  66. int heightDifference = usableHeightSansKeyboard - usableHeightNow;
  67.  
  68. if (heightDifference > (usableHeightSansKeyboard/4)) {
  69.  
  70. // keyboard probably just became visible
  71.  
  72. frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
  73.  
  74. } else {
  75.  
  76. // keyboard probably just became hidden
  77.  
  78. frameLayoutParams.height = usableHeightSansKeyboard;
  79.  
  80. }
  81.  
  82. mChildOfContent.requestLayout();
  83.  
  84. usableHeightPrevious = usableHeightNow;
  85.  
  86. }
  87.  
  88. }
  89.  
  90.  
  91. private int computeUsableHeight() {
  92.  
  93. Rect r = new Rect();
  94.  
  95. mChildOfContent.getWindowVisibleDisplayFrame(r);
  96.  
  97. return (r.bottom - r.top ) ;
  98.  
  99. }
  100.  
  101.  
  102. }

Usage: If we set full screen, we will load it, otherwise it will be ignored:

  1. if (isFullScreen (this)) {
  2.  
  3. AndroidBug5497Workaround.assistActivity(this);
  4.  
  5. }
  6.  
  7. ...
  8.  
  9. public boolean isFullScreen(Activity activity) {
  10.  
  11. return (activity.getWindow().getAttributes().flags &
  12.  
  13. WindowManager.LayoutParams.FLAG_FULLSCREEN)==WindowManager.LayoutParams.FLAG_FULLSCREEN;
  14.  
  15. }

Next, let's look at the specific animation events. When the keyboard pops up, the whole thing moves upward and the LOGO shrinks. When the keyboard is retracted, the whole thing moves downward and the LOGO returns to its original size. All the attributes used here are animations. Only with attribute animations can we achieve a true translation effect.

I see many people online using addOnLayoutChangeListener() to listen for keyboard events, but this method calls back too frequently. For example, in this example, a clear icon is displayed when there is text behind the input box. If you use this method, it will also be executed once, which may affect your animation. Of course, you can also record the height of the first time so that it will not follow the logic, but I don't think it is very reliable. Although my method is not great either๑乛◡乛๑~.

*** Paste the source code:

If you have any questions, please point them out. I will give you an example address, including another way to achieve the panning effect by sliding the scrollview to the top.

<<:  The fourth session of the Aiti Tribe Technical Clinic

>>:  Gradle for Android Part 1 (Starting with Gradle and AS)

Recommend

Solid info! 5 major customer acquisition models for APP promotion!

I used to think about this question often when I ...

How to establish a complete user growth system?

The Internet has entered the second half of its e...

IE abandoned: open source monopoly is a good monopoly

Microsoft IE is about to die. A series of browser...

How to attract user traffic and trigger social communication?

Today’s topic is how to ignite social communicati...

Permian, the last days of the Paleozoic Era, the Earth became a purgatory

For the past Humans always have a natural curiosi...