Can't get objectAnimator working with xFraction

后端 未结 3 1480
慢半拍i
慢半拍i 2020-12-21 16:24

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<

3条回答
  •  被撕碎了的回忆
    2020-12-21 16:54

    since above code listings are only partial, I thought I'd share with you a complete working example. It works as outlined in the question.

    Pressing the button toggles between 2 fragments A and B (by slide animation right to left). The fragments are just stupid text (AAAAAA and BBBBB) with different backgrounds.

    MainActivity.java

    package com.example.slidetrans;
    
    import android.app.Activity;
    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    
    public class MainActivity extends Activity {
    
        boolean showingA = true;
        Button button;
    
        A a;
        B b;
    
        private void incarnate(FragmentManager fm){
            int layoutId = R.id.frame;
            boolean fragmentWasNull = false;
            Fragment f = fm.findFragmentById(layoutId);
            if (f == null){
                if (showingA){
                    f = a = new A();
                } else {
                    f = b = new B();
                }
                fragmentWasNull = true;
            }
            if (fragmentWasNull){
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(layoutId, showingA ? a : b,  "main").commit(); 
            }
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            FragmentManager fm = getFragmentManager();
            incarnate(fm);
            button = (Button)findViewById(R.id.button);
            OnClickListener listener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction transaction = fm.beginTransaction();
                    transaction.setCustomAnimations(R.anim.in, R.anim.out);
                    transaction.replace(R.id.frame, showingA ? new B() : new A()).commit();
                    showingA = !showingA;
                    button.setText(showingA ? "slide in B" : "slide in A");
                }
            };
            button.setOnClickListener(listener);
        }
    }
    

    LL.java

    package com.example.slidetrans;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.LinearLayout;
    
    public class LL extends LinearLayout {
    
        public LL(Context context) {
            super(context);
        }
    
        public LL(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();
            float newWidth = (width > 0) ? (xFraction * width) : -9999;
            setX(newWidth);
        }
    }
    

    main.xml (layout)

    
    
        

    a.xml (layout)

    
    
    
        
    
    
    

    b.xml (layout)

    
    
    
        
    
    
    

    in.xml (anim)

    
    
        
    
    
    

    out.xml (anim)

    
    
        
    
    
    

提交回复
热议问题