我有一个线性布局,我想显示或隐藏与动画,推动布局向上或向下每当我改变其可见性。
我在那里看过一些样品,但没有一个适合我的需要。
我已经为动画创建了两个xml文件,但我不知道当我改变线性布局的可见性时如何启动它们。
我有一个线性布局,我想显示或隐藏与动画,推动布局向上或向下每当我改变其可见性。
我在那里看过一些样品,但没有一个适合我的需要。
我已经为动画创建了两个xml文件,但我不知道当我改变线性布局的可见性时如何启动它们。
当前回答
如果你想同时显示/隐藏多个视图,你可以使用TransitionSet(因为你不能同时播放2个“单个”过渡)
fun slideTopBottomVisibility(topLayout: View, bottomLayout: View, show: Boolean) {
val topTransition: Transition = Slide(Gravity.TOP)
topTransition.duration = 600
topTransition.addTarget(topLayout)
val bottomTransition: Transition = Slide(Gravity.BOTTOM)
bottomTransition.duration = 600
bottomTransition.addTarget(bottomLayout)
val transitionSet = TransitionSet()
transitionSet.addTransition(topTransition)
transitionSet.addTransition(bottomTransition)
TransitionManager.beginDelayedTransition(topLayout.parent as ViewGroup, transitionSet)
topLayout.visibility = if (show) View.VISIBLE else View.GONE
bottomLayout.visibility = if (show) View.VISIBLE else View.GONE
}
其他回答
随着Android 3.0 (Honeycomb)引入的新动画API,创建这样的动画非常简单。
将视图向下滑动一段距离:
view.animate().translationY(distance);
你可以稍后将视图滑回原来的位置,如下所示:
view.animate().translationY(0);
您还可以轻松地组合多个动画。下面的动画会将一个视图按高度向下滑动,同时淡入:
// Prepare the View for the animation
view.setVisibility(View.VISIBLE);
view.setAlpha(0.0f);
// Start the animation
view.animate()
.translationY(view.getHeight())
.alpha(1.0f)
.setListener(null);
然后,您可以将视图淡出,并将其滑回原始位置。我们还设置了一个AnimatorListener,这样我们可以在动画完成后将视图的可见性设置为GONE:
view.animate()
.translationY(0)
.alpha(0.0f)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.setVisibility(View.GONE);
}
});
使用ObjectAnimator
private fun slideDown(view: View) {
val height = view.height
ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, 0f, height.toFloat()).apply {
duration = 1000
start()
}
}
private fun slideUp(view: View) {
val height = view.height
ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, height.toFloat(), 0f)).apply {
duration = 1000
start()
}
}
从ashakirov的回答为Kotlin用户
val transition: Transition = Slide(Gravity.BOTTOM)
transition.duration = 600
transition.addTarget(you_parent_layout_id)
TransitionManager.beginDelayedTransition(rootLayoutId, transition)
yourViewIdToHide.visibility = if (yourViewIdToHide.isShown) View.GONE else View.VISIBLE
科特林
根据Suragch的回答,下面是一种使用View扩展的优雅方式:
fun View.slideUp(duration: Int = 500) {
visibility = View.VISIBLE
val animate = TranslateAnimation(0f, 0f, this.height.toFloat(), 0f)
animate.duration = duration.toLong()
animate.fillAfter = true
this.startAnimation(animate)
}
fun View.slideDown(duration: Int = 500) {
visibility = View.VISIBLE
val animate = TranslateAnimation(0f, 0f, 0f, this.height.toFloat())
animate.duration = duration.toLong()
animate.fillAfter = true
this.startAnimation(animate)
}
然后无论你想在哪里使用它,你只需要myview。slideup()或myview。slidedown ()
你可以上下滑动任何视图或布局使用咆哮的代码在安卓应用程序
boolean isClicked = false;
LinearLayout mLayoutTab = (LinearLayout) findViewById(R.id.linearlayout);
if(isClicked) {
isClicked = false;
mLayoutTab.animate()
.translationYBy(120)
.translationY(0)
.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
} else {
isClicked = true;
mLayoutTab.animate()
.translationYBy(0)
.translationY(120)
.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
}