无法获取objectAnimator与xFraction工作工作、objectAnimator、xFraction

由网友(小三就时你们的神)分享简介:我想一个动画滑入/滑出 片段之间的动画。我知道有很吨similiar问题在那里上的计算器的,但相信我 - 我想所有的人,并没有正常工作。 随着我的code(基本上取自similiar问题):自定义布局公共类SlidingFrameLayout扩展的FrameLayout {公共SlidingFrameLayout(上下...

我想一个动画滑入/滑出 片段之间的动画。我知道有很吨similiar问题在那里上的计算器的,但相信我 - 我想所有的人,并没有正常工作。

随着我的code(基本上取自similiar问题):

自定义布局

 公共类SlidingFrameLayout扩展的FrameLayout {

    公共SlidingFrameLayout(上下文的背景下){
        超(上下文);
    }

    公共SlidingFrameLayout(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }

    公众持股量getXFraction(){
        最终诠释宽度=的getWidth();
        如果(!宽度= 0)返回的getX()/的getWidth();
        否则返回的getX();
    }

    公共无效setXFraction(浮动xFraction){
        最终诠释宽度=的getWidth();
        setX的(?(宽度大于0)(xFraction *宽度):-9999);
    }
}
 

片段交易

  FragmentManager FM = getFragmentManager();
FragmentTransaction英尺= fm.beginTransaction();
ft.setCustomAnimations(R.animator.card_enter_right,R.animator.card_out_left);
ft.replace(R.id.quiz_container,CardFragment.newInstance());
ft.commit();
 

对象动画

 < objectAnimator的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:时间=1000
    机器人:插=@机器人:动画/ linear_interpolator
    机器人:propertyName的=xFraction
    机器人:valueFrom =1.0
    机器人:valueTo =0.0
    机器人:值类型=floatType/>
 
2018 04 21 属性动画 1 初步认识ObjectAnimator

布局文件

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < com.my.package.views.SlidingFrameLayout
        机器人:ID =@ + ID / quiz_container
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent/>

    <! - 其他的观点 - >

< /的FrameLayout>
 

所以,当我运行此code没有任何反应。该片段获得的替换,但无动画。 我究竟做错了什么?

注意::如果我正在与动画X而不是xFraction和更改值假设从1280到0 它的工作。不幸的是,这不是我一个解决方案,因为我需要的百分比的由于各种各样的显示分辨率。

解决方案

由于 pskink 谁领导我的解决方案。我一直认为我的片段的容器需要实现getXFraction()/ setXFraction()方法。但事实并非如此。你片段布局需要实施 getXFraction()/ setXFraction 。 所以基本上我改变了我的片段,从布局是这样的:

 < android.support.v7.widget.CardView的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:preFIX =htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_margin =40dp
    preFIX:cardCornerRadius =4DP>

<! - 内容 - >
< /android.support.v7.widget.CardView>
 

 < com.my.package.views.SlidingFrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:preFIX =htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>

    < android.support.v7.widget.CardView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_margin =40dp
        preFIX:cardCornerRadius =4DP>

<! - 内容 - >
    < /android.support.v7.widget.CardView>
< /com.my.package.views.SlidingFrameLayout>
 

解决方案

setXFraction和getXFraction应在根视图定义:您从onCreateView回报,你的情况自定义视图CardView

I'm trying to animate a slide-in/slide-out animation between Fragments. I know there're tons of similiar question out there on StackOverflow, but believe me - I tried all of them and none is working.

Following my code (basically taken from similiar questions):

Custom Layout

public class SlidingFrameLayout extends FrameLayout {

    public SlidingFrameLayout(Context context) {
        super(context);
    }

    public SlidingFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public float getXFraction() {
        final int width = getWidth();
        if (width != 0) return getX() / getWidth();
        else return getX();
    }

    public void setXFraction(float xFraction) {
        final int width = getWidth();
        setX((width > 0) ? (xFraction * width) : -9999);
    }
}

Fragment Transaction

FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(R.animator.card_enter_right, R.animator.card_out_left);
ft.replace(R.id.quiz_container, CardFragment.newInstance());
ft.commit();

Object animator

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:interpolator="@android:anim/linear_interpolator"
    android:propertyName="xFraction"
    android:valueFrom="1.0"
    android:valueTo="0.0"
    android:valueType="floatType" />

Layout file

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.my.package.views.SlidingFrameLayout
        android:id="@+id/quiz_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Other views -->

</FrameLayout>

So when I'm running this code nothing happens. The Fragments get replaced but without an animation. What am doing wrong?

Note: If I'm running the animation with "x" instead of "xFraction" and changing the values to let's say from:1280 to:0 it's working. Unfortunately this isn't a solution for me, since I need a "percentage value" because of the broad range of display resolutions.

Solution

Thanks to pskink who led me to the solution. I always thought that the container of my Fragment needs to implement the getXFraction()/setXFraction() method. But that's not true. You're Fragments layout needs to implement getXFraction()/setXFraction. So basically I changed my Fragment-Layout from something like this:

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:prefix="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="40dp"
    prefix:cardCornerRadius="4dp">

<!-- Content -->
</android.support.v7.widget.CardView>

to:

<com.my.package.views.SlidingFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:prefix="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="40dp"
        prefix:cardCornerRadius="4dp">

<!-- Content -->
    </android.support.v7.widget.CardView>
</com.my.package.views.SlidingFrameLayout>

解决方案

setXFraction and getXFraction should be defined in the root view: the view you return from onCreateView, in your case custom CardView

阅读全文

相关推荐

最新文章