栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android 动画的思考(View体系)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android 动画的思考(View体系)

学习Android的View体系一定要了解甚至熟练运用动画,才能做出优秀的应用,这里强调是View/ViewGroup体系下的动画,因为又新出了Jetpack Compose体系的动画,后续会总结姊妹篇出来。

import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;


public class AnimationUtil {
    

    
    public static void startShakeByViewAnim(View view, float scaleSmall, float scallLarge,float shakeDegrees, long duration) {
        if (view == null) {
            return;
        }

        // 有小变大
        Animation scaleAnim = new ScaleAnimation(scaleSmall, scallLarge, scaleSmall, scallLarge);
        // 从左向右
        Animation rotateAnim = new RotateAnimation(-shakeDegrees, shakeDegrees,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        scaleAnim.setDuration(duration);
        rotateAnim.setDuration(duration / 10);
        rotateAnim.setRepeatMode(Animation.REVERSE);
        rotateAnim.setRepeatCount(10);

        AnimationSet smallAnimationSet = new AnimationSet(false);
        smallAnimationSet.addAnimation(scaleAnim);
        smallAnimationSet.addAnimation(rotateAnim);

        view.startAnimation(smallAnimationSet);
    }

    
    public static void startShakeByPropertyAnim(View view, float scaleSmall, float scallLarge, float shakeDegrees, long duration) {
        if (view == null) {
            return;
        }

        // 先变小后变大
        PropertyValuesHolder scaleXValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_X,
                Keyframe.ofFloat(0f,1.0f),
                Keyframe.ofFloat(0.25f,scaleSmall),
                Keyframe.ofFloat(0.5f,scallLarge),
                Keyframe.ofFloat(0.75f,scallLarge),
                Keyframe.ofFloat(1.0f,1.0f)
                );
        PropertyValuesHolder scaleYValuesHolder = PropertyValuesHolder.ofKeyframe(View.SCALE_Y,
                Keyframe.ofFloat(0f,1.0f),
                Keyframe.ofFloat(0.25f,scaleSmall),
                Keyframe.ofFloat(0.5f,scallLarge),
                Keyframe.ofFloat(0.75f,scallLarge),
                Keyframe.ofFloat(1.0f,1.0f)
                );

        // 先往左再往右
        PropertyValuesHolder rotatevaluesHolder = PropertyValuesHolder.ofKeyframe(View.ROTATION,
                Keyframe.ofFloat(0f,0f),
                Keyframe.ofFloat(0.1f,-shakeDegrees),
                Keyframe.ofFloat(0.2f,shakeDegrees),
                Keyframe.ofFloat(0.3f,-shakeDegrees),
                Keyframe.ofFloat(0.4f,shakeDegrees),
                Keyframe.ofFloat(0.5f,-shakeDegrees),
                Keyframe.ofFloat(0.6f,shakeDegrees),
                Keyframe.ofFloat(0.7f,-shakeDegrees),
                Keyframe.ofFloat(0.8f,shakeDegrees),
                Keyframe.ofFloat(0.9f,-shakeDegrees),
                Keyframe.ofFloat(1.f,0f)
        );

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scaleXValuesHolder,
                scaleYValuesHolder,rotatevaluesHolder);
        objectAnimator.setDuration(duration);
        objectAnimator.start();
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/458778.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号