直接上代码:
1.这个是相对于自身往上平移自身高度的动画
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f,
Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,-1.0f);
2.这个是相对于自身往下平移自身高度的动画
TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
其实就是对于
public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
int fromYType, float fromYValue, int toYType, float toYValue)
这个方法的调用。
不过在动画的执行的地方有不同,不是通过Start()方法执行动画,而是通过设置View 的Visibility来控制动画的播放。
比如这里有一个需要隐藏的View 变量名为topLayout,
那么这里实现动画的方式就为
toplayout.setAnimation(translateAnimation);
toplayout.setVisibility(View.INVISIBLE);
这里就可以实现动画的播放,这里设置的是INVISIBLE,是因为我们的控件本来是可见的(VISIBLE),所以动画的控制是通过View的可见性来控制的,也就是说,这里也可以设置为View.GONE,也是可以触发动画。当处于INVISIBLE或者GONE的时候,设置为VISIBLE就可以播放动画了。
下面上效果: