Using TranslateAnimation class : Animation « Animation « Android






Using TranslateAnimation class

    
//package br.com.tecnove.random.android.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LayoutAnimationController;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * @author "Carlos Eduardo Ruhrwiem"
 * 
 */
public class AnimationUtil {

  private final static int RUNNING_VALUES = 50;
  private final static String[] SPINNING = new String[] { "-", "/", "|", "\\" };
  
  private final static Animation anim_TL = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation anim_T = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation anim_TR = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation anim_R = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation anim_BR = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation anim_B = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation anim_BL = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation anim_L = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
  private final static Animation[] animations = new Animation[]{anim_TL, anim_T,anim_TR, anim_R, anim_BR, anim_B, anim_BL, anim_L};

  public static String[] getValuesForAnimation(int min, int max) {
    String[] returnValues = new String[RUNNING_VALUES];
    for (int i = 0; i < RUNNING_VALUES; i++) {
      returnValues[i] = String.valueOf(RandomGenerator.generateFromRange(min, max));
    }
    return returnValues;
  }

  public static String[] generateAnimationString(String[] inputArray) {
    String[] returnValue = new String[inputArray.length];

    int animationIndex = 0;
    for (int j = 0; j < inputArray.length; j++) {
      returnValue[j] = SPINNING[animationIndex] + "  " + inputArray[j] + "  " + SPINNING[animationIndex];
      animationIndex++;
    }
    
    return returnValue;
  }
  
  public static Animation getRandomDirectionAnimation(){
    return animations[RandomGenerator.generateFromRange(0, 7)];
  }
  
  /**
   * This should be improved. But it works right now.
   * @param values
   * @param outView
   * @param animatedText
   */
  public static void animateTextView(String result, ScrollView outView, TextView animatedText) {
    animatedText.setText(result);

    AnimationSet set = new AnimationSet(true);

    Animation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(500);
    set.addAnimation(animation);

    animation = AnimationUtil.getRandomDirectionAnimation();
    animation.setDuration(1000);
    set.addAnimation(animation);

    LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f);
    outView.setLayoutAnimation(controller);
  }

}


class RandomGenerator {

  private static final Random r = new Random();

  /**
   * Random a value from a specified range
   * 
   * @param min
   *            minimun range (inclusive)
   * @param max
   *            maximun range (inclusive)
   * @return randomed int value
   */
  public static int generateFromRange(int min, int max) {
    // swap values if they come in the wrong order
    if (min > max) {
      int aux = min;
      min = max;
      max = aux;
    }
    int i = max - min;
    int x = r.nextInt(i + 1);
    return x + min;
  }

  /**
   * Random a value from a string vector
   * 
   * @param input
   *            String[] containing all the possible values
   * @return randomed String value
   */
  public static String generateFromStringArray(String[] input) {
    int i = generateFromRange(0, input.length - 1);
    return input[i];
  }

  /**
   * Random a value from a string collection. Just a facilitator for the generateFromStringArray
   * 
   * @param input
   *            Collection of String that contains the possible values
   * @return randomed String value
   */
  public static String generateFromStringCollection(Collection<String> input) {
    return generateFromStringArray((String[]) input.toArray());
  }

  /**
   * Generate a random upercase char
   * 
   * @return randomed char
   */
  public static char generateLetter() {
    return (char) generateFromRange((int) 'A', (int) 'Z');
  }
  
  /**
   * Generate random numbers for a lottery
   * @param maxRange limit the range from 1 to this number (included) 
   * @param numbersToGenerate how many numbers will be generated 
   * @return List of Integers with the non repeating values 
   */
  public static List<Integer> generateLottery(Integer maxRange, Integer numbersToGenerate) {
    List<Integer> numbersGenerated = new ArrayList<Integer>();
    for (int c = 0; c < numbersToGenerate; c++) {
      int nGenerated = 0;
      do {
        nGenerated = r.nextInt(maxRange);
        // adding +1 to make the int start at 1 and the maxRange be included in the sorted values
        nGenerated++;
        // if the number is allready on the list, sort a new number
      } while (numbersGenerated.contains(nGenerated));
      numbersGenerated.add(nGenerated);
    }
    return numbersGenerated;
  }
  
  /**
   * Random a value from a string with values separated by '/'
   * @param valor
   * @return
   */
  public static String generateFromString(String valor) {
    // the input must be like: curitiba/fortaleza/google/facebook/android/motorola
    String[] vetor = valor.split("/");
    String outValue = "";
    if (vetor.length>0){
      outValue = vetor[r.nextInt(vetor.length)];
    }
    return outValue;
  }
}

   
    
    
    
  








Related examples in the same category

1.Animation: fade in, fade out
2.Frame based animation
3.extends Animation to create your own animation
4.Slide out down animation, bounce in down animation
5.XML for Fade in animation
6.Xml for Fade out animation
7.XML for slide out animation
8.Animation Cloning
9.Animation Loading
10.Multi Property Animation
11.Animation: push up in,push up out,push left in,push left out,fade in,fade out,hyperspace in,hyperspace out
12.Using AlphaAnimation class to do animation in code
13.Animation Interpolator
14.Demonstrates the seeking capability of ValueAnimator.
15.Animating by calling invalidate() from draw(),loading and drawing resources, handling onPause() in an animation