Wednesday 13 February 2013

Android Image Animation Example

Here Below Example shows Animate Images in Android

  1. translate
  2. alpha
  3. scale
  4. rotate
  5. complex
Example :

First Create Below Animation files and place in (res/anim folder)

For Translate Animation(anim_translate.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="500"
        android:repeatCount="1"
        android:repeatMode="reverse"/>
</set>

For Alpha Animation(anim_alpha.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration="500"
        android:repeatCount="1"
        android:repeatMode="reverse" />
</set>

For Scale Animation(anim_scale.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <scale
        android:fromXScale="1.0"
        android:toXScale="3.0"
        android:fromYScale="1.0"
        android:toYScale="3.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="500"
        android:repeatCount="1"
        android:repeatMode="reverse" />
</set>


For Rotate Animation(anim_rotate.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator" >

    <rotate
        android:duration="500"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:startOffset="0"
        android:toDegrees="360" />

</set>

For Complex Animation

Merge all above 4 animations

Android UI Layout(main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/animatedImage"
        android:layout_width="160dp"
        android:layout_height="160dp"
        android:layout_centerInParent="true"
        android:src="@drawable/androidman" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/translate"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:text="Translate"
            android:textSize="7dp" />

        <Button
            android:id="@+id/alpha"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:text="Alpha"
            android:textSize="7dp" />

        <Button
            android:id="@+id/scale"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:text="Scale"
            android:textSize="7dp" />

        <Button
            android:id="@+id/rotate"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:text="Rotate"
            android:textSize="7dp" />

        <Button
            android:id="@+id/complex"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:text="Complex"
            android:textSize="7dp" />
    </LinearLayout>

</RelativeLayout>


Android Activity Class(AndroidAnimButtonsActivity.java)

package com.androidsurya.AndroidImageAnimation;

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.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidAnimButtonsActivity extends Activity {
ImageView imageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.animatedImage);
final Animation animTranslate = AnimationUtils.loadAnimation(this,
R.anim.anim_translate);
final Animation animAlpha = AnimationUtils.loadAnimation(this,
R.anim.anim_alpha);
final Animation animScale = AnimationUtils.loadAnimation(this,
R.anim.anim_scale);
final Animation animRotate = AnimationUtils.loadAnimation(this,
R.anim.anim_rotate);

Button btnTranslate = (Button) findViewById(R.id.translate);
Button btnAlpha = (Button) findViewById(R.id.alpha);
Button btnScale = (Button) findViewById(R.id.scale);
Button btnRotate = (Button) findViewById(R.id.rotate);
Button btnComplex = (Button) findViewById(R.id.complex);
btnTranslate.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View view) {
imageView.startAnimation(animTranslate);
}
});

btnAlpha.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View view) {
imageView.startAnimation(animAlpha);
}
});

btnScale.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View view) {
imageView.startAnimation(animScale);
}
});

btnRotate.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View view) {
imageView.startAnimation(animRotate);
}
});
btnComplex.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
AnimationSet sets = new AnimationSet(false);
sets.addAnimation(animAlpha);
sets.addAnimation(animScale);
sets.addAnimation(animRotate);
imageView.startAnimation(sets);
}
});
}
}

Register Android Activity in Android Manifest File

     <activity
            android:name="com.androidsurya.AndroidImageAnimation.AndroidAnimButtonsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Output Screen Shot:
























3 comments:

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...