首页 > 解决方案 > 如何使用 ViewPropertyAnimation 制作从右到左的平移动画?

问题描述

我试图显示一个从右到左动画的片段,就好像它来自屏幕的右侧,但我的代码完全相反。

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

flBackground 这里是我的片段中根视图的 id。

我玩的是“translationX”方法。我以像素为单位给出了屏幕宽度,但我的片段离开了屏幕。ViewPropertyAnimation 中是否有一种方法可以设置视图的初始定位以从中进行动画处理?

我只想使用 ViewPropertyAnimation,而不是其他 Animation 对象。其他的都很大,我想为我的项目编写简单的代码。

标签: javaandroidandroid-animation

解决方案


您可以使用FragmentTransaction.setCustomAnimations(). 这是最好的方法。

例子:

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

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


推荐阅读