简体   繁体   English

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

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

I currently have a view in my Android app and the view is playing a frame animation.我目前在我的 Android 应用中有一个视图,并且该视图正在播放帧动画。 I want to animate the view to increase its size to 150%.我想为视图设置动画以将其大小增加到 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.当我对其应用缩放动画并完成缩放动画时,我希望查看器在 Activity 的其余生命周期中保持该新大小。 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?我怎样才能让它保持新的动画转换?

I'm using我正在使用

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

Thanks!谢谢!

Make sure you add below attributes to the root element in your animation xml:确保将以下属性添加到动画 xml 的根元素中:

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

try constructing the animation from code and setting the properties like this尝试从代码构建动画并设置这样的属性

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

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!也请欣赏其他答案,帮助了我:)祝你好运!

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.视图将位于新位置。

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 onAnimationEnd handler, and in there, manually adjust the "real" (pre-transformation) bounds of your view to match the end result of the scale animation.为了在动画后按预期工作,至少根据此线程,您必须编写一个onAnimationEnd处理程序,并在其中手动调整视图的“真实”(转换前)边界以匹配最终结果比例动画。

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".将 android:fillAfter="true" 放在 SET 标签中 - 因为将它们放在动画标签中会将它们限制在“过渡参数”或“动画期间/动画参数”中。 The parameters in the SET will apply the transformation you are looking for after it finishes the animations. SET 中的参数将在完成动画后应用您正在寻找的转换。

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.实际上,由于您使用的动画似乎是嵌入在 Android 框架中的动画,我不确定您是否可以更改其中的任何内容。
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:fillAfter="true"。

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

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