简体   繁体   English

Android片段翻译动画不起作用

[英]Android Fragment Translate Animation Not Working

I'm trying to do a simple translate animation for two fragments. 我正在尝试为两个片段做一个简单的翻译动画。 One comes in from the right while the other goes out to the left. 一个从右侧进入,另一个向左侧进入。 My min SDK is 14. What happens is the transition takes place, but without the actual animation. 我的最小SDK是14.会发生转换,但没有实际的动画。 After the time specified in the animation xml, the fragments just swap. 在动画xml中指定的时间之后,片段才会交换。 So the animation time is being respected, but there is no actual translation. 所以动画时间正在受到尊重,但没有实际的翻译。

My fragment animation code is: 我的片段动画代码是:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.enter_from_right, R.animator.exit_to_left);
fragmentTransaction.replace(android.R.id.content, termsFragment);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();

My animation xml files are (enter_from_right): 我的动画xml文件是(enter_from_right):

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="100%"
    android:toXDelta="0%"
    android:duration="1000" />
</set>

and exit_to_left: 和exit_to_left:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="0%"
    android:toXDelta="-100%"
    android:duration="1000"/>
</set>

Looks like you are confusing two animation frameworks, android.animation and android.view.animation (yes, it's confusing). 看起来你混淆了两个动画框架, android.animationandroid.view.animation (是的,令人困惑)。

Your XML animation is kind of a mixture between the two. 您的XML动画是两者之间的混合体。 Either you set an android.animation.ObjectAnimator or a android.view.animation.Animation : see here and here for reference. 您可以设置android.animation.ObjectAnimatorandroid.view.animation.Animation :请参阅此处此处以供参考。 In this particular case I think you are looking for a simple translate animation, which belongs to the latter, older, simpler class (and link). 在这个特殊情况下,我认为你正在寻找一个简单的翻译动画,它属于后者,更老,更简单的类(和链接)。

So: 所以:

  • change <objectAnimator> tag to <translate> ; <objectAnimator>标记更改为<translate> ;
  • move your file from the animator folder to the anim resource folder, and recall it using R.anim . 将文件从animator文件夹移动到anim资源文件夹,并使用R.anim调用它。

I recommend reading the official documentation linked which is very clear on this topic. 我建议阅读链接的官方文档,这个主题非常明确。 Basically, for simple translation / rotation / alpha animation, it's better to use view animations ( <translate>, <rotate>, <scale>, <alpha> ) in res/anim folder. 基本上,对于简单的平移/旋转/ alpha动画,最好在res / anim文件夹中使用视图动画( <translate>, <rotate>, <scale>, <alpha> )。

Property animation (like <objectAnimator> in res/animator) are a more powerful tool that you would rather use for complex situations. 属性动画(如res / animator中的<objectAnimator> )是一个更强大的工具,您宁愿用于复杂情况。


With some research I found that setCustomAnimations() has an even more confusing behaviour. 通过一些研究,我发现setCustomAnimations()的行为更加混乱。

If you are using support libraries, setCustomAnimations() only accepts simple animation objects (like your <translate> ). 如果使用支持库,则setCustomAnimations()仅接受简单的动画对象(如<translate> )。 In that case it all should work, you just have to change getFragmentManager() to getSupportFragmentManager() . 在这种情况下,它应该工作,你只需要将getFragmentManager()更改为getSupportFragmentManager()

If you are not using support libraries, setCustomAnimations() only accepts property animations (like the <objectAnimator> ). 如果您使用支持库,则setCustomAnimations()仅接受属性动画(如<objectAnimator> )。

In this second case your simple animation becomes quite hard to do (see here and here for reference). 在第二种情况下,您的简单动画变得非常困难(请参阅此处此处以供参考)。

You can: 您可以:

  • switch to support libraries, which can be boring if your development is not at an early stage, and use the support fragment manager; 切换到支持库,如果您的开发尚未处于早期阶段,这可能会很无聊,并使用支持片段管理器;
  • do some work about objectAnimators and translations - there are lots of questions about, I just linked one above; 做一些关于objectAnimators和翻译的工作 - 有很多问题,我只是在上面链接了一个;
  • use one of the default transitions: 使用其中一个默认转换:

     FragmentTransaction t = getFragmentManager().beginTransaction(); t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); or t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); or t.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); or 
  • try with a very bad <objectAnimator> in your res/animator resource folder: 尝试在res/animator资源文件夹中使用非常糟糕的<objectAnimator>

     <objectAnimator android:propertyName="translationX" android:duration="1000" android:valueFrom="1000" android:valueTo="0" android:valueType="floatType"/> 

This is bad because you need to specify a value in pixels (here I put 1000), so that will look different on different devices. 这很糟糕,因为你需要指定一个像素值(这里我放1000),这样在不同的设备上看起来会有所不同。 But maybe for a fast translation that's not a real problem. 但也许对于快速翻译而言,这不是一个真正的问题。

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

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