如何在 Android 中为视图设置动画并使其保持在新的位置/大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3345084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:49:56  来源:igfitidea点击:

How to animate a view in Android and have it stay in the new position/size?

androidanimation

提问by justinl

I currently have a view in my Android app and the view is playing a frame animation. I want to animate the view to increase its size to 150%. When I apply a scale animation to it, and the scale animation is completed, I want the viewer to stay at that new size for the rest of the activity's life cycle. Unfortunately right now when the scale-up animation is complete, the view snaps back to the original size. How can I get it to keep the new animated transformation?

我目前在我的 Android 应用中有一个视图,并且该视图正在播放帧动画。我想为视图设置动画以将其大小增加到 150%。当我对其应用缩放动画并完成缩放动画时,我希望查看器在 Activity 的其余生命周期中保持该新大小。不幸的是,当放大动画完成时,视图会恢复到原始大小。我怎样才能让它保持新的动画转换?

I'm using

我正在使用

myView.startAnimation(AnimationUtils.loadAnimation(mContext,R.anim.scaleUp150));

Thanks!

谢谢!

回答by Qlimax

Make sure you add below attributes to the root elementin your animation xml:

确保将以下属性添加到动画 xml的根元素中:

android:fillAfter="true" 
android:fillEnabled="true"

回答by Mina Samy

try constructing the animation from code and setting the properties like this

尝试从代码构建动画并设置这样的属性

anim.setFillEnabled(true);
anim.setFillAfter(true);

回答by cV2

if you want to put your animation in an xml, it may need this to help:

如果你想把你的动画放在 xml 中,它可能需要这个来帮助:

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

<translate
    android:fromYDelta="0"
    android:toYDelta="-20%p"
    android:duration="7000" />

</set>

https://stackoverflow.com/a/6519233/371749

https://stackoverflow.com/a/6519233/371749

please also appreciate the other answer, helped me :) good luck!

也请欣赏其他答案, 帮助了我:)祝你好运!

回答by Andrey

Nowadays it has become very easy:

现在它变得非常容易:

view.animate().x(valueX).y(valueY).setDuration(500).start();

(In this snippet ViewPropertyAnimator has been used).

(在这个片段中使用了 ViewPropertyAnimator)。

There is possible to append multiple other options either.

也可以附加多个其他选项。

The View will be located in the new position.

视图将位于新位置。

回答by Walter Mundt

EDIT: Qlimax's answer is better, but since the OP hasn't returned to change the checkmark and I can't delete an accepted answer, I'll copy it up here: just set fillAfter=true fillEnabled=true

编辑:Qlimax 的答案更好,但由于 OP 没有返回更改复选标记并且我无法删除已接受的答案,我将其复制到此处:只需设置 fillAfter=true fillEnabled=true

My original answer (which works, but is mildly ridiculous by comparison) follows:

我的原始答案(有效,但相比之下有点荒谬)如下:

For things to work as expected after the animation, at least according to this thread, you will have to write an onAnimationEndhandler, and in there, manually adjust the "real" (pre-transformation) bounds of your view to match the end result of the scale animation.

为了在动画后按预期工作,至少根据此线程,您必须编写一个onAnimationEnd处理程序,并在其中手动调整视图的“真实”(转换前)边界以匹配最终结果比例动画。

回答by Sephy

Actually, as the animation you are using seems to be one embedded in the Android framework, I'm not sure you can change anything in it.
However, you can create you own animation by following the example in the documentation. And you will have to put android:fillAfter="true" if you want the scaling to be kept after the end of the animation.

实际上,由于您使用的动画似乎是嵌入在 Android 框架中的动画,因此我不确定您是否可以更改其中的任何内容。
但是,您可以按照文档中的示例创建自己的动画。如果您希望在动画结束后保持缩放比例,则必须输入 android:fillAfter="true"。

回答by takrishna

Put android:fillAfter="true" in the SET tag - as putting them in animation tag confines them to the "transition parameters" or "during/while animating parameters". The parameters in the SET will apply the transformation you are looking for after it finishes the animations.

将 android:fillAfter="true" 放在 SET 标签中 - 因为将它们放在动画标签中会将它们限制在“过渡参数”或“动画期间/动画参数”中。SET 中的参数将在完成动画后应用您正在寻找的转换。