广告
返回顶部
首页 > 资讯 > 移动开发 >Android之 动画总结
  • 332
分享到

Android之 动画总结

android动画 2023-09-27 12:09:36 332人浏览 八月长安
摘要

一 动画种类 1 动画在Android中运用也非常广泛,如点击按钮,加载框,Activity的转场等都有动画的身影 2 常用的动画有以下以下几种 逐帧动画【Frame Animation】,即顺序播放事先准备的图片 补间动画【Tween A

一 动画种类

1 动画在Android中运用也非常广泛,如点击按钮,加载框,Activity的转场等都有动画的身影

2 常用的动画有以下以下几种

逐帧动画【Frame Animation】,即顺序播放事先准备的图片

补间动画【Tween Animation】,View的动画效果可以实现简单的平移、缩放、旋转。

属性动画【Property Animation】,补间动画增强版,支持对对象执行动画。

过渡动画【Transition Animation】,实现Activity或View过渡动画效果。包括5.0之后的MD过渡动画等。

二 逐帧动画 

1 定义一个动画xml,设置图片集合

                    

2 java设置动画

//定义组件ImageVIew ivImage = findViewById(R.id.iv_refresh_header);//开始动画ivImage.setImageResource(R.drawable.anim_loading);mAnimationDrawable = (AnimationDrawable) ivImage.getDrawable();mAnimationDrawable.start();//停止动画ivImage.clearAnimation();if (mAnimationDrawable != null){mAnimationDrawable.stop();}

三 补间动画

1 补间动画种类

透明度变化,大小缩放变化,位移变化,旋转变化

2 透明度动画,两种定义方式

xml定义

       

java定义

AlphaAnimation alpha = new AlphaAnimation(0, 1); alpha.setDuration(500);          //设置持续时间 alpha.setFillAfter(true);                   //动画结束后保留结束状态 alpha.setInterpolator(new AccelerateInterpolator());        //添加差值器 ivImage.setAnimation(alpha);

3 大小缩放动画,两种定义方式

xml定义

       

java定义

ScaleAnimation scale = new ScaleAnimation(1.0f, scaleXY, 1.0f, scaleXY, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scale.setDuration(durationMillis); scale.setFillAfter(true); ivImage.setAnimation(scale);

4 位移动画,两种定义方式

xml定义

       

Java定义

TranslateAnimation translate = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta); translate.setDuration(durationMillis); translate.setFillAfter(true); ivImage.setAnimation(translate);

5 旋转动画,两种定义方式

xml定义

       

java定义

RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotate.setDuration(durationMillis); rotate.setFillAfter(true); ivImage.setAnimation(rotate);

6 组合动画AnimationSet

RelativeLayout rlRoot = (RelativeLayout) findViewById(R.id.rl_root);//旋转动画RotateAnimation animRotate = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);animRotate.setDuration(1000);// 动画时间animRotate.setFillAfter(true);// 保持动画结束状态//缩放动画ScaleAnimation animScale = new ScaleAnimation(0, 1, 0, 1,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);animScale.setDuration(1000);animScale.setFillAfter(true);// 保持动画结束状态//渐变动画AlphaAnimation animAlpha = new AlphaAnimation(0, 1);animAlpha.setDuration(2000);// 动画时间animAlpha.setFillAfter(true);// 保持动画结束状态//动画集合AnimationSet set = new AnimationSet(true);set.addAnimation(animRotate);set.addAnimation(animScale);set.addAnimation(animAlpha);//启动动画rlRoot.startAnimation(set);

三 属性动画

1 xml+java调用方式

定义animator.xml

       

    java调用

Button button = (Button) findViewById(R.id.button); Animator mAnim = AnimatorInflater.loadAnimator(this, R.animator.animator); mAnim.setTarget(button); mAnim.start();

2 纯java方式

ObjectAnimator mAnimator = ObjectAnimator.ofFloat(view, type, start, end); // 设置动画重复播放次数 = 重放次数+1 // 动画播放次数 = infinite时,动画无限重复 mAnimator.setRepeatCount(ValueAnimator.INFINITE); // 设置动画运行的时长 mAnimator.setDuration(time); // 设置动画延迟播放时间 mAnimator.setStartDelay(0); // 设置重复播放动画模式 mAnimator.setRepeatMode(ValueAnimator.RESTART); // ValueAnimator.RESTART(默认):正序重放 // ValueAnimator.REVERSE:倒序回放 //设置差值器 mAnimator.setInterpolator(new LinearInterpolator()); return mAnimator; 

3 ValueAnimator值动画,主要负责值的计算和过度,以及动画的播放次数、播放模式和动画监听等

ValueAnimator animator = ValueAnimator.ofFloat(1, 0.5f, 1);animator.setDuration(3000);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {    @Override    public void onAnimationUpdate(ValueAnimator animation) {        float value = (Float) animation.getAnimatedValue();        imageView.setScaleX(value);    }});animator.start();

4 ObjectAnimator对象动画,继承ValueAnimator,可以直接修改对象的属性

ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "scaleX", 1, 0.5f, 1);animator.setDuration(3000);animator.start();

5 PropertyValuesHolder,用来保存属性的值

PropertyValuesHolder xHolder = PropertyValuesHolder.ofFloat("scaleX", 1.0f, 0.5f, 1.0f);PropertyValuesHolder yHolder = PropertyValuesHolder.ofFloat("scaleY", 1.0f, 0.5f, 1.0f);ObjectAnimator.ofPropertyValuesHolder(imageView, xHolder, yHolder)        .setDuration(3000)        .start();

6 AnimatorSet,实现多个动画效果

ObjectAnimator xAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.5f, 1.0f);ObjectAnimator yAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.5f, 1.0f);AnimatorSet animator = new AnimatorSet();animator.playTogether(xAnimator, yAnimator);animator.setDuration(3000);animator.start();

AnimatorSet还可以指定动画的顺序,调用playSequentially()方法依次播放动画 

ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.5f, 1.0f);animator1.setDuration(2000);ObjectAnimator xAnimator2 = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.5f, 1.0f);ObjectAnimator yAnimator2 = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.5f, 1.0f);AnimatorSet scaleAnimator = new AnimatorSet();scaleAnimator.playTogether(xAnimator2, yAnimator2);scaleAnimator.setDuration(2000);ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);animator3.setDuration(2000);AnimatorSet animator = new AnimatorSet();animator.playSequentially(animator1, scaleAnimator, animator3);animator.start();

7 Evaluator估值器,告诉动画系统如何从初始值过度到结束值

  • IntEvaluator :用于计算int类型属性值的计算器
  • FloatEvaluator :用于计算float类型属性值的计算器
  • ArgbEvaluator :用于计算十六进制形式表示的颜色值的计算器
  • TypeEvaluator:计算器的接口,我们可以实现该接口来完成自定义计算器

四 过渡动画,即Activity转场动画 

1 定义动画xml

        

2 设置style

3 java调用

startActivity(intent);overridePendingTransition(R.anim.bottom_top_anim, R.anim.alpha_hide);finish();overridePendingTransition(R.anim.alpha_show, R.anim.top_bottom_anim);

4 Android5.0之后,Android就自带几种动画特效。 3种转场动画 ,1种共享元素

   三种转场动画

@Requiresapi(api = Build.VERSION_CODES.LOLLIPOP)public void explode(View view) {intent = new Intent(this, TransitionActivity.class);intent.putExtra("flag", 0);startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());}@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)public void slide(View view) {intent = new Intent(this, TransitionActivity.class);intent.putExtra("flag", 1);startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());}@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)public void fade(View view) {intent = new Intent(this, TransitionActivity.class);intent.putExtra("flag", 2);startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());}

共享动画

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)public void share(View view) {View fab = findViewById(R.id.fab_button);intent = new Intent(this, TransitionActivity.class);intent.putExtra("flag", 3);//创建单个共享    //startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, view, "share")    //            .toBundle());//创建多个共享startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, Pair.create(view, "share"),Pair.create(fab,"fab")).toBundle());}

来源地址:https://blog.csdn.net/qq_29848853/article/details/131355185

--结束END--

本文标题: Android之 动画总结

本文链接: https://www.lsjlt.com/news/419611.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作