2015-12-14 29 views
0

我的解决方案存在问题。我想当软键盘显示一些视图得到动画,然后如果键盘隐藏,那会得到另一个动画。这里是我的代码:显示软键盘时的动画视图

coordinatorLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     public void onGlobalLayout() { 

      Rect r = new Rect(); 
      coordinatorLayout.getWindowVisibleDisplayFrame(r); 

      int screenHeight = coordinatorLayout.getRootView().getHeight(); 
      int heightDifference = screenHeight - (r.bottom - r.top); 
      Log.i("Keyboard Size", "Size: " + heightDifference); 

      if (heightDifference < screenHeight){ 
       fabAnimator.start(); 
       searchAnimator.start(); 
      } else { 
       fabAnimatorReverse.start(); 
       searchAnimatorReverse.start(); 
      } 
     } 
    }); 

在清单我已经添加了这一点:

android:windowSoftInputMode="stateHidden" 

第一个问题:当应用程序启动if语句执行的真实部分
12-14 15:41:27.403 22123-22123/ir.bluetec.mobile.boardingpass I/Keyboard Size: Size: 146 12-14 15:41:27.555 22123-22123/ir.bluetec.mobile.boardingpass I/Keyboard Size: Size: 146

问题二:时显示的动画执行几次键盘。
12-14 15:41:39.903 22123-22123/ir.bluetec.mobile.boardingpass I/Keyboard Size: Size: 146 12-14 15:41:40.313 22123-22123/ir.bluetec.mobile.boardingpass I/Keyboard Size: Size: 672 12-14 15:41:51.016 22123-22123/ir.bluetec.mobile.boardingpass I/Keyboard Size: Size: 672 12-14 15:41:51.018 22123-22123/ir.bluetec.mobile.boardingpass I/Timeline: Timeline: Activity_idle id: [email protected] time:264431500 12-14 15:41:54.987 22123-22123/ir.bluetec.mobile.boardingpass I/Keyboard Size: Size: 146

我该如何处理这个问题并解决这个问题?
感谢您的帮助。

回答

0
private void AnimationHandler() { 
    coordinatorLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     public void onGlobalLayout() { 
      //get rootlayout 
      Rect r = new Rect(); 
      coordinatorLayout.getWindowVisibleDisplayFrame(r); 

      //calculate screen, keyboard and top layout 
      float screen = coordinatorLayout.getHeight(); 
      float keyboard = r.bottom - r.top; 
      float top = linearLayout.getHeight(); 

      //calculations for FAB button animation 
      float calcFabTransValue = 5 * keyboard/6 - top - fab.getHeight(); 
      float fabTransValue; 

      //calculation for EditText: search animation 
      float calcSearchTransValue = keyboard/3 - top/2 + search.getHeight()/2; 
      float searchTransValue; 

      //calculation for TextView: description title animation 
      float calcDescTransValue = keyboard/6 - top/4; 
      float descTransValue; 

      //a trick for passing values to animationBuilder that not conflict with animationState 
      if(!animateState){ 
       fabTransValue = calcFabTransValue; 
       searchTransValue = calcSearchTransValue; 
       descTransValue = calcDescTransValue; 
      } else { 
       //// TODO: 15/12/2015 Handle Change Language Event 
       fabTransValue = tempFabTransValue; 
       searchTransValue = tempSearchTransValue; 
       descTransValue = tempDescTransValue; 
      } 

      //build animations with animationBuilder custom method that returns ObjectAnimator 
      descAnim = animationBuilder(desc, 0, descTransValue); 
      fabAnim = animationBuilder(fab, 0, fabTransValue); 
      searchAnim = animationBuilder(search,0,searchTransValue); 

      //run specific animations whether soft keyboard shown or hide 
      if (!animateState && keyboard < screen) { 
       descAnim.start(); 
       searchAnim.start(); 
       fabAnim.start(); 
       animateState = true; 
      } else if (animateState && keyboard >= screen) { 
       descAnim.reverse(); 
       searchAnim.reverse(); 
       fabAnim.reverse(); 
       animateState = false; 
      } 

      //tem values helps that trick 
      tempFabTransValue = calcFabTransValue; 
      tempSearchTransValue = calcSearchTransValue; 
      tempDescTransValue = calcDescTransValue; 

     } 
    }); 
} 

/** 
* 
* @param view Target View 
* @param from Start Value 
* @param to End Value 
* @return ObjectAnimator 
*/ 
public ObjectAnimator animationBuilder(View view, float from, float to){ 
    //create translate animation move with Y-axis, linear interpolator and 300ms duration 
    ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", from, to); 
    anim.setDuration(200); 
    anim.setInterpolator(new LinearInterpolator()); 
    return anim; 
}