更多安卓animation示例
时间: 2023-06-29 08:19:29 浏览: 53
好的,下面给你介绍几个常用的View Animation和Property Animation示例:
1. View Animation - 平移动画
```
Animation animation = new TranslateAnimation(0, 100, 0, 100);
animation.setDuration(1000);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
view.startAnimation(animation);
```
这段代码实现了一个平移动画,将View从原位置移动到坐标(100,100)的位置,并且动画重复进行,每次动画结束后反向执行动画。
2. View Animation - 旋转动画
```
Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
view.startAnimation(animation);
```
这段代码实现了一个旋转动画,将View从原位置顺时针旋转360度,并且动画重复进行,每次动画结束后反向执行动画。
3. View Animation - 缩放动画
```
Animation animation = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
view.startAnimation(animation);
```
这段代码实现了一个缩放动画,将View从原大小缩小到原来的一半,并且动画重复进行,每次动画结束后反向执行动画。
4. Property Animation - 透明度动画
```
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 0.0f);
animator.setDuration(1000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
```
这段代码实现了一个透明度动画,将View从完全不透明变为完全透明,并且动画重复进行,每次动画结束后反向执行动画。
5. Property Animation - 平移动画
```
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0f, 100f);
animator.setDuration(1000);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.start();
```
这段代码实现了一个平移动画,将View从原位置移动到x轴坐标为100的位置,并且动画重复进行,每次动画结束后反向执行动画。
以上是几个安卓animation的示例,可以根据自己的需求进行修改和调整。