1

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?

TychoTheTaco
  • 664
  • 1
  • 7
  • 28

2 Answers2

0

try to use pivotXType and pivotYType is Animation.RELATIVE_TO_SELF.

sonnv1368
  • 1,547
  • 12
  • 17
0

try to use pivotXType and pivotYType is Animation.RELATIVE_TO_SELF.

In my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    tools:context="com.sonnv1368.animationscale.MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

In my MainActivity.class

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);
        animationImageView();
    }

    private void animationImageView() {
        if (imageView.getVisibility() == View.VISIBLE) {
            final ScaleAnimation scaleAnimation = new ScaleAnimation(
                    1.0f, 0.0f, 1.0f, 0.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(5000);
            imageView.startAnimation(scaleAnimation);
        }
    }
}

It is working, it will scale at center of imageview.

sonnv1368
  • 1,547
  • 12
  • 17