gpt4 book ai didi

android - 水平滚动时展开和折叠动画

转载 作者:行者123 更新时间:2023-11-29 23:49:38 24 4
gpt4 key购买 nike

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

经过大量搜索后,这就是我想出的,但如果 View 可见,则此代码有效。如果 View 位于中心,我想让它工作。

展开和折叠 View 的代码:

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);
}

如何确定 View 是否位于 Horizo​​ntalScrollView 的中心?

最佳答案

 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();

关于android - 水平滚动时展开和折叠动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51036788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com