用平移动画实现shake
首先讲一下平移动画的使用吧
看一下构造方法
- TranslateAnimation(Context context, AttributeSet attrs) :参数2: 是一个属性,基本没怎么用过
- TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) 参数1: 在x轴上,相对于谁 一个是相对于父亲(Animation.RELATIVE_TO_PARENT很少用到) 一个是相对于自己(Animation.RELATIVE_TO_SELF经常用到),参数2: 在x轴上从什么位置开始, 参数3: 在x轴上到什么位置结束 . 剩下的参数就是y轴上的设置了
- TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 参数1:在x轴上从什么位置开始,参数2:在x轴上到什么位置结束 参数3.4 分别表示y轴
首先定义个平移动画
TranslateAnimation translateAnimation = new TranslateAnimation(-10, 10,0,0);//在x轴上从-10px到10px
translateAnimation.setDuration(100);//执行动画时间100ms
translateAnimation.setRepeatCount(3);//执行次数 3次
直接引用平移动画即可
indViewById(R.id.tv).startAnimation(translateAnimation); // 开始动画