14

In android how to make a image grow from one point using animation?

I mean to say is...i have a button ..and i want is when i click on that button my image must grow(ascending order) to grow bigger and bigger from that point ...and when again i click on that button again it must collapse gowing smaller and smaller to end at that point

Can any anybody help me in doing this using android animation? i'm new to android

user1125690
  • 137
  • 1
  • 1
  • 8

3 Answers3

40

This can be achieved using View Animation utility. This scales the image from 100% to 140% for 1 sec

Place the following file in res/anim/scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"    
 xmlns:android="http://schemas.android.com/apk/res/android">
<scale
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1.0"
    android:toXScale="1.4"
    android:fromYScale="1.0"
    android:toYScale="1.4"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="false"
    android:duration="1000" />
 </set>

Java code

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final View view = findViewById(R.id.imageView1);

    final Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);

    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {   
            view.startAnimation(anim);
        }
    });
}
Rajdeep Dua
  • 11,190
  • 2
  • 32
  • 22
3

I would suggest you look at this SO post: Android: Expand/collapse animation

public class DropDownAnim extends Animation {
int targetHeight;
View view;
boolean down;

public DropDownAnim(View view, int targetHeight, boolean down) {
    this.view = view;
    this.targetHeight = targetHeight;
    this.down = down;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    int newHeight;
    if (down) {
        newHeight = (int) (targetHeight * interpolatedTime);
    } else {
        newHeight = (int) (targetHeight * (1 - interpolatedTime));
    }
    view.getLayoutParams().height = newHeight;
    view.requestLayout();
}

@Override
public void initialize(int width, int height, int parentWidth,
        int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
}

@Override
public boolean willChangeBounds() {
    return true;
}
}

You would have to use that as an example as you want to apply it to your button.

Community
  • 1
  • 1
TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
1

Starting with Android 3.0, the preferred way of animating views is to use the android.animation package APIs.These Animator-based classes change actual properties of the View object, ....

Or you can use a ViewPropertyAnimator for simple things - an image button that grows over a period of 1000ms to 1.4 x its size:

imageButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        imageButton.animate().
        scaleX(1.4f).
        scaleY(1.4f).
        setDuration(1000).start();
    }
});
Jiri
  • 61
  • 1
  • 7