Home > DeveloperSection > Articles > Animations in Android

Animations in Android


Android Android  Android Styles 
Ratings:
0 Comment(s)
 943  View(s)
Rate this:

Animations in Android

Android provides a variety of powerful APIs for applying animation to UI elements and drawing custom 2D and 3D graphics. The sections below provide an overview of the APIs and system capabilities available and help you decide with approach is best for your needs.

These classes provide functionality for the property animation system, which allows you to animate object properties of any type. int, float, and hexadecimal color values are supported by default. You can animate any other type by telling the system how to calculate the values for that given type with a custom Type Evaluator.

Adding animations to your app interface will give high quality feel to your android applications. Animations can be performed through either XML or android code. In this tutorial i explained how to do animations using XML notations.

This article is help you to create animation in your application

1.      Create an android application

2.      Add folder in resource file. res/anim

3.      Create myanim.xml file in anim folder and resource type must be Property Animation.

 

And add following code

<?xml version="1.0" encoding="utf-8"?>

<set> 

    <scale

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:duration="5000"

        android:fromXScale="0.5"

        android:fromYScale="0.5"

        android:pivotX="50%"

        android:pivotY="50%"

        android:toXScale="3.0"

        android:toYScale="3.0" >

    </scale>

 

</set>

4.       My activity_main.xml file

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:gravity="center"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

   

 

    <ImageView

        android:id="@+id/myImage"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/ic_launcher" />

 

</LinearLayout>

 

5.       Add following code in MainActivity.class

 

package com.example.androidanimation;

 

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.AnimationUtils;

import android.widget.ImageView;

 

public class MainActivity extends Activity {

                ImageView imageView;

 

                @Override

                protected void onCreate(Bundle savedInstanceState) {

                                super.onCreate(savedInstanceState);

                                setContentView(R.layout.activity_main);

                                // Animations can add subtle visual cues that notify users about what's

                                // going on in your app and improve their mental model of your app's

                                // interface.

                                final Animation animation = AnimationUtils.loadAnimation(

                                                                getApplicationContext(), R.anim.myanim);

                               

                                imageView = (ImageView) findViewById(R.id.myImage);

                                imageView.setOnClickListener(new OnClickListener() {

 

                                                @Override

                                                public void onClick(View v) {

                                                                // TODO Auto-generated method stub

                                                                imageView.startAnimation(animation);

                                                }

                                });

 

                }

 

                @Override

                public boolean onCreateOptionsMenu(Menu menu) {

                                // Inflate the menu; this adds items to the action bar if it is present.

                                getMenuInflater().inflate(R.menu.main, menu);

                                return true;

                }

 

}

 

 

Now run your application

 

 

After click in image

 


Don't want to miss updates? Please click the below button!

Follow MindStick