当用户从底部和顶部视图滑动时,我需要创建动画也反映并合并视图。
我使用了bottomsheet和topbottomsheet,但没有运气。
我还尝试了运动布局来执行此操作,但没有给出预期的结果。
这是示例代码:
topBehaviour = TopSheetBehavior.from(cl_pickup_drop);
slidingLayoutInital = findViewById(R.id.slidingLayoutInital);
behaviorInitial = BottomSheetBehavior.from(slidingLayoutInital);
behaviorInitial.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
Log.d("SHUBHAMNEWS",newState+"");
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
new Handler().post(new Runnable() {
@Override
public void run() {
cl_pickup_drop.setVisibility(View.GONE);
searchModeClicked = PlaceSearchListFragment.PlaceSearchMode.DROP.getOrdinal();
openPickupDropSearchUI(PlaceSearchListFragment.PlaceSearchMode.PICKUP_AND_DROP);
behaviorInitial.setState(BottomSheetBehavior.STATE_COLLAPSED);
cl_pickup_drop.setVisibility(View.VISIBLE);
}
});
} else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
slidingLayoutInital.animate().alpha(1f).setDuration(100);
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
int[] locationOnScreen = new int[2];
bottomSheet.getLocationOnScreen(locationOnScreen);
double newvalue = 1.0 - (slideOffset+0.2);
Log.d(
"SHUBHAM slideOffset",
slideOffset+"--"+newvalue + "--"+cl_pickup_drop.getMeasuredHeight()
);
if(slideOffset == 0.0){
//slidingLayoutInital.animate().alpha(1f).setDuration(100);
vvCouponCode.animate().alpha(1f).setDuration(700);
llSetWhereTo.animate().alpha(1f).setDuration(700);
}else if(slideOffset==1){
relDestinationWhereTo.setBackground(getDrawable(R.drawable.bg_menu_item_color_black));
relSourceWhereTo.setBackground(getDrawable(R.drawable.bg_menu_item_color_f7_r));
}else{
vvCouponCode.animate().alpha((float) newvalue).setDuration(700);
llSetWhereTo.animate().alpha((float) newvalue).setDuration(700);
// slidingLayoutInital.animate().alpha(0.5f).setDuration(100);
}
new Handler().post(new Runnable() {
@Override
public void run() {
//int mVal = cl_pickup_drop.getMeasuredHeight() - (locationOnScreen[1]);
int mVal = cl_pickup_drop.getMeasuredHeight() - (locationOnScreen[1]);
double calcualtePerce = (screenHeight / 100.0f) * 22;
if(initialMyLocationBtn.getVisibility() == View.VISIBLE){
calcualtePerce = calcualtePerce + (screenHeight / 100.0f) * 4;
}
if(relativeLayoutPoolInfoBar.getVisibility() == View.VISIBLE){
calcualtePerce = calcualtePerce + (screenHeight / 100.0f) * 4;
}
double mValWithPlus = mVal + calcualtePerce;
//Log.d("HomeBehaviour FINAL1",String.valueOf(cl_pickup_drop.getMeasuredHeight()));
//Log.d("HomeBehaviour FINAL2",String.valueOf(locationOnScreen[1]));
//Log.d("HomeBehaviour FINAL3",String.valueOf(mVal));
Log.d("HomeBehaviour FINAL+50",String.valueOf(mValWithPlus));
if(mValWithPlus > cl_pickup_drop.getMeasuredHeight()){
topBehaviour.setPeekHeight((int) cl_pickup_drop.getMeasuredHeight());
}else{
topBehaviour.setPeekHeight((int) mValWithPlus);
}
cl_pickup_drop.requestLayout();
bottomSheet.requestLayout();
// Code here will run in UI thread
}
});
}
});
https://github.com/carlos-mg89/TopSheetBehavior
附上示例视频:
https://www.dropbox.com/s/ka9atmtg1thydf9/WhatsApp%20Video%202022-08-13%20at%201.10.55%20PM.mp4?dl=0
试试这个:
在 res 文件夹中创建 anim 文件夹并复制这四个文件:
slide_in_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p"
android:duration="@android:integer/config_longAnimTime"/>
slide_out_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0"
android:duration="@android:integer/config_longAnimTime" />
slide_in_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:toYDelta="0%p"
android:duration="@android:integer/config_longAnimTime" />
slide_out_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:toYDelta="100%p"
android:duration="@android:integer/config_longAnimTime" />
当您单击图像视图时调用:
overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom);
当您点击原始地点呼叫时:
overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_top);