【问题标题】:Custom Property animation not working on disabling animation自定义属性动画无法禁用动画
【发布时间】:2016-02-29 10:12:42
【问题描述】:

我正在创建一个 android 应用程序,其中每个片段都绑定到自定义的从左到右滑动动画。我使用Custom Property Animation(FractionLinearLayout.java 下面给出的类)实现了这一点。我的应用有几个Fragment,在这些片段之间切换之后是从右到左的幻灯片动画。在我出于测试目的禁用开发人员选项中的动画之前,一切都运行良好。

当我禁用动画时,什么都没有出现。我可以看到 logcats 意味着应用程序运行完美,只是视图没有被加载,(可能是)因为自定义属性 FractionTranslationX 和 FractionTranslationY。

有人遇到过这个问题吗?提前致谢。

FractionLinearLayout.java:动画的自定义类

public class FractionLinearLayout extends LinearLayout {

    DisplayMetrics matrics = getContext().getResources().getDisplayMetrics();

    public FractionLinearLayout(Context context, AttributeSet attrs,
                                int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }

    public FractionLinearLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }

    public FractionLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public float getFractionTranslationX() {

        return getWidth() > 0 ? super.getTranslationX() / getWidth() : Float.MAX_VALUE;
    }

    public void setFractionTranslationX(float translationX) {

        int width = getWidth();
        super.setTranslationX(width > 0 ? width * translationX : Float.MAX_VALUE);
    }

    public float getFractionTranslationY() {

        return getHeight() > 0 ? super.getTranslationX() / getHeight() : Float.MAX_VALUE;
    }

    public void setFractionTranslationY(float translationY) {
        int height = getHeight();
        super.setTranslationY(height > 0 ? height * translationY : Float.MAX_VALUE);
    }

    public float getAnimWidth() {

        return getLayoutParams().width;
    }

    public void setAnimWidth(int animWidth) {

        getLayoutParams().width = animWidth;
        requestLayout();
    }
}

片段的布局文件:

fragment_main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<com.bbi.views.FractionLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/esc_app_background"
    android:orientation="vertical">
...........
...........
</com.bbi.views.FractionLinearLayout>

自定义动画xml文件:

alide_in_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@integer/animation_time"
        android:propertyName="fractionTranslationX"
        android:valueFrom="1"
        android:valueTo="0"
        android:valueType="floatType" />

</set>

slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="@integer/animation_time"
        android:propertyName="fractionTranslationX"
        android:valueFrom="0"
        android:valueTo="-1"
        android:valueType="floatType" />

</set>

在 Activity 上添加片段的代码:

getFragmentManager().beginTransaction()
                .setCustomAnimations(R.anim.slide_in_left;, R.anim.slide_out_left)
                .replace(R.id.frameTopContainer, newsToolFragment)
                .commitAllowingStateLoss();

当我在 fragment_main_layout.xml 中将 com.views.FractionLinearLayout 替换为 LinearLayout 时,一切正常(不是动画很明显,因为我禁用了动画并删除了属性自定义动画) .

【问题讨论】:

    标签: android android-fragments android-animation custom-transition qpropertyanimation


    【解决方案1】:

    最后通过阅读animation_scale标志并据此删除setCustomAnimations()方法解决了我的问题。

    【讨论】:

    • 我认为问题在于自定义动画的实现,完全不建议通过读取系统标志来跳过设置动画,因为这不是正常做法,使用您的视图的人不会期望这样做.
    【解决方案2】:

    问题是当你禁用动画时,动画师会调用setFractionTranslationX()同时传递初始值和最终值,此时你的视图还没有宽度和高度,这将是一个非常早的时刻,所以您每次都会将您的视图转换为 Float.MAX_VALUE。

    要在动画启用与否时正确设置视图 x 和 y 位置,您可以:

    private float mXFraction;
    private float mYFraction;
    
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    
        setFractionTranslationX(mXFraction);
        setFractionTranslationY(mYFraction);
    }
    
    
    public float getFractionTranslationX() {
        return mXFraction;
    }
    
    public void setFractionTranslationX(float xFraction) {
        mXFraction = xFraction;
    
        int width = getWidth();
        if (width > 0) setTranslationX(xFraction * width);
    }
    
    public float getFractionTranslationY() {
        return mYFraction;
    }
    
    public void setFractionTranslationY(float yFraction) {
        mYFraction = yFraction;
    
        int height = getHeight();
        if (height > 0) setTranslationY(yFraction * height);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-13
      • 2011-03-11
      • 2012-12-20
      • 2012-07-29
      • 2014-09-20
      • 2018-11-12
      • 1970-01-01
      相关资源
      最近更新 更多