简体   繁体   English

如何使用ViewPropertyAnimation制作从右到左的翻译动画?

[英]How to make Right To Left Translation animation with ViewPropertyAnimation?

I am trying to show a fragment with right to left animation, as though it is coming from the right side of the screen but my code does exactly the opposite. 我试图显示一个从右到左的动画片段,好像它来自屏幕的右侧,但是我的代码却恰好相反。

flBackground.animate().setDuration(250).x(0).translationX(0).alpha(1).start();

flBackground here is the id of the root view in my fragment. flBackground是片段中根视图的ID。

I played with 'translationX' method. 我玩了“ translationX”方法。 I gave the screen width in pixels, but my fragment got off screen. 我以像素为单位给出了屏幕宽度,但是我的片段离开了屏幕。 Is there a way in ViewPropertyAnimation that I can set initial positioning of a view to animate from? ViewPropertyAnimation中是否可以设置视图的初始位置以进行动画处理?

I only want to use ViewPropertyAnimation, not other Animation objects. 我只想使用ViewPropertyAnimation,而不要使用其他Animation对象。 Other ones are quite big and I want simple code for my project. 其他的很大,我想为我的项目提供简单的代码。

You can use FragmentTransaction.setCustomAnimations() . 您可以使用FragmentTransaction.setCustomAnimations() It's the best way. 这是最好的方法。

Example: 例:

getFragmentManager().beginTransaction()
        .setCustomAnimations(R.animator.slide_right, R.animator.slide_left)
        .add(R.id.list_fragment_container, YourFragment.instance())
        .commit()

More: https://developer.android.com/reference/android/app/FragmentTransaction#setCustomAnimations(int,%20int,%20int,%20int) 更多: https : //developer.android.com/reference/android/app/FragmentTransaction#setCustomAnimations(int,%20int,%20int,%20int)

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

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