Sunday, June 8, 2014

Creating 4 different Tween Animation effects

Android platform facilitates three different types of animation you can define in your application resources: property, tween and frame. In this example, we show you how to do 4 different tween animation effect.



Create Activity
  1. package com.easyinfogeek;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.view.animation.AlphaAnimation;
  8. import android.view.animation.Animation;
  9. import android.view.animation.AnimationSet;
  10. import android.view.animation.AnimationUtils;
  11. import android.view.animation.ScaleAnimation;
  12. import android.view.animation.TranslateAnimation;
  13. import android.view.animation.Animation.AnimationListener;
  14. import android.view.animation.RotateAnimation;
  15. import android.widget.Button;
  16. import android.widget.ImageView;
  17. public class AnimationDemoActivity extends Activity {
  18. private ImageView imageView;
  19. // define animationType enum
  20. enum AnimationType{
  21. Alpha,
  22. Rotate,
  23. Scale,
  24. Translate,
  25. Complex
  26. }
  27. /** Called when the activity is first created. */
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. //define 4 button for each tween animation
  33. imageView = (ImageView)findViewById(R.id.imageView);
  34. Button alpha = (Button)findViewById(R.id.Alpha);
  35. alpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha));
  36. Button rotate = (Button)findViewById(R.id.Rotate);
  37. rotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate));
  38. Button scale = (Button)findViewById(R.id.Scale);
  39. scale.setOnClickListener(new AnimationClickListener(AnimationType.Scale));
  40. Button translate = (Button)findViewById(R.id.Translate);
  41. translate.setOnClickListener(new AnimationClickListener(AnimationType.Translate));
  42. Button complex = (Button)findViewById(R.id.Complex);
  43. complex.setOnClickListener(new AnimationClickListener(AnimationType.Complex));
  44. }
  45. // create a listener inner class
  46. class AnimationClickListener implements OnClickListener{
  47. private AnimationType animationType;
  48. public AnimationClickListener(AnimationType animType){
  49. animationType = animType;
  50. }
  51. public void onClick(View v) {
  52. // TODO Auto-generated method stub
  53. switch (animationType) {
  54. case Alpha:
  55. //Alpha animation. repeat 5 times, last for 1 mins
  56. AlphaAnimation alphaAnimation = (AlphaAnimation)AnimationUtils.loadAnimation(AnimationDemoActivity.this, R.anim.alpha);
  57. imageView.startAnimation(alphaAnimation);
  58. break;
  59. case Rotate:
  60. //rotate animation
  61. RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  62. rotateAnimation.setDuration(3000);
  63. rotateAnimation.setRepeatCount(3);
  64. //start animation
  65. imageView.startAnimation(rotateAnimation);
  66. break;
  67. case Scale:
  68. //scale animation
  69. ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  70. scaleAnimation.setDuration(3000);
  71. scaleAnimation.setRepeatCount(3);
  72. //start animation
  73. imageView.startAnimation(scaleAnimation);
  74. break;
  75. case Translate:
  76. //translate animation
  77. TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);
  78. translateAnimation.setDuration(3000);
  79. translateAnimation.setRepeatCount(3);
  80. //start animation
  81. imageView.startAnimation(translateAnimation);
  82. break;
  83. case Complex:
  84. //four animation overlap
  85. AnimationSet sets = new AnimationSet(false);
  86. AlphaAnimation _animation1 = new AlphaAnimation(1f, 0.1f);
  87. _animation1.setDuration(3000);
  88. RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  89. rotateAnimation1.setDuration(3000);
  90. ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
  91. scaleAnimation1.setDuration(3000);
  92. TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);
  93. translateAnimation1.setDuration(3000);
  94. sets.addAnimation(_animation1);
  95. sets.addAnimation(rotateAnimation1);
  96. sets.addAnimation(scaleAnimation1);
  97. sets.addAnimation(translateAnimation1);
  98. imageView.startAnimation(sets);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. }
  105. }

Create Layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello" /> 
    <ImageView 
        android:id="@+id/imageView" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/qa" /> 
    <Button 
        android:id="@+id/Alpha" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Alpha" /> 
    <Button 
        android:id="@+id/Rotate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Rotate" /> 
    <Button 
        android:id="@+id/Scale" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Scale" /> 
    <Button 
        android:id="@+id/Translate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Translate" /> 
    <Button 
        android:id="@+id/Complex" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Complex" /> 
</LinearLayout>

create alpha.xml resource file

<?xml version="1.0" encoding="utf-8"?> 
<alpha  
    android:fromAlpha="1" android:toAlpha="0.3" android:duration="2000" android:repeatCount="3"
</alpha>



No comments:

Post a Comment