activity_bounce_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/mr_bean"
android:visibility="gone" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="30dp"
android:text="Start Animation" />
</RelativeLayout>
BounceAnimationActivity.Java
package com.example.bounceanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class BounceAnimationActivity extends Activity implements AnimationListener {
ImageView imgPoster;
Button btnStart;
// Animation
Animation animBounce;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bounce_animation);
imgPoster = (ImageView) findViewById(R.id.imgLogo);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animBounce = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.bounce);
// set animation listener
animBounce.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// start the animation
imgPoster.setVisibility(View.VISIBLE);
imgPoster.startAnimation(animBounce);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for zoom in animation
if (animation == animBounce) {
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
After this create one folder in Resources name as anim. In anim folder create one one xml file as bounce.xml with below code
bunce.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator">
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
Output
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imgLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/mr_bean"
android:visibility="gone" />
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="30dp"
android:text="Start Animation" />
</RelativeLayout>
BounceAnimationActivity.Java
package com.example.bounceanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class BounceAnimationActivity extends Activity implements AnimationListener {
ImageView imgPoster;
Button btnStart;
// Animation
Animation animBounce;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bounce_animation);
imgPoster = (ImageView) findViewById(R.id.imgLogo);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animBounce = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.bounce);
// set animation listener
animBounce.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// start the animation
imgPoster.setVisibility(View.VISIBLE);
imgPoster.startAnimation(animBounce);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for zoom in animation
if (animation == animBounce) {
}
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
After this create one folder in Resources name as anim. In anim folder create one one xml file as bounce.xml with below code
bunce.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator">
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
Output
No comments:
Post a Comment