Android动画

    xiaoxiao2021-03-25  85

    Android动画

    在android动画主要分为帧动画,View动画,属性动画。帧动画就是通过一帧一帧播放一组图片从而产生动画效果,View动画就是通过不断改变View的绘制位置来产生动画效果,属性动画就是通过改变对象的属性来产生动画效果。下面做一个三个动画的详细介绍。

    一.帧动画

    帧动画就是播放一组图片,适合帧数比较少的动画,不然容易产生OOM。

    1.1通过xml文件实现

    在xml文件定义AnimationDrawable <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@mipmap/img_1" android:duration="500" /> <item android:drawable="@mipmap/img_2" android:duration="500" /> <item android:drawable="@mipmap/img_3" android:duration="500" /> </animation-list>

    oneshot是表示是否重复播放,drawbable是表示播放的图片,duration是表示时间,单位是毫秒

    将定义的AnimationDrawable作为View的背景,然后播放AnimationDrawable动画。 imageView.setImageResource(R.drawable.img_animlist); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.start();

    1.2通过存Java代码实现

    AnimationDrawable anim = new AnimationDrawable(); for (int i = 1; i <= 3; i++) { int id = getResources().getIdentifier("img_" + i, "mipmap", getPackageName()); Drawable drawable = getResources().getDrawable(id); anim.addFrame(drawable, 500); } anim.setOneShot(false); imageView.setImageDrawable(anim); anim.start();

    二.View动画

    View动画主要支持平移动画,缩放动画,旋转动画,透明动画四种动画效果,是一种渐进式动画,而且View动画支持自定义。View支持的四种动画中,不仅可以用xml来定义,还可以用纯Java代码实现。

    2.1 xml文件实现View动画

    1). 在res文件下创建一个anim的文件夹,然后在anim文件创建一个XML文件,路径类似res/anim/anim_view.xml。

    2). 在anim_view里面定义动画属性

    <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true" android:interpolator="@android:anim/accelerate_interpolator" > <alpha android:fromAlpha="1" android:toAlpha="0.8"/> <scale android:fromXScale="1" android:toXScale="0.8" android:fromYScale="1" android:toYScale="0.8" android:pivotX="0.5" android:pivotY="0.5"/> <translate android:fromXDelta="100" android:fromYDelta="100" android:toXDelta="200" android:toYDelta="200"/> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotY="0.5" android:pivotX="0.5"/> </set>

    <set>标签表示动画集合,它可以包含若干个动画。它主要有两个属性:

    android:shareInterpolator :表示集合中的动画是否合集合共享一个插值器,如果集合不指定插值器,那么子动画需要单独指定所需的插值器或者使用默认值。android:interpolator :表示动画集合使用的插值器。可以不指定,但是有默认值,默认为@android:anim/decelerate_interpolator加速减速插值器。

    <translate>标签表示的时候平移动画,可以完成在x轴跟y轴完移动。它主要有几个属性

    android:fromXDelta:表示x的起始值。android:fromYDelta:表示y的起始值。android:toXDelta:表示x的结束值。android:toYDelta:表示y的结束值。

    <scale>标签表示缩放动画,可以完成放大缩小的动画效果。它主要有几个属性

    android:fromXScale:x轴方向缩放的起始值android:fromYScale:y轴方向缩放的起始值 android:toXScale: x轴方向缩放的结束值android:toYScale:y轴方向缩放的结束值android:pivotX: 缩放轴点的x坐标android:pivotY:缩放轴点点y坐标

    <rotate>标签表示旋转动画,看可以完成围绕轴点进行旋转的动画效果

    android:fromDegrees :旋转起始的角度android:toDegrees :旋转结束的角度android:pivotY :旋转轴点的x坐标android:pivotX :旋转轴点点y坐标

    <rotate>标签表示透明度动画,可以完成透明度变化的动画效果

    android:fromAlpha= :透明度的起始值android:toAlpha :透明度的结束值

    四种标签动画除了以上几个属性外,还有几个共同属性

    android:duration:持续的时间android:fillAfater:动画结束后是否停止在结束的位置。android:fillBefore:动画结束后是否停止在开始的位置。android:repeatCount:重复的次数android:interpolator :插值器

    3).使用AnimationUtils加载动画然后通过View开启动画

    ImageView imageView=findViewById(R.id.img) Animation animtion = AnimationUtils.loadAnimation(this, R.anim.view_anim_translate); imageView.startAnimation(animtion);

    2.2 通过纯Java代码实现View动画

    平移动画 TranslateAnimation animtion = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 100, Animation.RELATIVE_TO_SELF, 100, Animation.RELATIVE_TO_SELF, 200, Animation.RELATIVE_TO_SELF, 200; animtion.setDuration(500); imageView.startAnimation(animtion);

    Animation.RELATIVE_TO_PARENT:相对于父控件身的坐标 Animation.RELATIVE_TO_SELF:相对于自身的坐标

    缩放动画 ScaleAnimation animtion = new ScaleAnimation( 1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animtion(500); imageView.startAnimation(scaleAnimation); 旋转动画 RotateAnimation animtion = new RotateAnimation( 0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animtion(500); imageView.startAnimation(rotateAnimation); 透明度动画 AlphaAnimation animtion = new AlphaAnimation(1, 0); animtion.setDuration(500); imageView.startAnimation(alphaAnimation);

    三.属性动画

    属性动画是android API 11 才加入的特性,它主要就通过改变对象的属性值来实现动画效果,不仅能完成View动画的效果外,还能实现更加炫酷的动画,几乎无所不能。如果想要在低版本使用属性动画,就需要使用属性动画兼容库 Nineoldandoids , Nineoldandoids 本质上是通过 View 动画实现的,但是效果看起来就跟属性动画一样,而且 API 使用也跟属性动画一模一样。下面来看下属性动画的用法。

    ValueAnimator

    ValueAnimator是整个属性动画中最核心的一个点,整个属性动画过渡的计算都是由ValueAnimator来执行,同时还负责动画的播放次数、播放模式、动画设置监听器等。

    用法

    ValueAnimator anim = ValueAnimator.ofFloat(0f, 0.8f); anim.setDuration(500); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float currentValue = (float) animation.getAnimatedValue(); Log.d("ValueAnimator ",currentValue); } }); anim.start();

    为什么 跟之前的View的动画不一样,没有太多缩放,旋转等类,只有很简单的几句代码?这就是属性动画的魅力之一所在,我们可以根据ValueAnimator计算出来的值进行各种实现各种想要的动画,显得更加灵活。

    ObjectAnimator

    ObjectAnimator是继承于ValueAnimator,也是属性动画中使用最频繁的一个类,它可以直接对任意对象的任意属性进行动画操作。它的使用跟ValueAnimator很相似。

    使用

    ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f); animator.setDuration(500); animator.start();

    这里我们对参数进行解释一下:

    第一个参数需要传入需要进行动画的操作的对象。

    第二个参数是对象中需要改变的属性第三个

    后面几个参数是不固定的,需要改变什么值就传入什么值。

    刚才我们说ObjectAnimator可以对任何对象的属性的操作,由此可以得出,不单单alpha的属性可以改变,rotation,translationX,translationY,scaleX,scaleY等属性都是可以改变,然后达到动画效果。举个例子:

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

    这个可以让imageView在X轴上放大2倍。

    ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 180f); animator.setDuration(500); animator.start();

    这个可以让imageView旋转180°。

    大家举一反三就做出其他很多动画效果出来 。

    当然我们还可以进行动画组合:

    ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f); ObjectAnimator animator2 = ObjectAnimator.ofFloat(textview, "rotation", 0f, 180f); ObjectAnimator animator3 = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f); AnimatorSet animatorSet = new AnimatorSet(); animatorSet .play(animator1).with(animator2 ).after(animator3); animSet.setDuration(500); animSet.start();

    这样就同时执行三个动画效果了。

    好了动画基本就讲得差不多了,其实属性动画也是通过XML文件进行实现,但是实际开发中还是建议用代码进行实现,一方面更加简单,另外一方面也更加灵活,所以这里就不细讲了。这篇文章也只是讲了Android动画的基本使用,Android动画的知识点还远远不止这些,还有插值器,View动画的特殊使用场景等等,大家有兴趣可以去学习学习。

    转载请注明原文地址: https://ju.6miu.com/read-34466.html

    最新回复(0)