Android中点击图片完成动画效果:以图片中心瞄点,旋转一周,从完全透明变化成完全不透明,从图片中心放大到显示,持续3秒钟
时间: 2024-03-12 16:48:48 浏览: 18
可以使用 `ObjectAnimator` 实现上述动画效果。具体实现步骤如下:
1. 在布局文件中添加一个 `ImageView`,并设置图片资源文件。
2. 在代码中获取 `ImageView` 对象,并使用 `ObjectAnimator` 分别实现旋转和透明度变化的动画效果。
3. 使用 `AnimatorSet` 将两个动画合并,并设置动画时长为3秒钟。
4. 启动动画。
以下是示例代码:
XML布局文件:
```
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_image" />
```
Java 代码:
```
ImageView imageView = findViewById(R.id.image_view);
// 旋转动画
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
rotationAnimator.setDuration(3000);
// 透明度动画
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f);
alphaAnimator.setDuration(3000);
// 缩放动画
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 0f, 1f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 0f, 1f);
AnimatorSet scaleAnimatorSet = new AnimatorSet();
scaleAnimatorSet.playTogether(scaleXAnimator, scaleYAnimator);
scaleAnimatorSet.setDuration(3000);
// 动画集合
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(rotationAnimator)
.with(alphaAnimator)
.before(scaleAnimatorSet);
animatorSet.start();
```
上述代码中,通过 `ObjectAnimator` 对象实现了旋转和透明度变化的动画效果,通过 `AnimatorSet` 合并了两个动画,并设置动画时长为3秒钟。最后启动动画即可。