AndyRoid
AndyRoid

Reputation: 5057

TranslateAnimation from middle to Top Left without cutting off view

I have a parent container called mContainer and a child view called mChildImageView. The ImageView is positioned in the middle of the container and I am trying to translate the ImageView to the top left without cutting off the ImageView.

What I would like is to translate the top most point of the ImageView to the top left part of mContainer, this way it would make the ImageView cut off.

This is the following code I am using, as you can see I am using Absolute coordinates, I would like to make it more dynamic how can I accomplish this?

TranslateAnimation a = new TranslateAnimation(
            Animation.ABSOLUTE,             // Specifies X-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
            0.0f,                           // Change in x coordinate at start of animation, can be absolute or percentage if RELATIVE
            Animation.ABSOLUTE,
            -30,                            // Change in x coordinate at end of animation, can be absolute or percentage if RELATIVE
            Animation.ABSOLUTE,             // Specifies Y-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
            0.0f,                           // Change in y coordinate at start of animation, can be absolute or percentage if RELATIVE
            Animation.ABSOLUTE,
            30                              // Change in y coordinate at end of animation, can be absolute or percentage if RELATIVE
    );

mChildImageView.setAnimation(a);
mChildImageView.animate();

Upvotes: 0

Views: 1175

Answers (2)

AndyRoid
AndyRoid

Reputation: 5057

I figured it out so to get it to move to the top left we can do the following

TranslateAnimation a = new TranslateAnimation(
        Animation.RELATIVE_TO_PARENT,            
        0.0f,                          
        Animation.RELATIVE_TO_PARENT,
        -20.0f,      // Translate to left - This is arbitrary enter your own value             
        Animation.RELATIVE_TO_PARENT,           
        0.0f,                           
        Animation.RELATIVE_TO_PARENT,
        -15.0f       // Translate up - This is arbitrary enter your own value                         
);

mChildImageView.setAnimation(a);
mChildImageView.animate();

RELATIVE_TO_PARENT uses percentages so 0.0f to 1.0f

Upvotes: 0

Vitaly Zinchenko
Vitaly Zinchenko

Reputation: 4911

If you want to change TranslationX or/and TranslationY parameters of a view with relation to its parent, I highly recommend you to use Animators instead of Animations as they have some drawbacks (for example: Android - Animation Issue).

Possible variant:

mChildImageView.animate().translationX(newTranslationX).translationY(newTranslationY).setDuration(duration).start();

Upvotes: 1

Related Questions