【问题标题】:Understanding animation fragment and animation view together一起理解动画片段和动画视图
【发布时间】:2014-09-18 22:16:47
【问题描述】:

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

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”。 我们看到了结果。在您创建片段之前,动画视图不会开始。

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

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

【问题讨论】:

  • 更多代码?让我试试看~
  • 你的视图在你的片段中吗??

标签: android animation android-fragments view


【解决方案1】:
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中试过,上面的代码,view和fragment的动画一起播放。 mTestAnimationView 是布局在片段容器(R.id.simple_fragment)上方的 textView。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多