ObjectAnimator - Animation in Honeycomb

In previous exercises, we have demonstrate some effect of android animation. In Android 3.0, Honeycomb, ObjectAnimator was added. It's a example show how to implement animation using ObjectAnimator, also compare with animation using Animation.
ObjectAnimator - Animation in Honeycomb
Refer to the video, the upper button (Animator Button) animate using ObjectAnimator. The lower button (Animation Button) animate using TranslateAnimation. You can see, Animation change the visual only: user cannot click on the lower button (Animation Button) to trigger the OnClickListener(). The actual button is still in the original position defined in main.xml, so user have to click on the original space to trigger it. For the upper button animate using ObjectAnimator for Honeycomb, user can click on the button on the shown position.




package com.exercise.AndroidObjectAnimator;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.Toast;

public class AndroidObjectAnimatorActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button animatorButton = (Button)findViewById(R.id.animatorbutton);
Button animationButton = (Button)findViewById(R.id.animationbutton);

ObjectAnimator objectAnimatorButton
= ObjectAnimator.ofFloat(animatorButton, "translationX", 0f, 400f);
objectAnimatorButton.setDuration(1000);
objectAnimatorButton.start();

AnimationSet animSetAnimationButton = new AnimationSet(true);
TranslateAnimation translateAnimAnimationButton
= new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0);
animSetAnimationButton.addAnimation(translateAnimAnimationButton);
animSetAnimationButton.setDuration(500);
animSetAnimationButton.setFillAfter(true);
animationButton.setAnimation(animSetAnimationButton);

animatorButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), 
"Animator Button Clicked", 
Toast.LENGTH_SHORT).show();
}});

animationButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), 
"Animation Button Clicked", 
Toast.LENGTH_SHORT).show();
}});
}


}


<?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" 
android:background="#303030">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/animatorbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animator Button" />
<Button
android:id="@+id/animationbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animation Button" />
</LinearLayout>


Download the files.

0 Response to "ObjectAnimator - Animation in Honeycomb"

Post a Comment