Skip to main content

Create Animation Effect In Android

1] Create anim Folder in res folder

  A] alph.xml:

               <?xml version="1.0" encoding="utf-8"?>
              <set xmlns:android="http://schemas.android.com/apk/res/android">
              <alpha
                           android:fromAlpha="0"
                           android:toAlpha="1.0"
                           android:fillAfter="true"
                           android:duration="7000"/>
                 </set>
  B] rotate.xml:

               <?xml version="1.0" encoding="utf-8"?>
               <set xmlns:android="http://schemas.android.com/apk/res/android">
               <rotate
                         android:fromDegrees="0"
                         android:toDegrees="-360"
                         android:pivotX="50%"
                         android:pivotY="50%"
                         android:duration="7000"/>
                </set>

    C] scale.xml
         
               <?xml version="1.0" encoding="utf-8"?>
               <set xmlns:android="http://schemas.android.com/apk/res/android">
               <scale
                        android:pivotX="50%"
                        android:pivotY="50%"
                        android:fromXScale="1.0"
                        android:fromYScale="1.0"
                       android:toXScale="2.0"
                       android:toYScale="2.0"
                       android:duration="2500"/>
</set>
     
    D]spin.xml
     
               <?xml version="1.0" encoding="utf-8"?>
               <set xmlns:android="http://schemas.android.com/apk/res/android">
               <rotate
                      android:fromDegrees="0"
                      android:toDegrees="360"
                      android:duration="2000"
                      android:pivotX="50%"
                      android:pivotY="50%"
                      android:repeatCount="infinite"/>
                 </set>
     E] translate.xml
           
                   <?xml version="1.0" encoding="utf-8"?>
                  <set xmlns:android="http://schemas.android.com/apk/res/android">
                   <translate
                       android:fromXDelta="110"
                       android:toXDelta="-120"
                       android:duration="4000"
                       android:fillAfter="true"
                       android:fromYDelta="190"
                       android:toYDelta="0"/>
                    </set>

In This Folder create xml file like alpha,rotate,scale,translte

2] Then Create String Array In string.xml file

C 

3] Add Spineer Control in main.xml file Then Entries in Spinner Control:
   
  


5] And Add ImageView in main.xml File:

6] Then Create Code In MainActivity.java File:
 
package com.example.animdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity implements OnItemSelectedListener

{

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Spinner s = (Spinner)findViewById(R.id.spinner1);

        s.setOnItemSelectedListener(this);

    }

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,

long arg3)

{

                    ImageView i = (ImageView)findViewById(R.id.imageView1);

if(arg3==0)

{

                    Animation a = AnimationUtils.loadAnimation(this, R.anim.alpha);

                     i.startAnimation(a);

}

if(arg3==1)

{

                      Animation a = AnimationUtils.loadAnimation(this, R.anim.rotate);

                      i.startAnimation(a);

}

if(arg3==2)

{

                        Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);

                        i.startAnimation(a);

}

if(arg3==3)

{

                        Animation a = AnimationUtils.loadAnimation(this, R.anim.spin);

                        i.startAnimation(a);

}

if(arg3==4)

{

                         Animation a = AnimationUtils.loadAnimation(this, R.anim.translate);

                         i.startAnimation(a);
}

}

public void onNothingSelected(AdapterView<?> arg0)

{

}

}

Output:


Comments

Post a Comment

Popular posts from this blog

Create Shared Prefrence Via XML In Android

Download Source Code : Click Here Output:

Sandeep Maheshwari on zee news

Network Security Exception in Android Retrofit Cleartext HTTP traffic not permitted

Create One directory in res/xml Create file in xml directory Past give below code : < network-security-config > < base-config cleartextTrafficPermitted= "true" /> < domain-config > < domain includeSubdomains= "true" >www.xyz.com</ domain >> < trust-anchors > < certificates src= "system" /> </ trust-anchors > </ domain-config > </ network-security-config > Add one Line into AndroidMenifest.xm File in application tag : android :networkSecurityConfig= "@xml/yourfilename" android :usesCleartextTraffic= "true"