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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Jenkins(3)拉取git仓库代码,执行python自动化脚本[通俗易懂]

    Jenkins(3)拉取git仓库代码,执行python自动化脚本[通俗易懂]前言python自动化的脚本开发完成后需提交到git代码仓库,接下来就是用Jenkins拉取代码去构建自动化代码了新建项目打开Jenkins新建一个自由风格的项目源码管理Repository

    2022年7月31日
    5
  • u8和u16是什么意思_u8在哪里声明的

    u8和u16是什么意思_u8在哪里声明的unsignedint32(C语言标准表达方法)2.uint32_t;3.u32;这三种表达式是同一个意思,只是在不用的版本当中,为了兼容旧版本而出现这么多的表达方式。但不管怎么变化,都是基于标准c。主要用处是为了在定义数据类型的时候少写几个符号。unsignedchar=uint8_t=u8unsignedshortint=…

    2022年10月15日
    3
  • 服务器出现kernel: TCP: time wait bucket table overflow解决

    服务器出现kernel: TCP: time wait bucket table overflow解决http://blog.sina.com.cn/s/blog_69cdee7f0100yowt.html#一台Nginx+php-fcgi的服务器做了负载均衡,在主控端发现一直在报错:viewsourceprint?01#—————————-引用文字-开始———————-

    2022年5月15日
    33
  • NTP时间服务器简介

    NTP(NetworkTimeProtocol)网络时间协议,工作在UDP的123端口上。是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(局域网上与标准间差小于1毫秒,互联网上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP服务器NTP服务器提供准确时间,首先要有准确的时间来源,这一时间…

    2022年4月9日
    58
  • IIS 无法启动:发生意外错误0x8ffe2740 的原因

    IIS 无法启动:发生意外错误0x8ffe2740 的原因发生意外错误0x8ffe2740原因如果系统中存在端口冲突就有可能发生本情况.IIS默认使用80端口进行HTTP通信.如果除IIS外的应用程序正在运行并且正在相同的IP地址上使用80端口,在您试图使用IIS管理器启动网站时您也可能收到该错误讯息.解决方法要解决这个问题,您可以进行以下任一项操作:•在IIS管理器中更改网站绑定端口为除80端口外的其它端口.•停止正在使用80端口…

    2022年7月26日
    6
  • pip 卸载包_linux卸载python3

    pip 卸载包_linux卸载python3使用pip卸载模块pipuninstallbagname

    2022年10月10日
    3

发表回复

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

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