Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you scale down an ImageView with ObjectAnimator, but have it retain its position in layout?

Tags:

android

I am trying to scale an ImageView down in an animation, but whenever I do so it appears to shift down and to left in the layout. Here is a screenshot before I animate:

before animation

And here what it looks like after the animation:

after animation

Is it possible to have the ImageView scale down, but have its top-left corner remain in the top-left corner of the layout?

Here is the code I am using:

// ScaleTestActivity.java

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

public class ScaleTestActivity extends Activity {

    ImageView mImageView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mImageView = (ImageView)findViewById(R.id.image_view);
    }

    @Override
    protected void onResume() {
        super.onResume();

        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(mImageView, "scaleX", 0.5f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(mImageView, "scaleY", 0.5f);

        scaleDownX.setDuration(1000);
        scaleDownY.setDuration(1000);

        AnimatorSet scaleDown = new AnimatorSet();

        scaleDown.play(scaleDownX).with(scaleDownY);
        scaleDown.start();
    }
}

The layout:

<!-- main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/image_view"
        android:src="@drawable/droid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />

</RelativeLayout>

The drawable resource:

Large Android Logo

like image 607
Neil Goodman Avatar asked Aug 11 '11 15:08

Neil Goodman


People also ask

What can be used to animate between two or more views?

In Android, ViewAnimator is a sub class of FrameLayout container that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views ( like TextView, ImageView or any layout). It is mainly useful to animate the views on screen.

What is animation in android?

Animations can add visual cues that notify users about what's going on in your app. They are especially useful when the UI changes state, such as when new content loads or new actions become available. Animations also add a polished look to your app, which gives it a higher quality look and feel.


1 Answers

You may try to set your view's pivotX and pivotY to 0. Scaling is done from that point. I think it will work fine.

like image 85
Seb Avatar answered Nov 08 '22 00:11

Seb