2016-06-15 6 views
0

なぜこれが常に難しいのか分かりません。私はAppCompatライブラリとandroid.app.Fragmentを使用しています。私は左/右(iOSのように)で新しいフラグメントをスライドするアニメーションを追加しようとしますが、フラグメントが追加されると、アニメーションなしで即座に追加/削除されます。objectAnimatorsを使用したR.animatorがフラグメントのアニメーションの追加/削除を使用していません

私は間違っていますか?私も(すなわち、3秒)3000にアニメーションの時間を設定

getFragmentManager() 
    .beginTransaction() 
    .setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_to_the_left) 
    .add(R.id.navrootlayout, fragment) 
    .addToBackStack(null) 
    .commit(); 

RES /アニメーター/ slide_in_from_right.xml

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

RES /アニメーター/ slide_out_to_the_left.xml

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

私はそれがまったく使われているかどうかを明確に見ることができたが、そうではない。フラグメントはアニメーションなしですべて追加されます。私はそれが起こっている画面ビデオをキャプチャし、新しい断片が現れて(そして最終的には消える)瞬時に表示されます。

+0

「xFraction」はどのようなプロパティですか? – DeeV

+0

Yeeeeaaaah、私はちょうどそれを考え出す最後の時間を過ごした。どうやら、私がこれを得たサンプルコードは、レイアウトクラスにカスタムプロパティを追加することを前提としていましたが、それは言及していませんでした。 –

+0

アニメーターでは、相対的な翻訳メカニズムがないので、スライドできないと思うのです。 "xFraction"アニメーション( "X"の割合)を作成してみましょう。 – DeeV

答えて

0

私が間違っていたことを理解しました。私はどこかの例からアニメーションXMLファイルを取得していましたが、この例ではxFractionプロパティを自分で実装する必要があるとは言えませんでした。私は間違って、xFractionとxが関連していることを理解するビルトインビヘイビアであると仮定しました。これは、旧スタイルのres/animスタイルアニメーションではパーセント値をアニメーションの開始/終了値として使用できます。

しかし、これを行うには、レイアウトのサブクラスを作成してxFractionプロパティを自分で追加する必要があります。これが私のやり方です。

package com.mydomain.myapp; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.RelativeLayout; 

public class SlideableLayout extends RelativeLayout { 


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

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

    public SlideableLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    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(); 
     if (width > 0) { 
      setX(xFraction * width); 
     } else { 
      setX(-10000); 
     } 
    } 
} 

その後、私はオンとオフスクリーンアニメーション化したい私のフラグメントの各々のために、私は私のSlideableLayoutルートレイアウトとして使用します。

<?xml version="1.0" encoding="utf-8"?> 
<com.mydomain.myapp.SlideableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@android:color/holo_blue_bright" 
    > 

</com.mydomain.myapp.SlideableLayout> 
関連する問題