Android 从左到右动画滑动面板?
我正在尝试在我的布局中添加一个类似于滑动抽屉的滑动面板,只是它将被放置在主布局的左侧,而不是覆盖它。在我的布局的左上角有一个小按钮,当我点击它时,它会展开/折叠面板。当它展开/折叠时,我希望动画平滑,以便相邻的视图也会移动。这是我试过的代码。面板在第一次展开/折叠后停止工作:Android 从左到右动画滑动面板?,android,animation,expand,collapse,Android,Animation,Expand,Collapse,我正在尝试在我的布局中添加一个类似于滑动抽屉的滑动面板,只是它将被放置在主布局的左侧,而不是覆盖它。在我的布局的左上角有一个小按钮,当我点击它时,它会展开/折叠面板。当它展开/折叠时,我希望动画平滑,以便相邻的视图也会移动。这是我试过的代码。面板在第一次展开/折叠后停止工作: public Animation expandHiddenPanel(final View v, final boolean expand) { panelExpanded = expand; v.meas
public Animation expandHiddenPanel(final View v, final boolean expand) {
panelExpanded = expand;
v.measure(MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
final int initialWidth = v.getMeasuredWidth();
Log.i("test", "initialWidth = " + initialWidth);
v.getLayoutParams().width = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int newWidth;
if (expand) {
newWidth = (int)(initialWidth * interpolatedTime);
Log.i("test", "new Width = " + newWidth);
}
else {
newWidth = (int)(initialWidth * (1 - interpolatedTime));
Log.i("test", "new Width = " + newWidth);
}
v.getLayoutParams().width = newWidth;
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setInterpolator(new AccelerateInterpolator());
a.setDuration(2500);
v.startAnimation(a);
return a;
}
确保如果您想以不断增长的宽度显示动画,请在xml文件中提及特定于布局的宽度,并使用以下代码以宽度展开和折叠动画
//expand animation
public static void expand(final View v) {
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
final int targtetWidth = v.getMeasuredWidth();
Log.v("view width", "view expand width==>"+targtetWidth);
v.getLayoutParams().width = 0;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,Transformation t) {
v.getLayoutParams().width = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT: (int)(targtetWidth * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
a.setDuration(100);
v.startAnimation(a);
}
//collapse animation
public static void collapse(final View v) {
final int initialWidth = v.getMeasuredWidth();
Log.v("initial width", "initial width==>"+initialWidth);
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
if (interpolatedTime == 1) {
Log.v("interpolated", "interpolated time is 1");
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().width = initialWidth - (int) (initialWidth * interpolatedTime);
Log.v("interpolated", "interpolated time is===>"+v.getLayoutParams().width);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
Log.v("duration", "duration for collapse==>"+((int)(initialWidth /v.getContext().getResources().getDisplayMetrics().density)));
a.setDuration((int) (initialWidth / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}