Reputation: 107
I am working on an application which has an option to select through a list of products. I am showing these images/products (ImageView) in a HorizontalScrollView. Everything is working fine except this last requirement that I have: I have show an animation - when the view is in the centre or while scrolling the view reaches centre, it should expand and all other views in the scroll view should shrink. kinda like the dock in the MacBook-when you hover your mouse over it it expands.
After searching a lot this is what I have come up with but this code works if the views are visible. I want to make it work if the views are in the center.
code to expand and collapse the views:
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);
}
}
expand and collapse code:
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);
}
how do I find out if the views are in centre in HorizontalScrollView?
Upvotes: 1
Views: 392
Reputation: 1414
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();
Upvotes: 1