android缩放动画中心缩放_安卓动画缩放调到多少比较好

android缩放动画中心缩放_安卓动画缩放调到多少比较好什么是ScaleAnimationScaleAnimation即缩放动画,应用场景特别多,比如常见的隐藏菜单点击显示下面我分两种方式来介绍ScaleAnimation如何使用。1.xml文件形式文件名:anim_scale_in.xml效果:呈现view放大显示效果源码:<?xmlversion=”1.0″encoding=”utf-8″?><setxmlns:android=”http://schemas.android.com/apk/res/

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

什么是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,故效率上有所损失,实际动画效果和速度比图片的快。

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181716.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Hive实现笛卡尔积

    Hive实现笛卡尔积如下方式可以实现笛卡尔积select*fromaleftjoinbon1=1假设a表100行,b表20万行,则笛卡尔积后为100*20w=2000w行假设a表100行,b表20万*60行,则笛卡尔积后为100*60*20w=12亿行假设a表1000行,b表20万行,则笛卡尔积后为1000*20w=20000w行=2亿行严重影响执行效率…

    2022年7月11日
    17
  • php第三方登录(微博登录,仿照慕课网)

    php第三方登录(微博登录,仿照慕课网)

    2021年10月25日
    44
  • kali 更新源,亲测好用

    kali 更新源,亲测好用debhttp://mirrors.163.com/debian/jessiemainnon-freecontribdebhttp://mirrors.163.com/debian/jessie-updatesmainnon-freecontribdebhttp://mirrors.163.com/debian/jessie-backportsmainnon-f

    2022年5月8日
    44
  • sublime text激活码【2021最新】

    (sublime text激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlML…

    2022年3月20日
    405
  • 远程读取elasticSearch数据库并导出数据「建议收藏」

    远程读取elasticSearch数据库并导出数据「建议收藏」packageorg.elasticsearch.esTest;importjava.awt.List;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.util.ArrayList;importjava

    2022年6月6日
    73
  • 冯诺依曼与哈佛结构的区别[通俗易懂]

    冯诺依曼与哈佛结构的区别[通俗易懂]cortexM3,M4主要采用哈弗结构       个人理解:最主要的区别在于程序空间和数据空间是否是一体的,冯·诺依曼结构数据空间和地址空间是不分开的,而哈佛结构数据空间和地址空间是分开的      哈弗结构的优势:如果采用流水线设计,如三级流水线:取指,译指,…

    2022年10月4日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号