天天看点

Android view动画之缩放动画

方法一

用 AnimationUtils 和 xml 的方式,加载指定的缩放动画。

Animation scaleAnimation = AnimationUtils.loadAnimation(mContext, R.anim.scale_animation);
scaleAnimation.setFillAfter(true);
mImageView.startAnimation(scaleAnimation);      

scale_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--
        android:fromXScale
        Float. 水平方向缩放比例的初始值,其中1.0是没有任何变化。
        android:toXScale
        Float. 水平方向缩放比例的结束值,其中1.0是没有任何变化。
        android:fromYScale
        Float. 竖直方向缩放比例的初始值,其中1.0是没有任何变化。
        android:toYScale
        Float. 竖直方向缩放比例的结束值,其中1.0是没有任何变化。
        android:pivotX
        Float. 缩放中心点的x坐标
        android:pivotY
        Float. 缩放中心点的y坐标
    -->

    <scale
        android:duration="5000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" />
</set>      
ScaleAnimation scaleAnim = new ScaleAnimation(1, 1.5f, 1,1.5f);
scaleAnim.setFillAfter(true);
mImageView.startAnimation(scaleAnim);      
//利用AnimatorSet和ObjectAnimator实现缩放动画
final AnimatorSet animatorSet = new AnimatorSet();
mImageView.setPivotX(mImageView.getWidth() / 2);
mImageView.setPivotY(mImageView.getHeight() / 2);
animatorSet.playTogether(
                    ObjectAnimator.ofFloat(mImageView, "scaleX", 1, 1.5f).setDuration(5000),
                    ObjectAnimator.ofFloat(mImageView, "scaleY", 1, 1.5f).setDuration(5000));
animatorSet.start();      

继续阅读