当前位置: 代码网 > it编程>App开发>Android > Android 缩放动画 ScaleAnimation的使用小结

Android 缩放动画 ScaleAnimation的使用小结

2024年05月15日 Android 我要评论
什么是scaleanimationscaleanimation即缩放动画,应用场景特别多,比如常见的隐藏菜单点击显示下面我分两种方式来介绍scaleanimation如何使用。1. xml文件形式文件

什么是scaleanimation

scaleanimation即缩放动画,应用场景特别多,比如常见的隐藏菜单点击显示

下面我分两种方式来介绍scaleanimation如何使用。

1. xml文件形式

文件名:anim_scale_in.xml
效果:呈现view放大显示效果
源码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="1000"
        android:fillafter="true"
        android:fromxscale="0.0"
        android:fromyscale="0.0"
        android:pivotx="50%"
        android:pivoty="50%"
        android:toxscale="1.0"
        android:toyscale="1.0" />
</set>

属性解释:
interpolator:动画插入器,该功能在xml里设置貌似无效,需在代码中加
fromxscale:从自身x轴长度多少倍开始缩放,如:fromxscale= 0.5表示从自身x轴长度0.5倍开始缩放
toxscale:缩放到自身x轴长度多少倍结束,如:toxscale = 2.0表示x轴缩放到自身x轴长度2倍结束
上面两条意思就是:该view的x轴从自身x轴长度的0.5倍开始缩放到自身x轴长度的2倍结束
fromyscale:从自身y轴长度多少倍开始缩放,如:fromyscale= 0.5表示从自身y轴长度0.5倍开始缩放
toyscale:缩放到自身y轴长度多少倍结束,如:toyscale = 2.0表示x轴缩放到自身y轴长度2倍结束
pivotx:动画相对于控件x坐标的开始位置
pivoty:动画相对于控件y坐标的开始位置
如:pivotx = 50%,pivoty = 50% 表示从该控件的中心开始缩放

   //表示控件左下角开始
   android:pivotx="0"
   android:pivoty="100%"
   //表示控件左上角开始
   android:pivotx="0"
   android:pivoty="0"
   //表示控件右下角开始
    android:pivotx="100%"
    android:pivoty="100%"
   //表示控件右上角开始
   android:pivotx="100%"
   android:pivoty="0"
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="1000"
        android:fillafter="true"
        android:fromxscale="1.0"
        android:fromyscale="1.0"
        android:pivotx="50%"
        android:pivoty="50%"
        android:toxscale="0"
        android:toyscale="0" />
</set>

ok,现在有了xml布局文件,我们需要用java代码让他工作起来,如下;

 /**
     * 缩放变大动画
     *
     * @param context
     * @param view 目标view
     */
    public static void startscaleinanim(context context, view view) {
        animation animation = animationutils.loadanimation(context, r.anim.anim_scale_in);
        if (view != null)
            view.startanimation(animation);
    }
    /**
     * 缩放缩小动画
     *
     * @param context
     * @param view 目标view
     */
    public static void startscaleoutanim(context context, view view) {
        animation animation = animationutils.loadanimation(context, r.anim.anim_scale_out);
        if (view != null)
            view.startanimation(animation);
    }

我单独封装在一个动画工具类中,哪里需要就哪里调用。

下面看看代码的执行效果:

缩放同时还可以添加透明度变化,如下:

放大+淡入:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="1000"
        android:fillafter="true"
        android:fillenabled="true"
        android:fromxscale="0"
        android:fromyscale="0"
        android:pivotx="50%"
        android:pivoty="50%"
        android:toxscale="1.0"
        android:toyscale="1.0" />
    <!--同时配置淡入功能-->
    <alpha
        android:duration="700"
        android:fillafter="true"
        android:fromalpha="0"
        android:toalpha="1" />
</set>

缩小+淡出

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="1000"
        android:fillafter="true"
        android:fromxscale="1.0"
        android:fromyscale="1.0"
        android:pivotx="50%"
        android:pivoty="50%"
        android:toxscale="0"
        android:toyscale="0" />
    <!--同时配置淡出功能-->
    <alpha
        android:duration="700"
        android:fillafter="true"
        android:fromalpha="1"
        android:toalpha="0" />
</set>

效果如下:

下拉菜单显示与收回,效果:

显示:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="300"
        android:fillafter="true"
        android:fillenabled="true"
        android:fromxscale="1.0"
        android:fromyscale="0"
        android:pivotx="100%"
        android:pivoty="0"
        android:toxscale="1.0"
        android:toyscale="1.0" />
    <!--同时配置淡入功能-->
    <alpha
        android:duration="300"
        android:fillafter="true"
        android:fromalpha="0"
        android:toalpha="1" />
</set>

收起:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="300"
        android:fillafter="true"
        android:fromxscale="1.0"
        android:fromyscale="1.0"
        android:pivotx="100%"
        android:pivoty="0"
        android:toxscale="1.0"
        android:toyscale="0" />
    <!--同时配置淡出功能-->
    <alpha
        android:duration="300"
        android:fillafter="true"
        android:fromalpha="1"
        android:toalpha="0" />
</set>

效果:

缩放下拉与收回效果:

显示:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/decelerate_interpolator"
        android:duration="200"
        android:fillafter="true"
        android:fillenabled="true"
        android:fromxscale="0"
        android:fromyscale="0"
        android:pivotx="100%"
        android:pivoty="0"
        android:toxscale="1.0"
        android:toyscale="1.0" />
    <!--同时配置淡入功能-->
    <alpha
        android:duration="200"
        android:fillafter="true"
        android:fromalpha="0"
        android:toalpha="1" />
</set>

收起:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="200"
        android:fillafter="true"
        android:fromxscale="1.0"
        android:fromyscale="1.0"
        android:pivotx="100%"
        android:pivoty="0"
        android:toxscale="0"
        android:toyscale="0" />
    <!--同时配置淡出功能-->
    <alpha
        android:duration="200"
        android:fillafter="true"
        android:fromalpha="1"
        android:toalpha="0" />
</set>

效果:

类似游戏按钮的按下放大再还原效果:

  public static void animscalein(view view){
        //缩放动画
        scaleanimation animation = new scaleanimation(1,1.2f,1,1.2f,animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);
        animation.setduration(100);
        animation.setfillafter(true);
        animation.setrepeatmode(animation.reverse);
        animation.setrepeatcount(1);
        //透明度动画
        alphaanimation animation1 = new alphaanimation(1,0.8f);
        animation1.setduration(100);
        animation1.setrepeatcount(1);
        animation1.setrepeatmode(animation.reverse);
        animation1.setfillafter(true);
        //装入animationset中
        animationset set = new animationset(true);
        set.addanimation(animation);
        set.addanimation(animation1);
        if (view != null)
        view.startanimation(set);
    }

效果如下:

备注:由于我的图片是导出视频再用ps转换成的gif,故效率上有所损失,实际动画效果和速度比图片的快。

到此这篇关于android 缩放动画 scaleanimation的文章就介绍到这了,更多相关android scaleanimation内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com