Wednesday, February 15, 2012

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.

1 comment:

  1. I am using objectAnimator for animating a button from bottom to top in Android. Now i am using the below code

    ObjectAnimator transAnimation = ObjectAnimator.ofFloat(button,"translationY",0,440);
    transAnimation.setDuration(440);
    transAnimation.start().
    I have also tried with sample code below. But still the problem exists


    ObjectAnimator transAnimation = ObjectAnimator.ofFloat(loginLayout, "translationY",0f,0.8f);
    transAnimation.setDuration(480);
    transAnimation.start();



    It works fine in large screen devices. But when it comes to small screen devices it goes off the screen. I want to stay it in the top of the screen irrespective of different screen sizes. I think i have to give values in percentage (say 0% to 100% or 0 to 100%p). So my question is how to give values in percentage in objectAnimator in Android. I also noticed one thing that this objectAnimator is introduced only in HoneyComb. So is there any backward compatibility libraries for running it in low versions. Could anyone guide me to find a solution for this.

    Thanks inAdvance


    ReplyDelete