android中关于动画的理解和使用

2021-12-03  本文已影响0人  雨田Android开发

在android开发过程中为了使用户的交互和体验更流畅和自然,动画已经是应用开发中不可缺少的部分,在Android中动画分类比较多,最早的帧动画,补间动画,3.0后添加了属性动画 5.0又增加了vectorDrawable能够满足用户的各种需求

1、帧动画

帧动画就是frame动画是一系列图片按照一定顺序逐个展示的过程,可以通过xml定义也可以通过代码实现,animation-list定义的动画将顺次从上往下执行

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--oneshot: true只展示一次。
             false不停的循环播放动画。
    android:duration 图片显示时间长度。 -->
    <item
        android:drawable="@drawable/comp1_00000"
        android:duration="25"></item>
    <item
        android:drawable="@drawable/comp1_00001"
        android:duration="25"></item>
    <item
        android:drawable="@drawable/comp1_00002"
        android:duration="25"></item>
    <item
        android:drawable="@drawable/comp1_00003"
        android:duration="25"></item>
    <item
        android:drawable="@drawable/comp1_00004"
        android:duration="25"></item>

</animation-list>

定义完成之后需要将动画设置给View,例如设置为imageview的背景

<ImageView
                android:id="@+id/load_image"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/loading_anim_image" />

此时动画并不会在imageview显示时启动,我们还需要通过代码启动该动画

((AnimationDrawable)imageview.getBackground()).start();

2、补间动画

补间动画就是tween动画,是操作控件让其进行旋转、鉴别、移动、缩放的一种过程,同样可以通过xml形式或者是代码实现,主要是分为AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation、AnimationSet

<set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/decelerate_interpolator"  
    android:shareInterpolator="true" >  
  
    <scale  
        android:duration="2000"  
        android:fromXScale="0.2"  
        android:fromYScale="0.2"  
        android:pivotX="50%"  
        android:pivotY="50%"  
        android:toXScale="1.5"  
        android:toYScale="1.5" />  
  
    <rotate  
        android:duration="1000"  
        android:fromDegrees="0"  
        android:repeatCount="1"  
        android:repeatMode="reverse"  
        android:toDegrees="360" />  
  
    <translate  
        android:duration="2000"  
        android:fromXDelta="0"  
        android:fromYDelta="0"  
        android:toXDelta="320"  
        android:toYDelta="0" />  
  
    <alpha  
        android:duration="2000"  
        android:fromAlpha="1.0"  
        android:toAlpha="0.1" />  

</set>  

fromXScale/fromYScale:沿着X轴/Y轴缩放的起始比例
toXScale/toYScale:沿着X轴/Y轴缩放的结束比例
pivotX/pivotY:缩放的中轴点X/Y坐标,即距离自身左边缘的位置,比如50%就是以图像的 中心为中轴点
fromAlpha :起始透明度
toAlpha:结束透明度
透明度的范围为:0-1,完全透明-完全不透明
fromXDelta/fromYDelta:动画起始位置的X/Y坐标
toXDelta/toYDelta:动画结束位置的X/Y坐标
fromDegrees/toDegrees:旋转的起始/结束角度
repeatCount:旋转的次数,默认值为0,代表一次,假如是其他值,比如3,则旋转4次 另外,值为-1或者infinite时,表示动画永不停止
repeatMode:设置重复模式,默认restart,但只有当repeatCount大于0或者infinite或-1时 才有效。还可以设置成reverse,表示偶数次显示动画时会做方向相反的运动!
使用方法也比较简单

Animation animation = AnimationUtils.loadAnimation(this,
                        R.anim.anim_);
view.startAnimation(animation);

3、属性动画

属性动画不再是针对view设计 也不限定为上面几种简单的动画操作,实际上是一种在一定时间段内不断修改某个对象的属性值的机制
属性动画的核心类ValueAnimator。一半都是通过ofFloat、ofInt等静态工厂函数构建ValueAnimator。例如将数值从0.0到1.0过渡的动画

private void startValueAnimation(){
        ValueAnimator animator = ValueAnimator.ofFloat(0.0f,1.0f);
        animator.setDuration(1000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.e("","属性值:"+animation.getAnimatedValue())
            }
        });
        animator.start();
    }

对任意对象执行指定属性值进行修改使用ValueAnimator的实现类ObjectAnimator

ObjectAnimator objectAnimator = ObjectAnimator.ofObject(mView,"Alpha",0.0f,1.0f);
        objectAnimator.setDuration(1000);
        objectAnimator.start();

4、VectorDrawable

VectorDrawable目的就是用来渲染矢量图,AnimatedVectorDrawable用来播放矢量动画

上一篇 下一篇

猜你喜欢

热点阅读