首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ViewPropertyAnimator -动画不会第一次发生

ViewPropertyAnimator -动画不会第一次发生
EN

Stack Overflow用户
提问于 2016-09-23 13:43:38
回答 2查看 1K关注 0票数 3

对于AppBarLayout,我使用以下代码进行幻灯片/滑出切换。

代码语言:javascript
复制
public void showToolbar(boolean show) {
    if (appBar == null) {
        Log.e(TAG, "showToolbar: Toolbar is null");
        return;
    }
    boolean toolbarShown = Utils.isViewVisible(appBar);
    Log.d(TAG, "showToolbar: shown:" +shown);
    boolean changed = (show != toolbarShown);
    if (changed) {
        if (show) {
            Log.d(TAG, "showToolbar: showing");
            appBar.setVisibility(View.VISIBLE);
            appBar.animate()
                    .translationY(0)
                    .setInterpolator(new DecelerateInterpolator())
                    .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) {
                            appBar.setVisibility(View.VISIBLE);
                        }

                        @Override
                        public void onAnimationEnd(Animator animator) { }

                        @Override
                        public void onAnimationCancel(Animator animator) { }

                        @Override
                        public void onAnimationRepeat(Animator animator) { }
                    })
                    .start();
        } else {
            Log.d(TAG, "showToolbar: hiding");
            appBar.animate()
                    .translationY(-toolbar.getBottom())
                    .setInterpolator(new DecelerateInterpolator())
                    .setListener(new Animator.AnimatorListener() {
                        @Override
                        public void onAnimationStart(Animator animator) { }

                        @Override
                        public void onAnimationEnd(Animator animator) {
                            appBar.setVisibility(View.INVISIBLE);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) { }

                        @Override
                        public void onAnimationRepeat(Animator animator) { }
                    })
                    .start();
        }
    } else {
        Log.d(TAG, "showToolbar: no change");
    }
}

除了第一次调用showToolbar(true)来显示工具栏外,动画工作得很好。这个视图在第一次没有动画的时候就出现了。我已经搜索了网站,并发现了类似的问题,但解决方案似乎并不适合我。

值得注意的是,只有当我们希望首先隐藏appBar时,才会发生这种情况。我猜这也许是为了动画

更新1:

代码语言:javascript
复制
public static boolean isViewVisible(View view) {
    if (View.VISIBLE == view.getVisibility()) return true;
    else return false;
}

更新2

我删除了isViewWithinScreenBounds()方法,因为这个检查并不是真正需要的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-25 19:19:33

确保设置visibilitytranslationY的初始值。

如果您希望最初隐藏工具栏并显示第一个动画,请确保设置android:visibility="invisible"而不是 "gone",并设置负android:translationY (如-56dp )。

票数 1
EN

Stack Overflow用户

发布于 2016-09-25 13:05:07

试试这个

代码语言:javascript
复制
<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:translationY="-120dp"
    android:layout_height="120dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SOME TEXT HERE" />
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

活动代码

代码语言:javascript
复制
@BindView(R.id.app_bar)
AppBarLayout appBar;
@BindView(R.id.toolbar)
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_layout);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            animateToolbar(false); //Just for testing purpose - delay execute of function animateToolbar() for 4 seconds
        }
    }, 4000);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    animateToolbar(true);
    return super.onCreateOptionsMenu(menu);
}

private void animateToolbar(boolean show) {

    if (show) {
        // As you can see in the xml layout initial position of the appBar is translated by its height to the top of the screen "-" sign
        // Slide int from out of the screen to initial position -> from -120 dp (height of the app bar, see xml) to 0 dp 
        appBar.animate()
                .translationY(0)
                .setDuration(1000)
                .start(); 

    } else {
        // Slide out from initial position to the top of the screen -> from 0 dp to -120 dp (height of the app bar, see xml)
        appBar.animate()
                .translationY(-appBar.getHeight())
                .setDuration(1000)
                .start();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39662361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档