Monday, January 30, 2012

Animate falling action using Animation of translate

It's a effect of Tweening. To move view from one position to another position.

Animate falling action using Animation of translate



Firstly, make a XML to define the animation, /res/anim/falling.xml.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:fromYDelta="-100%p"
android:toYDelta="0"
android:duration="2000"/>
</set>


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

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/starttranslate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Translate"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom">

<ImageView
android:id="@+id/floatingimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

</LinearLayout>

</LinearLayout>


Main activity
package com.exercise.AndroidAnimTranslate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

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

Button buttonStartTranslate = (Button)findViewById(R.id.starttranslate);
final ImageView floatingImage = (ImageView)findViewById(R.id.floatingimage);

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

buttonStartTranslate.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
floatingImage.startAnimation(animationFalling);
}});
}
}


Download the files.

Related article:
- Tween animation: rotate
- Animate Fade In/Fade Out by changing Alpha
- Animation of Scale

No comments: