Android button animation

In this tip, I am going to show you how to make an animation on a Android Button widget in two different ways. For the first way, i will use the ObjectAnimator class to make the button animated. ObjectAnimator allows you to specify the target view to animate, its property, and the value of the property that will be changed while the view is animating. For example, when a button is clicked, it will be change from completely opaque to less opaque or more transparent.

Now to have a workable example application, you need to create a new Android project. You will modify the content of activity_main.xml to add a button as shown below.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:id="@+id/container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
  
   >

   <Button
     android:id="@+id/bt"
     android:text="Click me"
     android:onClick="animateButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />

</LinearLayout>


Then in the MainActivity class, add the animateButton method. Here is the body of the animateButton method:

  public void animateButton(View view){
   //get current alpha value of the button
   float al=view.getAlpha();
   //create an animator object
   ObjectAnimator animator;
   if(al<1.0)
   animator=ObjectAnimator.ofFloat(view, "alpha",1f);
   else
   animator=ObjectAnimator.ofFloat(view, "alpha",0.5f);
   //set animation duration
   animator.setDuration(1000);
   //start animation
   animator.start();
   }

The second way to make the button animation is using the LayoutTransition class. The LayoutTransition class allows you to make the change on the view hierarchy of the layout container animated. You will add the following code to onStart method of the MainActivity class to achieve this goal.

  //create a LayoutTransition object
LayoutTransition translation = new LayoutTransition();
//set duration of animation
translation.setDuration(3000);
//enable translation type
translation.enableTransitionType(LayoutTransition.APPEARING);      
//create a reference to layout container
viewGroup = (ViewGroup) findViewById(R.id.container);
//set LayoutTransition object to the container
viewGroup.setLayoutTransition(translation);
//add a button to the layout container
Button bt= new Button(this);
bt.setText("Animating the Button...");
viewGroup.addView(bt);

Android-button-animation

Posted by: Dara | post date: 07-03-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.