一起理解动画片段和动画视图

时间:2014-09-18 09:07:41

标签: android animation android-fragments view

我理解为不可能同时执行片段和视图的动画。 我尝试同时运行视图动画和片段

private void startAnim(){
    mShowDarkOverlay = ObjectAnimator.ofFloat(mBackDarkOverlay, "alpha", 0f, 1f);
    mShowDarkOverlay.setDuration(100);
    mShowDarkOverlay.setStartDelay(0);
    mShowDarkOverlay.start();
    getSupportFragmentManager().beginTransaction()
            .setCustomAnimations(android.R.anim.fade_in,R.anim.paddle_fade_out)
            .replace(R.id.fragment_container, new MyFragment())
            .commit();
}

但我只看到动画片段。 我知道视图上的动画在主线程中运行,当它开始片段的动画时,动画视图被中断。 是对的吗?无法立即执行动画视图和片段?

更新

我创建了测试项目。 github

打开应用程序 - 在操作栏中按下按钮(添加项目)(等待3秒) - 单击按钮“2”。 我们看到了结果。在创建片段之前,动画视图不会开始。

片段阻塞主线程? 在我的情况下,片段需要2-3秒才能创建。

我想更改背景,同时更改片段。这可能吗?

1 个答案:

答案 0 :(得分:0)

public class FragmentCustomAnimationSupport extends FragmentActivity {
int mStackLevel = 1;
private TextView mTestAnimationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_stack);

    mTestAnimationView = (TextView) findViewById(R.id.title);
    // Watch for button clicks.
    Button button = (Button)findViewById(R.id.new_fragment);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            addFragmentToStack();
        }
    });

    if (savedInstanceState == null) {
        // Do first time initialization -- add initial fragment.
        Fragment newFragment = CountingFragment.newInstance(mStackLevel);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.simple_fragment, newFragment).commit();
    } else {
        mStackLevel = savedInstanceState.getInt("level");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("level", mStackLevel);
}


void addFragmentToStack() {
    mStackLevel++;

    Animator animator = ObjectAnimator.ofFloat(mTestAnimationView, "alpha", 0f, 1f);
    animator.setDuration(300);
    animator.start();

    // Instantiate a new fragment.
    Fragment newFragment = CountingFragment.newInstance(mStackLevel);

    // Add the fragment to the activity, pushing this transaction
    // on to the back stack.
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.setCustomAnimations(R.anim.fragment_slide_left_enter,
            R.anim.fragment_slide_left_exit,
            R.anim.fragment_slide_right_enter,
            R.anim.fragment_slide_right_exit);
    ft.replace(R.id.simple_fragment, newFragment);
    ft.addToBackStack(null);
    ft.commit();
}



public static class CountingFragment extends Fragment {
    int mNum;

    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static CountingFragment newInstance(int num) {
        CountingFragment f = new CountingFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello_world, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Fragment #" + mNum);
        tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
        return v;
    }
}

}

我试过在SupportV4Demo中,上面的代码,视图和片段的动画一起播放。 mTestAnimationView是一个位于片段容器(R.id.simple_fragment)上方的textView。