I'm trying to scale an ImageButton
from 100% to 0%. That part is working but it also seems to be moving the view toward the top left corner of the screen (towards 0, 0). I don't want that I just want to scale it. Why is this also translating the view?
The ScaleAnimation implementation
if (view.getVisibility() == 0) {
final ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.ABSOLUTE, centerX, Animation.ABSOLUTE, centerY);
scaleAnimation.setInterpolator(interpolator);
scaleAnimation.setDuration(5000);
view.startAnimation((Animation)scaleAnimation);
scaleAnimation.setAnimationListener(new Animation.AnimationListener(){
public void onAnimationEnd(Animation animation) {
view.setVisibility(n);
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
}
}
The ImageButton
and its parent android.support.design.widget.CoordinatorLayout
in XML:
<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ImageButton
android:id="@+id/buttonToggleResultsWindow0"
android:layout_width="35dp"
android:layout_gravity="top|end"
android:layout_height="35dp"
android:visibility="gone"
android:hint="Hide results window"
android:backgroundTint="@color/accent"
android:src="@drawable/ic_filter_tilt_shift_white_24dp"
android:onClick="toggleResultsWindow"
android:background="@drawable/ripple_circle"
/>
Edit 1
If I change the ScaleAnimation
constructor to this
final ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f, Animation.RELATIVE_TO_SELF, centerX, Animation.RELATIVE_TO_SELF, centerY);
And set centerX
to 8 and centerY
to 4 it seems to stay in the same position or very close to the same position. This does not make any sense to me. What is causing this behavior?
Edit 2
I figured out what is causing the translation of the views but I'm still not sure how to fix it. It seems like if I move the views using setx()
or setY()
any time before the animation, then it gets messed up. This is because the animation is still using the original location of the view, not the new one. So when it translates the view, it is being translated toward the original location. How can I make the ScaleAnimation
use the correct coordinates?