zoukankan      html  css  js  c++  java
  • Android View动画

      Animation框架定义了透明度(AlphaAnimation)、旋转(RotateAnimation)、缩放(ScaleAnimation)和位移(TranslateAnimation)几种常见的动画,并提供了AnimationSet动画集合。实现原理是每次绘图时View所在的ViewGroup中的dispathDraw,流程如下图:
    图片

    可以看下ViewGroup的drawChild方法,这里开始处理动画机制

    /**
     * Draw one child of this View Group. This method is responsible for getting
     * the canvas in the right state. This includes clipping, translating so
     * that the child's scrolled origin is at 0, 0, and applying any animation
     * transformations.
     *
     * @param canvas The canvas on which to draw the child
     * @param child Who to draw
     * @param drawingTime The time at which draw is occurring
     * @return True if an invalidate() was issued
     */
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        return child.draw(canvas, this, drawingTime);
    }
    

    1、透明度动画

    • 代码实现:
    AlphaAnimation animation = new AlphaAnimation(0, 1);// 透明度0变化到透明度为1
    animation.setDuration(1000);// 动画执行时间1s
    textView.startAnimation(animation);
    
    • xml实现:
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <alpha
            android:duration="1000"
            android:fromAlpha="0"
            android:interpolator="@android:anim/accelerate_interpolator"
            android:repeatCount="3"
            android:fillAfter="true"
            android:repeatMode="restart"
            android:toAlpha="1" />
    </set>
    
    
    参数 说明
    fromAlpha 动画开始时候透明度   0.0表示完全透明
    toAlpha 动画结束时候透明度   1.0表示完全不透

    2、旋转动画

    • 代码实现:
    RotateAnimation animation = new RotateAnimation(0, 360, 100, 100);
    animation.setDuration(1000);
    animation.setFillAfter(true); // 设置保持动画最后的状态
    animation.setInterpolator(new AccelerateInterpolator()); // 设置插入器
    textView.startAnimation(animation);
    

    还可以通过系统提供参数来控制动画

    new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    
    • xml实现:
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromDegrees="0.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50.0%"
        android:pivotY="50.0%"
        android:repeatCount="infinite"
        android:toDegrees="360.0" />
    
    参数 说明
    fromDegrees 为动画起始时的旋转角度,此角度是当前为0及360,设置其他值则先跳至该角度的位置再由from - to的值: 负则正向转,正则反向转
    toDegrees 为动画旋转到的角度
    pivotXType 为动画在X轴相对于物件位置类型
    pivotXValue 为动画相对于物件的X坐标的开始位置,此值是以本身原始位置为原点,即如设为20%p,则向右移动父控件的20%位移,为负数则向左移
    pivotYType 为动画在Y轴相对于物件位置类型
    pivotYValue 为动画相对于物件的Y坐标的开始位置,此值是以本身原始位置为原点,即如设为20%p,则向下移动父控件的20%位移,为负数则向上移

    3、位移动画

    • 代码实现:
    TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
    translateAnimation.setDuration(1000);
    textView.startAnimation(translateAnimation);
    
    • xml实现:
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <translate
            android:duration="1000"
            android:fillAfter="true"
            android:fromXDelta="0"
            android:fromYDelta="0"
            android:repeatCount="3"
            android:toXDelta="200"
            android:toYDelta="000" />
    </set>
    
    参数 说明
    fromXDelta 为动画起始时 X坐标上的移动位置
    toXDelta 为动画结束时 X坐标上的移动位置
    fromYDelta 为动画起始时Y坐标上的移动位置
    toYDelta 为动画结束时Y坐标上的移动位置

    4、缩放动画

    • 代码实现:
    ScaleAnimation animation = new ScaleAnimation(0,1,0,1);
    animation.setDuration(1000);
    textView.startAnimation(animation);
    

    缩放动画也可以设置缩放的中心点

    ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    
    • xml实现:
    <set xmlns:android="http://schemas.android.com/apk/res/android">
    
        <scale
            android:duration="1000"
            android:fillAfter="true"
            android:fillBefore="true"
            android:fromXScale="0.0"
            android:fromYScale="0.0"
            android:interpolator="@android:anim/linear_interpolator"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatCount="-1"
            android:repeatMode="reverse"
            android:startOffset="2000"
            android:toXScale="1.0"
            android:toYScale="1.0"/>
    </set>
    
    参数 说明
    fromX 为动画起始时 X坐标上的伸缩尺寸  0.0表示收缩到没有
    toX 为动画结束时 X坐标上的伸缩尺寸   1.0表示正常无伸缩
    fromY 为动画起始时Y坐标上的伸缩尺寸  值小于1.0表示收缩
    toY 为动画结束时Y坐标上的伸缩尺寸   值大于1.0表示放大
    pivotXType 为动画在X轴相对于物件位置类型
    pivotXValue 为动画相对于物件的X坐标的开始位置
    pivotXType 为动画在Y轴相对于物件位置类型
    pivotYValue 为动画相对于物件的Y坐标的开始位置

    5、anim文件使用

    Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
    textView.startAnimation(animation);
    

    6、动画集合

    AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,如同时播放,顺序播放等。

    AnimationSet animationSet = new AnimationSet(true);
    animationSet.setDuration(1000);
    
    AlphaAnimation alpha=new AlphaAnimation(0,1);
    alpha.setDuration(1000);
    animationSet.addAnimation(alpha);
    
    TranslateAnimation translate = new TranslateAnimation(100, 200, 0, 200);
    translate.setDuration(1000);
    animationSet.addAnimation(translate);
    textView.startAnimation(animationSet);
    

    7、动画监听

    对于动画事件,Android也提供了开始、结束和重复事件的监听方法。

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
    
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {
    
        }
    });
    
  • 相关阅读:
    用户控件被添加到容器的整个处理过程
    c#项目后期生成事件命令行常用命令
    Sass 系统知识
    非 root 用户
    Linux 系统版本查看
    Docker Swarm
    Docker Compose
    Linux RocketMQ双主双从,Centos7 RocketMQ4集群(双主双从)
    Linux RocketMQ安装配置和使用,Centos7 RocketMQ4安装配置
    Springboot Logback日志使用,Springboot Logback详细配置和日志分割
  • 原文地址:https://www.cnblogs.com/fomin/p/9667999.html
Copyright © 2011-2022 走看看