Search code examples
androidandroid-animationtranslate-animation

How to move image up and down from a fixed location, rather than starting from the bottom?


The code I am currently using:

    TranslateAnimation mAnimation = new TranslateAnimation(
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 1.0f);
    mAnimation.setDuration(2000);
    mAnimation.setRepeatCount(-1);
    mAnimation.setRepeatMode(Animation.REVERSE);
    mAnimation.setInterpolator(new LinearInterpolator());
    imageView.setAnimation(mAnimation);

Here is the result. (ignore the image quality, I will change it).

I had placed the image in the horizontal center, vertically in line with the floating action button. I want it to start from there.

Also, optionally, I created a fade in-fade out AlphaAnimation. How do I synchronize them both?

Code for AlphaAnimation:

AlphaAnimation blinkAnimation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
    blinkAnimation.setDuration(1000); // duration - half a second
    blinkAnimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
    blinkAnimation.setRepeatCount(5000); // Repeat animation infinitely
    blinkAnimation.setRepeatMode(Animation.REVERSE);

Solution

  • Try this :

    TranslateAnimation mAnimation = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, -1.0f);
    //Less up :     TranslateAnimation.RELATIVE_TO_SELF, -0.5f);
    

    To combine with alphaAnimation :

    AlphaAnimation blinkAnimation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
        blinkAnimation.setDuration(1000); // duration - half a second
        blinkAnimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
        blinkAnimation.setRepeatCount(5000); // Repeat animation infinitely
        blinkAnimation.setRepeatMode(Animation.REVERSE);
    
    AnimationSet set = new AnimationSet(true);
    set.addAnimation(trAnimation);
    set.addAnimation(mAnimation);
    imageView.startAnimation(set)