方向更改后,Android翻转动画会闪烁

时间:2017-05-25 23:13:21

标签: android android-fragments android-viewpager android-orientation

我正在使用像app这样的闪卡,我一直在和翻转动画进行摔跤。我将我的代码基于android的AnimationsDemo,不同之处在于它是在片段而不是活动中完成的。 它工作正常,不完全像上述演示的预览,但足够体面。改变方向后...... 如果应用程序是在风景中启动的,它在风景中工作得很好,但是在肖像方面很怪异,如果应用程序是以肖像方式启动的,那么它可以在纵向和怪异的环境中工作。

visualisation of the "freaking"

这是我的代码(我明显地浏览了数据模型)

public class MyFlashCardFragment extends Fragment {
private MyDataModel model;
private Handler mHandler = new Handler();
private boolean mShowingBack = false;
FrameLayout frameLayout;

public static MyFlashCardFragment create(MyDataModel m) {
    MyFlashCardFragment fragment = new MyFlashCardFragment();
    Bundle bundle = new Bundle();
    bundle.putString(AppKeys.FRONT_KEY,m.getFrontText());
    bundle.putString(AppKeys.BACK_KEY,m.getBackText());
    fragment.setArguments(bundle);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!getArguments().isEmpty()) {
        model = new MyDataModel();
        model.setFrontText(getArguments().getString(AppKeys.FRONT_KEY));
        model.setBackText(getArguments().getString(AppKeys.BACK_KEY));
    }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    if (model == null) {
        return super.onCreateView(inflater, container, savedInstanceState);
    } else {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_flashcard, container, false);
        frameLayout = (FrameLayout) root.findViewById(R.id.chilfd_frame);
        Button btn_overlay = (Button) root.findViewById(R.id.overlaybutton);
        btn_overlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipCard();
            }
        });

        return root;
    }
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if(savedInstanceState == null) {
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.chilfd_frame, CardFrontFragment.create(model.getFrontText()))
                .commit();
    } else {
        mShowingBack = (getChildFragmentManager().getBackStackEntryCount() > 0);
    }
    getChildFragmentManager().addOnBackStackChangedListener(new android.app.FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            mShowingBack = (getChildFragmentManager().getBackStackEntryCount() > 0);
            getActivity().invalidateOptionsMenu();
        }
    });
}

private void flipCard() {
    if (mShowingBack) {
        getChildFragmentManager().popBackStack();
        return;
    }

    mShowingBack = true;

    getChildFragmentManager()
            .beginTransaction()
            .setCustomAnimations(
                    R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                    R.animator.card_flip_left_in, R.animator.card_flip_left_out)
            .replace(frameLayout.getId(), CardBackFragment.create(model.getBackText()))
            .addToBackStack(null)
            .commit();
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            getActivity().invalidateOptionsMenu();
        }
    });
}


/**
 * A fragment representing the front of the card.
 */
public static class CardFrontFragment extends Fragment {
    String frontText;

    public static CardFrontFragment create(String frontText) {
        CardFrontFragment fragment = new CardFrontFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AppKeys.FRONT_KEY,frontText);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!getArguments().isEmpty()) {
            frontText = getArguments().getString(AppKeys.FRONT_KEY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (TextUtils.isEmpty(frontText)) {
            return super.onCreateView(inflater, container, savedInstanceState);
        } else {
            ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_flashcard_front, container, false);

            ((TextView) rootView.findViewById(R.id.tv_name)).setText(frontText);


            return rootView;
        }
    }
}

/**
 * A fragment representing the back of the card.
 */
public static class CardBackFragment extends Fragment {
    String backText;

    public static CardBackFragment create(String backText) {
        CardBackFragment fragment = new CardBackFragment();
        Bundle bundle = new Bundle();
        bundle.putString(AppKeys.BACK_KEY,backText);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!getArguments().isEmpty()) {
            backText = getArguments().getString(AppKeys.BACK_KEY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (TextUtils.isEmpty(backText)) {
            return super.onCreateView(inflater, container, savedInstanceState);
        } else {
            ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_flashcard_back, container, false);

            ((TextView) rootView.findViewById(R.id.tv_name)).setText(backText);


            return rootView;
        }
    }
}

}

父片段从viewpager加载:

pager = (ViewPager) findViewById(R.id.pager);
    adapter = new FlashCardPageAdapter(getFragmentManager(), getAllFrom(ref));
    pager.setAdapter(adapter);
    pager.setCurrentItem(position,true);

    pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            invalidateOptionsMenu();
        }
    });

使用的适配器是:

public class FlashCardPageAdapter  extends FragmentStatePagerAdapter {
private ArrayList<MyDataModel> Models;

public FlashCardPageAdapter(FragmentManager fm, ArrayList<MyDataModel> list) {
    super(fm);
    this.Models = list;
}

@Override
public Fragment getItem(int position) {
    return FlashCardFragment.create(Models.get(position));
}

@Override
public int getCount() {
    return Models.size();
}

}

我还尝试从Animator对象创建动画,并将其添加到透明按钮,并将视图设置为进出并切换可见性,结果是相同的。

欢迎任何想法/建议。

1 个答案:

答案 0 :(得分:0)

事实证明我的代码中没有任何遗漏,问题与模拟器有关。好吧,至少看起来那样。我在2个真实设备(1个电话/ 1个平板电脑)上尝试过它,问题没有显现在它们上面,因此可能是AVD错误或模拟方向改变的简单缺点。