简体   繁体   English

多次进行动画翻译后,将Android视图保持在新位置吗?

[英]Keep Android view in new location after animation translation multiple times?

I've seen the issue many times, where people need the view to stay put after translating it in an animation. 我已经看过很多次这个问题了,人们需要在将其转换为动画后将视图保持原样。 The fix is simple: just add fillAfter="true" and fillEnabled="true" to the XML. 修复很简单:只需将fillAfter =“ true”和fillEnabled =“ true”添加到XML。

This works, and the view does stay put after the animation. 这可以正常工作,并且视图在动画之后不会保留。 However, upon calling the animation again, the view resets to the original point and simply moves to the same spot. 但是,再次调用动画时,视图将重置为原始点,并且仅移动到同一位置。

For instance, say my view is at (0,0) and I call this animation by clicking a button: 例如,假设我的视图是在(0,0),然后单击一个按钮来调用此动画:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true" >

    <translate
        android:duration="100"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="20"
        android:toYDelta="20" >
    </translate>
</set>

It moves from (0,0) to (20,20) as expected. 它按预期从(0,0)移至(20,20)。 However, if I hit the button again and call the same animation, it magically jumps back to (0,0) and translates to (20,20) all over again. 但是,如果我再次按下该按钮并调用相同的动画,它会神奇地跳回到(0,0)并再次转换为(20,20)。

What if I want it to do the same translation, but originating from the new spot? 如果我希望它执行相同的翻译但又起源于新现货怎么办? So I hit the button once, it goes from (0,0) to (20,20). 所以我按下了一次按钮,它从(0,0)变为(20,20)。 I hit it again, it goes to (40,40), then (60,60), and so on. 我再次点击它,依次为(40,40),然后(60,60),依此类推。

Is there a simple way to do that in the XML, or am I going to need to create some variable that updates itself constantly upon each motion (something like START_LOCATION?) 有没有一种简单的方法可以在XML中做到这一点,或者我是否需要创建一些变量,该变量在每次动作时都会不断更新(例如START_LOCATION?)。

At that point, would it just make sense for me to do this all right in the Java and forget the XML? 到那时,对我来说,在Java中正确执行此操作并忘记XML只是有意义吗? I actually quite like how compact and simple the XML is compared to the Java, but I'll begrudgingly switch over if I have to. 我实际上很喜欢XML与Java相比多么紧凑和简单,但是如果需要的话,我会勉强进行切换。

EDIT I just had a smart idea, and removed fromXDelta="0" (and YDelta as well), since I imagined if you only give it the "toXDelta" then it won't care where it starts from. 编辑我只是有一个聪明的主意,并且从XDelta =“ 0”(也包括YDelta)中删除了,因为我想如果只给它加上“ toXDelta”,那么它将不在乎它从哪里开始。 It is a "delta," after all, which we all know means "change," regardless of origin. 毕竟,这是一个“三角洲”,众所周知,无论起源如何,它都意味着“变化”。 But that did nothing. 但这没做。 Sadface. 悲伤的脸。

So, when you animate a View using the normal view animations, you're only animating the position at which it is drawn. 因此,当使用普通的View动画为View设置动画时,您只是在动画绘制它的位置。 You're not affecting its actual location in the layout. 您不会影响其在布局中的实际位置。 Assuming you intend to make the View change position permanently, you'll need to update the layout parameters of the View after the animation is complete to match the movement you make with the animation. 假设您打算永久更改View位置,则需要在动画制作完成后更新View的布局参数,以匹配您通过动画进行的移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM