安卓 向左下角旋转缩小收起动画怎么做
时间: 2023-10-13 18:15:42 浏览: 43
你可以使用 Android 自带的 `AnimatorSet` 和 `ObjectAnimator` 实现一个向左下角旋转缩小收起的动画。
首先,你需要在布局文件中创建一个需要进行动画的视图。例如:
```xml
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Button" />
```
然后,在代码中获取该视图并设置动画:
```java
Button myButton = findViewById(R.id.myButton);
ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(myButton, "rotation", 0f, -45f);
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(myButton, "scaleX", 1f, 0.5f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(myButton, "scaleY", 1f, 0.5f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(rotateAnimator, scaleXAnimator, scaleYAnimator);
animatorSet.setDuration(500);
animatorSet.start();
```
这段代码将先让视图沿着 Z 轴旋转 -45 度,然后同时沿着 X 轴和 Y 轴缩小到原来的一半大小。你可以根据自己的需求修改旋转角度和缩放比例,同时也可以调整动画的时间长度。
当你需要还原该视图时,你可以使用类似的代码:
```java
ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(myButton, "rotation", -45f, 0f);
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(myButton, "scaleX", 0.5f, 1f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(myButton, "scaleY", 0.5f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(rotateAnimator, scaleXAnimator, scaleYAnimator);
animatorSet.setDuration(500);
animatorSet.start();
```
这段代码将让视图沿着 Z 轴旋转 45 度,然后同时沿着 X 轴和 Y 轴放大到原来的大小。