从中间到左上角的 TranslateAnimation 没有切断视图

TranslateAnimation from middle to Top Left without cutting off view

我有一个名为 mContainer 的父容器和一个名为 mChildImageView 的子视图。 ImageView 位于容器的中间,我试图将 ImageView 平移到左上角而不切断 ImageView

我想要的是将 ImageView 的最高点平移到 mContainer 的左上角部分,这样会使 ImageView 被截断。

这是我正在使用的以下代码,如您所见,我使用的是绝对坐标,我想让它更具动态性,我该如何实现?

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();

我明白了,要让它移动到左上角,我们可以执行以下操作

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 使用百分比,所以 0.0f 到 1.0f

如果你想改变一个视图相对于它的父视图的 TranslationX or/and TranslationY 参数,我强烈建议你使用 Animators 而不是 Animations 因为它们有一些缺点(例如:).

可能的变体:

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