【发布时间】:2015-05-14 00:29:34
【问题描述】:
我在创建动画时遇到问题。我在操作栏上有一个按钮,可以点击它,或者显示或隐藏菜单栏。到目前为止,它使用 GONE 和 VISIBLE 显示或隐藏。我想添加一个动画,这个菜单就在操作栏的下方,所以当我点击隐藏菜单时,我希望它向上移动,隐藏它。通过单击显示菜单,我希望它向下移动,显示它。另一个问题是布局的其余部分应该跟随所选的移动。有谁知道如何做到这一点?谢谢!
编辑:
public class HideAnimation extends Animation {
int targetHeight;
int orgHeight;
View view;
public HideAnimation(View view, int targetHeight) {
this.view = view;
this.targetHeight = targetHeight;
orgHeight=view.getHeight();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) ((orgHeight-targetHeight) * (1-interpolatedTime))+targetHeight;
view.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
}
public class ShowAnimation extends Animation {
int targetHeight;
View view;
public ShowAnimation(View view, int targetHeight) {
this.view = view;
this.targetHeight = targetHeight;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
view.getLayoutParams().height = (int) (targetHeight * interpolatedTime);
view.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
}
public void collapseControlMenu(boolean showEffect) {
final View controlScreen = (View) findViewById(R.id.volume_fragment);
if (controlScreen != null) {
MainApp.mShowControl = false;
if (showEffect) {
/*ObjectAnimator anim = ObjectAnimator.ofFloat(controlScreen, "translationY", -ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
anim.setDuration(500);
anim.start();
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mContactFragment.getListView(), "translationY", -ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
anim2.setDuration(500);
anim2.start();*/
Animation ani = new HideAnimation(controlScreen, 0);
ani.setDuration(500);
controlScreen.startAnimation(ani);
/*ViewPropertyAnimator anim = controlScreen.animate().translationYBy(0).translationY(-ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
anim.setDuration(500);
anim.start();
if (!isContactsFragmentVisible()) { // Recents
anim = mRecentFragment.getListView().animate();
} else {
anim = mContactFragment.getListView().animate();
}
anim.translationYBy(0).translationY(-ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
anim.setDuration(500);
anim.start();*/
} else {
controlScreen.setVisibility(View.GONE);
}
}
}
public void expandControlMenu(boolean showEffect) {
final View controlScreen = (View) findViewById(R.id.volume_fragment);
if (controlScreen != null) {
MainApp.mShowControl = true;
setControlOptions();
if (showEffect) {
/*ObjectAnimator anim = ObjectAnimator.ofFloat(controlScreen, "translationY", 0);
anim.setDuration(500);
anim.start();
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mContactFragment.getListView(), "translationY", 0);
anim2.setDuration(500);
anim2.start();*/
Animation ani = new ShowAnimation(controlScreen, (int) ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
ani.setDuration(500);
controlScreen.startAnimation(ani);
/*ViewPropertyAnimator anim = controlScreen.animate().translationYBy(-ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height)).translationY(0);
anim.setDuration(500);
anim.start();
if (!isContactsFragmentVisible()) { // Recents
anim = mRecentFragment.getListView().animate();
} else {
anim = mContactFragment.getListView().animate();
}
anim.translationY(0);
anim.setDuration(500);
anim.start();*/
} else {
controlScreen.setVisibility(View.VISIBLE);
}
}
}
【问题讨论】: