我想达到特定的片段转换行为,正如我在问题中提到的那样,我希望我的片段从右侧出现并以相同的方向消失,回到它开始的点。这些是我的 xml 动画:
right_to_left.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromXDelta="100%"
android:fromYDelta="0%"
android:toXDelta="0%"
android:toYDelta="0%" />
left_to_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="0%"
android:fromYDelta="0%"
android:toXDelta="-100%"
android:toYDelta="0%" />
以下是我尝试实现片段动画的不同方式:
当片段出现时:
productHolder.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putSerializable(Utils.productDetailsKey, productPojo);
ProductDetailsFragment productDetailsFragment = new ProductDetailsFragment();
productDetailsFragment.setArguments(bundle);
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setCustomAnimations(R.anim.product_animation_enter, R.anim.product_animation_enter);
transaction.add(R.id.newContainer, productDetailsFragment);
transaction.commit();
MainActivity.transparent.setBackgroundColor(ContextCompat.getColor(context, R.color.blackTransparent));
}
});
当片段消失时方法1:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.product_animation_exit, R.anim.product_animation_exit);
transaction.remove(ProductDetailsFragment.this);
transaction.commitAllowingStateLoss();
MainActivity.transparent.setBackgroundColor(0x00000000);
}
});
当片段消失时方法2:
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.product_animation_exit);
animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
try {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.remove(ProductDetailsFragment.this);
transaction.commitAllowingStateLoss();
MainActivity.transparent.setBackgroundColor(0x00000000);
} catch (Exception e) {
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
getView().startAnimation(animation);
}
});
不幸的是,这些都没有像我预期的那样工作,希望这张图片可以解释更多我想要的东西:图片 提前谢谢你。