Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to impliment view Animation in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 307
    Comment on it

    To implement view Animation in Android follow the steps mentioned below:-

    1) Start a new project in android studio.

    2) Now create a new Android resource Directory under res directory then choose interpolater option in directory type and name it anim .

    3) Here is your MainActivity code

    MainActivity.java

    1. public class MainActivity extends Activity {
    2. private Button mFade,mBlink,mMove,mSlide,mClockwise;
    3. private ImageView mImage;
    4. @Override
    5. protected void onCreate(Bundle savedInstanceState) {
    6. super.onCreate(savedInstanceState);
    7. setContentView(R.layout.activity_main);
    8.  
    9. mImage=(ImageView)findViewById(R.id.image123);
    10. mFade=(Button)findViewById(R.id.Fade);
    11. mClockwise=(Button)findViewById(R.id.Clockwise);
    12. mSlide=(Button)findViewById(R.id.Slide);
    13.  
    14. mFade.setOnClickListener(new View.OnClickListener() {
    15. @Override
    16. public void onClick(View v) {
    17. //implementing fade animation with the help of fade.xml file
    18. mImage.startAnimation(android.view.animation.AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade));
    19. }
    20. });
    21. mClockwise.setOnClickListener(new View.OnClickListener() {
    22. @Override
    23. public void onClick(View v) {
    24. //implementing rotation animation with the help of rotate.xml file
    25. mImage.startAnimation(android.view.animation.AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate));
    26. }
    27. });
    28. mSlide.setOnClickListener(new View.OnClickListener() {
    29. @Override
    30. public void onClick(View v) {
    31. //implementing slide animation with the help of slide.xml file
    32. mImage.startAnimation(android.view.animation.AnimationUtils.loadAnimation(MainActivity.this,R.anim.slide));
    33. }
    34. });
    35.  
    36. }
    37. }

    4) Create new animation resource files fade.xml, rotate.xml and slide.xml in anim directory. fade.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:interpolator="@android:anim/accelerate_interpolator">
    4. <alpha
    5. android:fromAlpha="0"
    6. android:toAlpha="1"
    7. android:duration="2000" >
    8. </alpha>
    9.  
    10. <alpha
    11. android:startOffset="2000"
    12. android:fromAlpha="1"
    13. android:toAlpha="0"
    14. android:duration="2000" >
    15. </alpha>
    16. </set>

    rotate.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android">
    3. <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    4. android:fromDegrees="0"
    5. android:toDegrees="360"
    6. android:pivotX="50%"
    7. android:pivotY="50%"
    8. android:duration="5000" >
    9. </rotate>
    10.  
    11. <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    12. android:startOffset="5000"
    13. android:fromDegrees="360"
    14. android:toDegrees="0"
    15. android:pivotX="50%"
    16. android:pivotY="50%"
    17. android:duration="5000" >
    18. </rotate>
    19.  
    20. </set>

    slide.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <set xmlns:android="http://schemas.android.com/apk/res/android"
    3. android:fillAfter="true">
    4. <scale
    5. android:duration="500"
    6. android:fromXScale="1.0"
    7. android:fromYScale="1.0"
    8. android:interpolator="@android:anim/linear_interpolator"
    9. android:toXScale="2.0"
    10. android:toYScale="2.0" />
    11. </set>

    5) Here is your activity_main.xml code

    activity_main.xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    4. android:paddingRight="@dimen/activity_horizontal_margin"
    5. android:paddingTop="@dimen/activity_vertical_margin"
    6. android:paddingBottom="@dimen/activity_vertical_margin"
    7. tools:context=".MainActivity"
    8. android:orientation="vertical">
    9.  
    10.  
    11. <ImageView
    12. android:id="@+id/image123"
    13. android:layout_width="200dp"
    14. android:layout_height="200dp"
    15. android:src="@android:drawable/btn_star_big_on"
    16. android:layout_gravity="center"
    17. />
    18. <LinearLayout
    19. android:layout_width="match_parent"
    20. android:layout_height="wrap_content"
    21. android:orientation="horizontal"
    22. android:layout_marginLeft="20dp"
    23. android:layout_marginTop="20dp">
    24. <Button
    25. android:layout_width="wrap_content"
    26. android:layout_height="wrap_content"
    27. android:text="clockwise"
    28. android:id="@+id/Clockwise"
    29. android:onClick="zoom"/>
    30.  
    31. <Button
    32. android:layout_width="wrap_content"
    33. android:layout_height="wrap_content"
    34. android:text="fade"
    35. android:id="@+id/Fade"
    36. android:onClick="fade"/>
    37.  
    38. <Button
    39. android:layout_width="wrap_content"
    40. android:layout_height="wrap_content"
    41. android:text="slide"
    42. android:onClick="slide"
    43. android:id="@+id/Slide"
    44. />
    45. </LinearLayout>
    46.  
    47. </LinearLayout>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: