Android 动画:使用 AnimatorSet 实现图片放大、移动和复位效果
你可以使用以下代码来实现 AnimatorSet 的效果:
// 创建一个 ImageView 对象并设置初始属性
ImageView imageView = findViewById(R.id.imageView);
imageView.setScaleX(1.0f);
imageView.setScaleY(1.0f);
// 创建一个 AnimatorSet 对象
AnimatorSet animatorSet = new AnimatorSet();
// 创建一个放大图片的动画
ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView, 'scaleX', 5.0f);
ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView, 'scaleY', 5.0f);
scaleXAnimator.setDuration(1000);
scaleYAnimator.setDuration(1000);
// 创建一个垂直移动图片的动画
ObjectAnimator translationYAnimator = ObjectAnimator.ofFloat(imageView, 'translationY', 0, imageView.getHeight());
translationYAnimator.setDuration(1000);
// 创建一个复位图片的动画
ObjectAnimator resetScaleXAnimator = ObjectAnimator.ofFloat(imageView, 'scaleX', 1.0f);
ObjectAnimator resetScaleYAnimator = ObjectAnimator.ofFloat(imageView, 'scaleY', 1.0f);
resetScaleXAnimator.setDuration(1000);
resetScaleYAnimator.setDuration(1000);
// 设置动画的先后顺序和依赖关系
animatorSet.play(scaleXAnimator).with(scaleYAnimator).before(translationYAnimator).before(resetScaleXAnimator).before(resetScaleYAnimator);
// 设置动画执行完后跳转到下一个页面
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// 跳转到下一个页面的代码
}
});
// 启动动画
animatorSet.start();
这里假设你有一个 ImageView 的 id 为 'imageView',你可以根据实际情况修改代码中的 id。动画的执行时间可以根据需要进行调整。在动画执行完后,你可以在 onAnimationEnd 方法中添加页面跳转的代码。
原文地址: https://www.cveoy.top/t/topic/ca8k 著作权归作者所有。请勿转载和采集!