我正在遵循this教程,以在滑动时上下滑动片段。但是以某种方式向下滑动动画不起作用,片段停留在相同位置。滑动动画效果很好。我已将动画放在childFragment上
下面是我的片段xml:
<com.xyz.utils.SlidingRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent_layout_discovery"
android:background="?android:windowBackground"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="@+id/bottom_level_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.SearchView
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_10sdp"
android:background="@drawable/searchview_style"
android:clickable="true"
android:focusable="true"
android:layoutDirection="rtl"
app:defaultQueryHint="Search" />
<TextView
android:id="@+id/noResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="@dimen/_10sdp"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="@dimen/_18sdp"
android:textStyle="italic|bold"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_new_discover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/search"/>
<TextView
android:id="@+id/txt_credit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:background="@color/color_txtWhite"
android:gravity="center"
android:padding="@dimen/_4sdp"
android:textAlignment="center"
android:textColor="@color/colorAccent"
android:visibility="gone" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/_300sdp"
android:layout_gravity="bottom"
android:background="#80000000"
android:visibility="gone"
android:clickable="true"
android:gravity="center">
<TextView
android:id="@+id/home_intro_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:padding="@dimen/_10sdp"
android:textColor="@color/white"
android:textSize="@dimen/_30sdp"
android:text="Walk through makeup tutorial side-by-side your face!"
android:textStyle="italic|bold" />
</RelativeLayout>
</com.xyz.SlidingRelativeLayout>
Below is the custom sliding layout:
public class SlidingRelativeLayout extends RelativeLayout {
private float yFraction = 0;
public SlidingRelativeLayout(Context context) {
super(context);
}
public SlidingRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SlidingRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private ViewTreeObserver.OnPreDrawListener preDrawListener = null;
public void setYFraction(float fraction) {
this.yFraction = fraction;
if (getHeight() == 0) {
if (preDrawListener == null) {
preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(preDrawListener);
setYFraction(yFraction);
return true;
}
};
getViewTreeObserver().addOnPreDrawListener(preDrawListener);
}
return;
}
float translationY = getHeight() * fraction;
setTranslationY(translationY);
}
public float getYFraction() {
return this.yFraction;
}
}
在我的父片段中,我正在像这样制作片段动画:
public class MirrorFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_mirror, null);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mContext = getContext();
fragment = (MirrorFragment) getFragmentManager().findFragmentByTag("mirror_frag");
initViews(view);
// setWidthOfBottomContainer();
isUp = false;
currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
// Landscape
cameraView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public void onSwipeLeft() {
super.onSwipeLeft();
onSlideLeftViewButtonClick();
Toast.makeText(mContext, "swipe left", Toast.LENGTH_LONG).show();
}
@Override
public void onSwipeRight() {
super.onSwipeRight();
onSlideLeftViewButtonClick();
Toast.makeText(mContext, "swipe right", Toast.LENGTH_LONG).show();
}
});
} else {
// Portrait
cameraView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
@Override
public void onSwipeUp() {
super.onSwipeUp();
onSlideViewButtonClick();
ConstraintLayout.LayoutParams up_arrowParams = (ConstraintLayout.LayoutParams) up_arrow.getLayoutParams();
up_arrowParams.verticalBias = 0.45f; // here is one modification for example. modify anything else you want :)
up_arrow.setLayoutParams(up_arrowParams);
ConstraintLayout.LayoutParams recorder = (ConstraintLayout.LayoutParams) iv_record.getLayoutParams();
recorder.verticalBias = 0.45f; // here is one modification for example. modify anything else you want :)
iv_record.setLayoutParams(recorder);
Toast.makeText(mContext, "swipe up", Toast.LENGTH_LONG).show();
}
@Override
public void onSwipeDown() {
super.onSwipeDown();
onSlideViewButtonClick();
ConstraintLayout.LayoutParams up_arrowParams = (ConstraintLayout.LayoutParams) up_arrow.getLayoutParams();
up_arrowParams.verticalBias = 0.90f; // here is one modification for example. modify anything else you want :)
up_arrow.setLayoutParams(up_arrowParams);
ConstraintLayout.LayoutParams recorder = (ConstraintLayout.LayoutParams) iv_record.getLayoutParams();
recorder.verticalBias = 0.90f; // here is one modification for example. modify anything else you want :)
iv_record.setLayoutParams(recorder);
Toast.makeText(mContext, "swipe down", Toast.LENGTH_LONG).show();
}
});
}
up_arrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSlideViewButtonClick();
}
});
iv_record.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mCapturingVideo){
stopVideo();
}else{
captureVideo();
}
}
});
cameraView.addCameraListener(new CameraListener() {
@Override
public void onVideoTaken(File video) {
super.onVideoTaken(video);
Log.d("Video_path", video.toString());
}
@Override
public void onOrientationChanged(int orientation) {
super.onOrientationChanged(orientation);
}
@Override
public void onZoomChanged(float newValue, float[] bounds, PointF[] fingers) {
super.onZoomChanged(newValue, bounds, fingers);
}
});
}
private void setWidthOfBottomContainer() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
// code for portrait mode
param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) height / 2);
param.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// mDraggingPanel.setLayoutParams(param);
} else {
// code for landscape mode
param = new RelativeLayout.LayoutParams((int) width / 2, ViewGroup.LayoutParams.MATCH_PARENT);
//mDraggingPanel.setLayoutParams(param);
}
}
private void initViews(View view) {
btn_control = view.findViewById(R.id.btn_control);
slidingView = view.findViewById(R.id.sliding_drawer);
frameContainer = view.findViewById(R.id.frame_video);
cameraView = view.findViewById(R.id.camera);
up_arrow = view.findViewById(R.id.up_arrow);
iv_record = view.findViewById(R.id.record);
cameraView.setLifecycleOwner(fragment.getViewLifecycleOwner());
cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM); // Pinch to zoom!
toLandAnim = AnimationUtils.loadAnimation(mContext, R.anim.view_to_landscape);
toPortAnim = AnimationUtils.loadAnimation(mContext, R.anim.view_to_portrait);
childFragment = new DiscoveryFragment();
/* FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.bottom_up, R.anim.bottom_down);
Bundle args = new Bundle();
args.putString("parent_fragment", "mirror_frag");
childFragment.setArguments(args);
transaction.replace(R.id.frame_video, childFragment,"discovery_frag");
transaction.addToBackStack(null);
transaction.commit();*/
}
private void toggleList() {
Fragment f = getFragmentManager().findFragmentByTag("discovery_frag");
if (f != null) {
getFragmentManager().popBackStack();
} else {
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.bottom_up, R.anim.bottom_down,R.anim.bottom_up,R.anim.bottom_down);
Bundle args = new Bundle();
args.putString("parent_fragment", "mirror_frag");
childFragment.setArguments(args);
transaction.replace(R.id.frame_video, childFragment,"discovery_frag");
transaction.addToBackStack(null);
transaction.commit();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE || newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
try {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onResume() {
super.onResume();
cameraView.start();
}
@Override
public void onPause() {
super.onPause();
cameraView.stop();
}
@Override
public void onDestroy() {
super.onDestroy();
cameraView.destroy();
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
super.onMultiWindowModeChanged(isInMultiWindowMode);
}
private void getHeightWidth() {
/* draggingPanelHeight = mLayout.getHeight();
draggingPanelWidth = mLayout.getWidth();*/
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
}
private void captureVideo() {
if (cameraView.getSessionType() != SessionType.VIDEO) {
Toast.makeText(getContext(), "Can't record video while session type is 'picture'.", Toast.LENGTH_SHORT).show();
return;
}
if (mCapturingVideo) return;
mCapturingVideo = true;
Toast.makeText(getContext(), "Recording started...", Toast.LENGTH_SHORT).show();
// message("Recording for 8 seconds...", true);
cameraView.startCapturingVideo(CommonUtilities.getVideoFilePath("lips"));
iv_record.setImageResource(R.drawable.recording_on);
}
private void stopVideo() {
if (cameraView.getSessionType() != SessionType.VIDEO) {
Toast.makeText(getContext(), "Can't stop record video while session type is 'picture'.", Toast.LENGTH_SHORT).show();
// message("Can't record video while session type is 'picture'.", false);
return;
}
if (mCapturingVideo) {
cameraView.stopCapturingVideo();
iv_record.setImageResource(R.drawable.recording_off);
if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
mNextVideoAbsolutePath = getVideoFilePath(getActivity(), "makeup");
}
// recorder.setOutputFile(mNextVideoAbsolutePath);
Toast.makeText(getContext(), "Recording stopped...", Toast.LENGTH_SHORT).show();
mCapturingVideo = false;
}
}
private String getVideoFilePath(Context context, String name) {
name = "smartmirror";
final File dir = context.getExternalFilesDir(null);
return (dir == null ? "" : (dir.getAbsolutePath() + "/")) + name + VIDEO_EXTENSION;
}
// slide the view from below itself to the current position
//TODO remove hardcoded height with the 50% of the screen
public void onSlideViewButtonClick() {
if (isUp) {
/* Animation bottomdown = AnimationUtils.loadAnimation(getContext(),
R.anim.bottom_down);
// CommonUtilities.slideDown(slidingView);
slidingView.startAnimation(bottomdown);
slidingView.setVisibility(View.GONE);*/
toggleList();
// slidingView.setVisibility(View.GONE);
} else {
/*Animation bottomUp = AnimationUtils.loadAnimation(getContext(),
R.anim.bottom_up);
// CommonUtilities.slideUp(slidingView);
slidingView.startAnimation(bottomUp);
slidingView.setVisibility(View.VISIBLE);*/
toggleList();
//slidingView.setVisibility(View.VISIBLE);
}
isUp = !isUp;
}
public void onSlideLeftViewButtonClick() {
if (isUp) {
CommonUtilities.slideToRight(slidingView);
} else {
CommonUtilities.slideToLeft(slidingView);
}
isUp = !isUp;
}
}
两个动画类是: bottom_down:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="yFraction"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="1.0"
android:duration="@android:integer/config_mediumAnimTime"/>
<objectAnimator
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="alpha"
android:valueType="floatType"
android:valueFrom="1"
android:valueTo="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
bottom_up:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="yFraction"
android:valueType="floatType"
android:valueFrom="0.5"
android:valueTo="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<objectAnimator
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="alpha"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="1"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
我不确定是什么问题。我遵循了教程中的确切说明。有人可以告诉我如何实现这一目标。