Algorithm:
1.) Create a new project by File-> New -> Android Project name it ObjectAnimationExample.
2.) Write following into main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run"
android:id="@+id/startButton"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run"
android:id="@+id/startButton"
/>
</LinearLayout>
3.) Create and write following into src/ShapeHolder.java:
package com.example.ObjectAnimationExample;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.graphics.RadialGradient;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
public class ShapeHolder {
private float x = 0, y = 0;
private ShapeDrawable shape;
private int color;
private RadialGradient gradient;
private float alpha = 1f;
private Paint paint;
private float x = 0, y = 0;
private ShapeDrawable shape;
private int color;
private RadialGradient gradient;
private float alpha = 1f;
private Paint paint;
public void setX(float value) {
x = value;
}
public float getX() {
return x;
}
public void setY(float value) {
y = value;
}
public float getY() {
return y;
}
public void setShape(ShapeDrawable value) {
shape = value;
}
public ShapeDrawable getShape() {
return shape;
}
public int getColor() {
return color;
}
public void setColor(int value) {
shape.getPaint().setColor(value);
color = value;
}
public void setGradient(RadialGradient value) {
gradient = value;
}
public RadialGradient getGradient() {
return gradient;
}
x = value;
}
public float getX() {
return x;
}
public void setY(float value) {
y = value;
}
public float getY() {
return y;
}
public void setShape(ShapeDrawable value) {
shape = value;
}
public ShapeDrawable getShape() {
return shape;
}
public int getColor() {
return color;
}
public void setColor(int value) {
shape.getPaint().setColor(value);
color = value;
}
public void setGradient(RadialGradient value) {
gradient = value;
}
public RadialGradient getGradient() {
return gradient;
}
public void setAlpha(float alpha) {
this.alpha = alpha;
shape.setAlpha((int)((alpha * 255f) + .5f));
}
this.alpha = alpha;
shape.setAlpha((int)((alpha * 255f) + .5f));
}
public float getWidth() {
return shape.getShape().getWidth();
}
public void setWidth(float width) {
Shape s = shape.getShape();
s.resize(width, s.getHeight());
}
return shape.getShape().getWidth();
}
public void setWidth(float width) {
Shape s = shape.getShape();
s.resize(width, s.getHeight());
}
public float getHeight() {
return shape.getShape().getHeight();
}
public void setHeight(float height) {
Shape s = shape.getShape();
s.resize(s.getWidth(), height);
}
return shape.getShape().getHeight();
}
public void setHeight(float height) {
Shape s = shape.getShape();
s.resize(s.getWidth(), height);
}
public ShapeHolder(ShapeDrawable s) {
shape = s;
}
}
shape = s;
}
}
4.) Run for output.
Steps:
1.) Create a project named ObjectAnimationExample and set the information as stated in the image.
Build Target: Android 4.0
Application Name: ObjectAnimationExample
Package Name: com. example. ObjectAnimationExample
Activity Name: ObjectAnimationExampleActivity
Min SDK Version: 14
Application Name: ObjectAnimationExample
Package Name: com. example. ObjectAnimationExample
Activity Name: ObjectAnimationExampleActivity
Min SDK Version: 14
2.) Open ObjectAnimationExampleActivity.java file and write following code there:
package com.example.ObjectAnimationExample;
import java.util.ArrayList;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
import android.animation.AnimatorSet;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.LinearLayout;
public class ObjectAnimationExampleActivity extends Activity {
private static final int DURATION = 1500;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
final MyAnimationView animView = new MyAnimationView(this);
container.addView(animView);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout container = (LinearLayout) findViewById(R.id.container);
final MyAnimationView animView = new MyAnimationView(this);
container.addView(animView);
Button starter = (Button) findViewById(R.id.startButton);
starter.setOnClickListener(new View.OnClickListener() {
starter.setOnClickListener(new View.OnClickListener() {
}
private static final float BALL_SIZE = 100f;
public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
AnimatorSet animation = null;
Animator bounceAnim = null;
ShapeHolder ball = null;
AnimatorSet animation = null;
Animator bounceAnim = null;
ShapeHolder ball = null;
public MyAnimationView(Context context) {
super(context);
addBall(50, 0);
addBall(150, 0);
addBall(250, 0);
addBall(350, 0);
}
super(context);
addBall(50, 0);
addBall(150, 0);
addBall(250, 0);
addBall(350, 0);
}
private void createAnimation() {
if (bounceAnim == null) {
ShapeHolder ball;
ball = balls.get(0);
ObjectAnimator yBouncer = ObjectAnimator.ofFloat(ball, "y",
ball.getY(), getHeight() -BALL_SIZE).setDuration(DURATION);
yBouncer.setInterpolator(new BounceInterpolator());
yBouncer.addUpdateListener(this);
if (bounceAnim == null) {
ShapeHolder ball;
ball = balls.get(0);
ObjectAnimator yBouncer = ObjectAnimator.ofFloat(ball, "y",
ball.getY(), getHeight() -BALL_SIZE).setDuration(DURATION);
yBouncer.setInterpolator(new BounceInterpolator());
yBouncer.addUpdateListener(this);
ball = balls.get(1);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", ball.getY(),
getHeight() - BALL_SIZE);
PropertyValuesHolder pvhAlpha =PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f);
ObjectAnimator yAlphaBouncer =ObjectAnimator.ofPropertyValuesHolder(ball,
pvhY, pvhAlpha).setDuration(DURATION/2);
yAlphaBouncer.setInterpolator(new AccelerateInterpolator());
yAlphaBouncer.setRepeatCount(1);
yAlphaBouncer.setRepeatMode(ValueAnimator.REVERSE);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", ball.getY(),
getHeight() - BALL_SIZE);
PropertyValuesHolder pvhAlpha =PropertyValuesHolder.ofFloat("alpha", 1.0f, 0f);
ObjectAnimator yAlphaBouncer =ObjectAnimator.ofPropertyValuesHolder(ball,
pvhY, pvhAlpha).setDuration(DURATION/2);
yAlphaBouncer.setInterpolator(new AccelerateInterpolator());
yAlphaBouncer.setRepeatCount(1);
yAlphaBouncer.setRepeatMode(ValueAnimator.REVERSE);
ball = balls.get(2);
PropertyValuesHolder pvhW = PropertyValuesHolder.ofFloat("width", ball.getWidth(),
ball.getWidth() * 2);
PropertyValuesHolder pvhH =PropertyValuesHolder.ofFloat("height", ball.getHeight(),
ball.getHeight() * 2);
PropertyValuesHolder pvTX = PropertyValuesHolder.ofFloat("x", ball.getX(),
ball.getX() - BALL_SIZE/2f);
PropertyValuesHolder pvTY = PropertyValuesHolder.ofFloat("y", ball.getY(),
ball.getY() - BALL_SIZE/2f);
ObjectAnimator whxyBouncer =ObjectAnimator.ofPropertyValuesHolder(ball, pvhW, pvhH,
pvTX, pvTY).setDuration(DURATION/2);
whxyBouncer.setRepeatCount(1);
whxyBouncer.setRepeatMode(ValueAnimator.REVERSE);
PropertyValuesHolder pvhW = PropertyValuesHolder.ofFloat("width", ball.getWidth(),
ball.getWidth() * 2);
PropertyValuesHolder pvhH =PropertyValuesHolder.ofFloat("height", ball.getHeight(),
ball.getHeight() * 2);
PropertyValuesHolder pvTX = PropertyValuesHolder.ofFloat("x", ball.getX(),
ball.getX() - BALL_SIZE/2f);
PropertyValuesHolder pvTY = PropertyValuesHolder.ofFloat("y", ball.getY(),
ball.getY() - BALL_SIZE/2f);
ObjectAnimator whxyBouncer =ObjectAnimator.ofPropertyValuesHolder(ball, pvhW, pvhH,
pvTX, pvTY).setDuration(DURATION/2);
whxyBouncer.setRepeatCount(1);
whxyBouncer.setRepeatMode(ValueAnimator.REVERSE);
ball = balls.get(3);
pvhY = PropertyValuesHolder.ofFloat("y", ball.getY(), getHeight()- BALL_SIZE);
float ballX = ball.getX();
Keyframe kf0 = Keyframe.ofFloat(0f, ballX);
Keyframe kf1 = Keyframe.ofFloat(.5f, ballX + 100f);
Keyframe kf2 = Keyframe.ofFloat(1f, ballX + 50f);
PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("x", kf0, kf1, kf2);
ObjectAnimator yxBouncer =ObjectAnimator.ofPropertyValuesHolder(ball, pvhY,
pvhX).setDuration(DURATION/2);
yxBouncer.setRepeatCount(1);
yxBouncer.setRepeatMode(ValueAnimator.REVERSE);
pvhY = PropertyValuesHolder.ofFloat("y", ball.getY(), getHeight()- BALL_SIZE);
float ballX = ball.getX();
Keyframe kf0 = Keyframe.ofFloat(0f, ballX);
Keyframe kf1 = Keyframe.ofFloat(.5f, ballX + 100f);
Keyframe kf2 = Keyframe.ofFloat(1f, ballX + 50f);
PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("x", kf0, kf1, kf2);
ObjectAnimator yxBouncer =ObjectAnimator.ofPropertyValuesHolder(ball, pvhY,
pvhX).setDuration(DURATION/2);
yxBouncer.setRepeatCount(1);
yxBouncer.setRepeatMode(ValueAnimator.REVERSE);
bounceAnim = new AnimatorSet();
((AnimatorSet)bounceAnim).playTogether(yBouncer, yAlphaBouncer, whxyBouncer,
yxBouncer);
}
}
((AnimatorSet)bounceAnim).playTogether(yBouncer, yAlphaBouncer, whxyBouncer,
yxBouncer);
}
}
public void startAnimation() {
createAnimation();
bounceAnim.start();
}
createAnimation();
bounceAnim.start();
}
private ShapeHolder addBall(float x, float y) {
OvalShape circle = new OvalShape();
circle.resize(BALL_SIZE, BALL_SIZE);
ShapeDrawable drawable = new ShapeDrawable(circle);
ShapeHolder shapeHolder = new ShapeHolder(drawable);
shapeHolder.setX(x);
shapeHolder.setY(y);
int red = (int)(100 + Math.random() * 55);
int green = (int)(100 + Math.random() * 105);
int blue = (int)(100 + Math.random() * 205);
int color = 0xff000000 | red << 16 | green << 8 | blue;
Paint paint = drawable.getPaint();
int darkColor = 0xff000000 | blue/4 << 16 | green/6 << 8 | red/4;
RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
50f, color, darkColor, Shader.TileMode.CLAMP);
paint.setShader(gradient);
shapeHolder.setPaint(paint);
balls.add(shapeHolder);
return shapeHolder;
}
OvalShape circle = new OvalShape();
circle.resize(BALL_SIZE, BALL_SIZE);
ShapeDrawable drawable = new ShapeDrawable(circle);
ShapeHolder shapeHolder = new ShapeHolder(drawable);
shapeHolder.setX(x);
shapeHolder.setY(y);
int red = (int)(100 + Math.random() * 55);
int green = (int)(100 + Math.random() * 105);
int blue = (int)(100 + Math.random() * 205);
int color = 0xff000000 | red << 16 | green << 8 | blue;
Paint paint = drawable.getPaint();
int darkColor = 0xff000000 | blue/4 << 16 | green/6 << 8 | red/4;
RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
50f, color, darkColor, Shader.TileMode.CLAMP);
paint.setShader(gradient);
shapeHolder.setPaint(paint);
balls.add(shapeHolder);
return shapeHolder;
}
@Override
protected void onDraw(Canvas canvas) {
for (ShapeHolder ball : balls) {
canvas.translate(ball.getX(), ball.getY());
ball.getShape().draw(canvas);
canvas.translate(-ball.getX(), -ball.getY());
}
}
protected void onDraw(Canvas canvas) {
for (ShapeHolder ball : balls) {
canvas.translate(ball.getX(), ball.getY());
ball.getShape().draw(canvas);
canvas.translate(-ball.getX(), -ball.getY());
}
}
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
invalidate();
}
}
}
}
3.) Compile and build the project.
Output
This post is extremely brilliant and interesting to read. The information you include about Animation development is awesome. Thanks for sharing this valuable information.
ReplyDeletegood code for learner
ReplyDeleteVery helpful code, I will going to take help of this post
ReplyDeleteThanks for sharing
Regards Bus Booking Software