Android:根据值动画视图向上移动

时间:2018-05-09 02:14:03

标签: java android animation imageview android-animation

我有一个CollapsingToolbarLayoutAppBarLayout我计算了用户通过此代码滚动和执行某些动画的百分比:

appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            float totalScrollRange = Math.abs(appBarLayout.getTotalScrollRange());
            float offset = Math.abs(verticalOffset);
            float scrollOffsetPercentageReverse;
            float scrollOffsetPercentage;
            float percentage;

            if (offset > 0) {
                percentage = (offset / totalScrollRange) * 100;
            } else {
                percentage = 0;
            }

            int reverse = 100 - (int) percentage; // 100 = 0,
            if (reverse > 0) {
                scrollOffsetPercentageReverse = 255.0f * (reverse / 100.0f);
                scrollOffsetPercentage = 255 * (percentage / 100);

                mBookCoverThumb.setImageAlpha((int) scrollOffsetPercentageReverse);

            }

            float scalePercentage = reverse / 100.0f;

            mBookCoverThumb.setScaleX(scalePercentage);
            mBookCoverThumb.setScaleY(scalePercentage);

        }
    });

如您所见,我在Scale上播放了AlphamBookCoverThumb个动画。但是,我希望它也能根据我所拥有的百分比值进行动画向上移动,反之亦然,它会回到原来的位置。我尝试了view.animate().translationY(percentage)view.setTranslateY(percentage),但我认为我做得不对。有什么建议吗?

以下是当前行为:

enter image description here

mBookCover锚定在我的AppBarLayout上,这就是为什么当我滚动时它会向上移动的原因,但我相信布局限制会阻止它进一步向上滚动。

0 个答案:

没有答案