【问题标题】:Animating a horizontal translation of Fragments with objectAnimator使用 objectAnimator 动画片段的水平平移
【发布时间】:2014-07-16 04:52:59
【问题描述】:

我想将片段之间的过渡动​​画化为水平滑入和滑出。

问题是,现在在我的objectAnimator 中,我硬编码了valueTovalueFrom 字段,如下所示:


slide_in_from_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime" 
android:propertyName="x"
android:valueFrom="1280"
android:valueTo="0"
android:valueType="floatType" />

slide_out_to_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="@android:integer/config_mediumAnimTime" 
android:propertyName="x"
android:valueFrom="0"
android:valueTo="-1280"
android:valueType="floatType" />

但这并不适用于所有设备。我怎么能像使用活动一样使用100%0% 之类的值。我在某处看到我必须在我的片段中创建getX()setX() 方法,但我不确定如何去实现它们。另外,如果是这样,我是否必须为我的所有片段实现方法,或者只为FragmentActivity

【问题讨论】:

    标签: android android-fragments android-animation objectanimator


    【解决方案1】:

    ObjectAnimator 对绝对值起作用。另一种解决方案是使用视图动画,因为您可以指定分数

    slide_right.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:shareInterpolator="false">
      <translate android:fromXDelta="-100%" android:toXDelta="0%"
                 android:fromYDelta="0%" android:toYDelta="0%"
                 android:duration="1000"/>
    </set>
    

    加载动画

    Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_right);
    view.startAnimation(animation);
    

    【讨论】:

    • 片段事务中如何设置片段动画? (而不是你建议的观点)
    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多