Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 277
user1125690 Avatar asked Jan 02 '12 04:01

user1125690


2 Answers

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);
        }
    });
}
like image 111
Rajdeep Dua Avatar answered Nov 15 '22 18:11

Rajdeep Dua


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.

like image 23
TryTryAgain Avatar answered Nov 15 '22 19:11

TryTryAgain