显示软键盘时的动画视图

时间:2015-12-14 12:34:35

标签: android keyboard

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

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: android.os.BinderProxy@327603cd time:264431500 12-14 15:41:54.987 22123-22123/ir.bluetec.mobile.boardingpass I/Keyboard Size: Size: 146

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

1 个答案:

答案 0 :(得分: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;
}