Android 动画:逐帧动画,补间动画和属性动画 | 您所在的位置:网站首页 › 补间动画的制作方法 › Android 动画:逐帧动画,补间动画和属性动画 |
1.三种动画的介绍
现在 Android 常用的动画有三种: 逐帧动画,补间动画和属性动画; FrameAnimation(逐帧动画):将多张图片组合起来进行播放,很多 App 的加载动画是采用这种方式(如:美团,去哪儿)。 TweenAnimation(补间动画):补间动画由 Animation 类来实现具体效果,包括平移(TranslateAnimation)、缩放(ScaleAnimation)、旋转(RotateAnimation)、透明度(AlphaAnimation)四个子类,四种变化,但补间动画只是达到了其视觉效果,并不是真正的位置上的变化。(属性动画出来之后,补间动画就不那么常用了)。 PropertyAnimation(属性动画):最为强大的动画,弥补了补间动画的缺点,实现位置+视觉的变化。下面重点讲解属性动画: 动画名称描述注释代码实例透明度透明度由0~1表示。 0表示完全透明, 1表示不透明 view:执行动画的View; "alpha":表示透明动画; 1f:起始透明度; 0f:动画结束后的透明度;//变为透明, //动画时间为两秒并开始 ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 0f); animator.setDuration(2000); animator.start(); 旋转下个度数大于上个度数, 顺时针旋转; 下个度数小于上个度数, 逆时针旋转。 view:执行动画的View; "rotation":表示旋转动画 0f => 360f ,顺时针; 360f => 0f,逆时针; //先顺时针,再逆时针, //动画时间为两秒并开始 ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f,360f, 0f); animator.setDuration(2000); animator.start(); 移动translationX: 下个位置大于上个上个位置时, 向右移动,反之向左移动; translationY: 下个位置大于上个上个位置时, 向下移动,反之向上移动。 view:执行动画的View; "translationX":在x轴移动 "translationY":在y轴移动 translationX: 0f=> -300f,向左; -300f=> 0f,向右。 //先向左移动,再向右移动, //动画时间为两秒并开始 ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0f, -300f, 0f); animator.setDuration(2000); animator.start(); 缩放后面的参数表示倍数, 1f表示原来的大小,以此推类: 2f表示两倍、3f表示三倍 view:执行动画的View; “scaleX”:沿x轴缩放 "scaleY":沿y轴缩放 //先沿x轴放大两倍,再缩小回去, //动画时间为两秒并开始 ObjectAnimator animator = ObjectAnimator.ofFloat(view, "scaleX", 1f, 2f, 1f); animator.setDuration(2000); animator.start(); 组合动画类AnimatorSet, 专门来组合这些动画。 after(Animator anim) : 将现有动画插入到传入的动画之后执行 after(long delay) : 将现有动画延迟指定毫秒后执行 before(Animator anim): 将现有动画插入到传入的动画之前执行 with(Animator anim) : 将现有动画和传入的动画同时执行 ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "translationX", 600, 0, 0); ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationX", 0, -600, -600); AnimatorSet set = new AnimatorSet(); set.play(animator1).before(animator2); set.setDuration(3000); set.start(); 2.PropertyAnimation(属性动画)实例下面是一个类似弹幕的例子,弹幕从最右边划入,滑到中间停留一下,从最左边划出(做 gif 动图不会。。所以先看下截图) (demo下载地址 https://download.csdn.net/download/wuqingsen1/10644093): 首先初始化两个 ObjectAnimator 和一个 AnimatorSet ,其次利用组合动画来实现,代码如下: public class MainActivity extends AppCompatActivity { private TextView tv_show; private AnimatorSet set = new AnimatorSet(); private ObjectAnimator animator1; private ObjectAnimator animator2; private LinearLayout ll_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_show = findViewById(R.id.tv_show); ll_login = findViewById(R.id.ll_login); tv_show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ll_login.setVisibility(View.VISIBLE); //第一个动画 animator1 和第二个动画 animator2 animator1 = ObjectAnimator.ofFloat(ll_login, "translationX", 800, 0, 0); animator2 = ObjectAnimator.ofFloat(ll_login, "translationX", 0, -800, -800); //先执行第一个动画,再执行第二个动画 set.play(animator1).before(animator2); set.setDuration(3000); set.start(); } }); } } 3.FrameAnimation(逐帧动画)实例下面是三张图片来回切换的例子(做 gif 动图不会。。所以先看下截图) (demo下载地址为https://download.csdn.net/download/wuqingsen1/10644237 ): 建一个名字为 zhuzhen 的 xml 文件,里面内容为: 代码中设置: public class MainActivity extends AppCompatActivity { private ImageView iv_zhuzhen; private AnimationDrawable animationDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_zhuzhen = findViewById(R.id.iv_zhuzhen); // 把动画资源设置为imageView的背景,也可直接在XML里面设置 iv_zhuzhen.setBackgroundResource(R.drawable.zhuzhen); animationDrawable = (AnimationDrawable) iv_zhuzhen.getBackground(); if (animationDrawable != null && !animationDrawable.isRunning()) { animationDrawable.start();//动画开始 } } }
更多好内容戳下面: https://blog.csdn.net/wuqingsen1 |
CopyRight 2018-2019 实验室设备网 版权所有 |