横向滚动扩展和折叠动画

时间:2018-06-26 07:05:20

标签: android android-animation horizontalscrollview

我正在开发一个可以选择产品列表的应用程序。我在Horizo​​ntalScrollView中显示这些图像/产品(ImageView)。除了我的最后一个要求,一切工作正常:我已经展示了一个动画-当视图位于中心或滚动视图到达中心时,它应该展开,滚动视图中的所有其他视图都应该缩小。就像MacBook中的基座一样,当您将鼠标悬停在其上时,它会扩展。

经过大量搜索,这是我想出的,但是如果视图可见,则此代码有效。如果视图在中间,我想使其正常工作。

代码以展开和折叠视图:

private void horizontalScrollAnimation(ImageView imageView1, ImageView imageView2, ImageView imageView3) {

    Rect scrollBounds = new Rect();
    horizontal_scroll_view.getHitRect(scrollBounds);
    if (imageView1.getLocalVisibleRect(scrollBounds)) {
        expand(imageView1);
    } else {
        collapse(imageView1);
    }

    if (imageView1.getLocalVisibleRect(scrollBounds)) {
        expand(imageView2);
    } else {
        collapse(imageView2);
    }

    if (imageView1.getLocalVisibleRect(scrollBounds)) {
        expand(imageView3);
    } else {
        collapse(imageView3);
    }


}

展开和折叠代码:

public static void expand(final View v) {
    v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 1;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? LinearLayout.LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if(interpolatedTime == 1){
                v.setVisibility(View.GONE);
            }else{
                v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

如何确定Horizo​​ntalScrollView中的视图是否居中?

1 个答案:

答案 0 :(得分:1)

 ObjectAnimator objectanimator1, objectanimator2;

 objectanimator1 = ObjectAnimator.ofFloat(view,"scaleX",1.0f,1.2f);

 objectanimator2 = ObjectAnimator.ofFloat(view,"scaleY",1.0f,1.2f); 


 objectanimator1.setDuration(4000);
 objectanimator2.setDuration(4000);

 objectanimator1.start();
 objectanimator2.start();