Android:使用animation显示/隐藏视图

我一直在浏览很多谷歌的结果/问题,以确定如何通过垂直animation显示/隐藏视图,但我似乎无法find一个完全正确或不太模糊的。

我有一个布局(撤消栏),在另一个布局下面,并在其他多个小部件之上; 此撤消栏应根据具体情况垂直滑动打开并滑动closures。

目前我所做的只是将视图设置为可见或不可见。

请帮忙。

在父布局中设置属性android:animateLayoutChanges="true"

如果不是,则将视图放置在布局中,并为该布局设置android:animateLayoutChanges="true"

注意:这只适用于API Level 11+(Android 3.0)

我为RelativeLayout创build了一个扩展,显示/隐藏布局与animation。 它可以扩展任何types的View以获得这些function。

 import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.widget.RelativeLayout; public class AnimatingRelativeLayout extends RelativeLayout { Context context; Animation inAnimation; Animation outAnimation; public AnimatingRelativeLayout(Context context) { super(context); this.context = context; initAnimations(); } public AnimatingRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; initAnimations(); } public AnimatingRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.context = context; initAnimations(); } private void initAnimations() { inAnimation = (AnimationSet) AnimationUtils.loadAnimation(context, R.anim.in_animation); outAnimation = (Animation) AnimationUtils.loadAnimation(context, R.anim.out_animation); } public void show() { if (isVisible()) return; show(true); } public void show(boolean withAnimation) { if (withAnimation) this.startAnimation(inAnimation); this.setVisibility(View.VISIBLE); } public void hide() { if (!isVisible()) return; hide(true); } public void hide(boolean withAnimation) { if (withAnimation) this.startAnimation(outAnimation); this.setVisibility(View.GONE); } public boolean isVisible() { return (this.getVisibility() == View.VISIBLE); } public void overrideDefaultInAnimation(Animation inAnimation) { this.inAnimation = inAnimation; } public void overrideDefaultOutAnimation(Animation outAnimation) { this.outAnimation = outAnimation; } } 

您可以使用overrideDefaultInAnimationoverrideDefaultOutAnimationoverrideDefaultInAnimation原始的Animation

我的原始animation是fadeIn / Out,我添加了用于翻译进/出屏幕的XMLanimation文件(转换顶部和顶部)

in_animation.xml:

  <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="600" android:fillAfter="false" android:fromXDelta="0" android:fromYDelta="-100%p" android:toXDelta="0" android:toYDelta="0" /> 

out_animation.xml:

  <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="600" android:fillAfter="false" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="-100%p" /> 

这可以合理地在API 12及更高版本的单行声明中实现。 下面是一个例子,其中v是你想要animation的视图;

 v.animate().translationXBy(-1000).start(); 

这会将问题的View向左滑动1000px。 要将视图滑回到用户界面上,我们可以简单地执行以下操作。

 v.animate().translationXBy(1000).start(); 

我希望有人认为这有用。

如果你只想animation视图的高度(从0到某个数字),你可以实现你自己的animation:

 final View v = getTheViewToAnimateHere(); Animation anim=new Animation(){ protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); // Do relevant calculations here using the interpolatedTime that runs from 0 to 1 v.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, (int)(30*interpolatedTime))); }}; anim.setDuration(500); v.startAnimation(anim); 

我用这两个函数来平滑地隐藏和显示带有过渡animation的视图。

 @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void expand(final View v, int duration, int targetHeight, final int position) { int prevHeight = v.getHeight(); v.setVisibility(View.VISIBLE); ValueAnimator valueAnimator = ValueAnimator.ofInt(0, targetHeight); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { v.getLayoutParams().height = (int) animation.getAnimatedValue(); v.requestLayout(); } }); valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.setDuration(duration); valueAnimator.start(); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { v.clearAnimation(); } }); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void collapse(final View v, int duration, int targetHeight, final int position) { if (position == (data.size() - 1)) { return; } int prevHeight = v.getHeight(); ValueAnimator valueAnimator = ValueAnimator.ofInt(prevHeight, targetHeight); valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { v.getLayoutParams().height = (int) animation.getAnimatedValue(); v.requestLayout(); } }); valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.setDuration(duration); valueAnimator.start(); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { animBoolArray.put(position, false); v.clearAnimation(); } }); } 

也许你正在寻找的是一个SlidingDrawer 。

尝试使用TranslateAnimation类,该类创build位置更改的animation。 尝试阅读此帮助 – http://developer.android.com/reference/android/view/animation/TranslateAnimation.html

更新:这是这个例子。 如果你的视图的高度是50,而在隐藏模式下你只想显示10像素。 示例代码将是 –

 TranslateAnimation anim=new TranslateAnimation(0,0,-40,0); anim.setFillAfter(true); view.setAnimation(anim); 

PS:有很多或其他的方法来帮助你根据你的需要使用animation。 如果你想完全自定义代码,也可以看看RelativeLayout.LayoutParams,但是使用TranslateAnimation更容易使用。

编辑: – 使用LayoutParams的复杂版本

 RelativeLayout relParam=new RelativeLayout.LayoutParam(RelativeLayout.LayoutParam.FILL_PARENT,RelativeLayout.LayoutParam.WRAP_CONTENT); //you can give hard coded width and height here in (width,height) format. relParam.topMargin=-50; //any number that work.Set it to 0, when you want to show it. view.setLayoutParams(relparam); 

这个示例代码假定你把你的视图放在RelativeLayout中,如果不改变Layout的名字,但是其他的布局可能不起作用。 如果你想给它们一个animation效果,请缓慢地减小或增加topMargin。 你也可以考虑使用Thread.sleep()。

尝试这个。

 view.animate() .translationY(0) .alpha(0.0f) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); view.setVisibility(View.GONE); } }); 

首先得到视图的高度哟要看到,并作出布尔保存如果视图显示:

 int heigth=0; boolean showing=false; LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout); proDetailsLL.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // gets called after layout has been done but before display // so we can get the height then hide the view proHeight = proDetailsLL.getHeight(); // Ahaha! Gotcha proDetailsLL.getViewTreeObserver().removeGlobalOnLayoutListener(this); proDetailsLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0)); } }); 

然后调用显示隐藏视图的方法,并更改布尔值:

 slideInOutAnimation(showing, heigth, layout); proShowing = !proShowing; 

方法:

 /** * Method to slide in out the layout * * @param isShowing * if the layout is showing * @param height * the height to slide * @param slideLL * the container to show */ private void slideInOutAnimation(boolean isShowing, int height, final LinearLayout slideLL, final ImageView arroIV) { if (!isShowing) { Animation animIn = new Animation() { protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); // Do relevant calculations here using the interpolatedTime that runs from 0 to 1 slideLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (heigth * interpolatedTime))); } }; animIn.setDuration(500); slideLL.startAnimation(animIn); } else { Animation animOut = new Animation() { protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); // Do relevant calculations here using the interpolatedTime that runs from 0 to 1 slideLL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (heigth * (1 - interpolatedTime)))); } }; animOut.setDuration(500); slideLL.startAnimation(animOut); } }