android 动画(三种常见动画基本应用)——篇一

目录

逐帧动画:

补间动画

属性动画:


说起动画,也是Android的一个难点和重点,应用有了动画就是比别人的好用和好看,让人更舒服,现在,一起来屡屡动画的相关知识吧。(本文仅作为个人笔记)


逐帧动画:

代码实现:

private AnimationDrawable mAnimationDrawable;

private ImageView mImageView;

//点击事件触发动画
public void startFrameAnimal(View view){
        mAnimationDrawable = new AnimationDrawable();
        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.icn_4ptz_sel_), 100);
        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.icn_180panorama_sel), 100);
        mAnimationDrawable.addFrame(getResources().getDrawable(R.drawable.icn_360panorama_sel), 100);
        mAnimationDrawable.setOneShot(false);
        mImageView.setBackground(mAnimationDrawable);
        mAnimationDrawable.start();
    }

xml中实现

public void startFrameAnimalFromXml(View view){
        mAnimationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.frame_anim);
        mImageView.setBackground(mAnimationDrawable);
        mAnimationDrawable.start();
    }

 在drawable下,建立一个frame_anim.xml 文件,文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    android:visible="true">

    <item android:drawable="@drawable/icn_4ptz_sel_" android:duration="50" />
    <item android:drawable="@drawable/icn_180panorama_sel" android:duration="50" />
    <item android:drawable="@drawable/icn_360panorama_sel" android:duration="50" />
</animation-list>

 AnimationDrawable 的一些方法解释:

光为AnimationDrawable设置帧还不能完成播放动画的功能,还需要AnimationDrawable定义好的其他的一些方法来操作逐帧动画,下面简单介绍一下AnimationDrawable的常用方法:

  • void start():开始播放逐帧动画。
  • void stop():停止播放逐帧动画。
  • void addFrame(Drawable frame,int duration):为AnimationDrawable添加一帧,并设置持续时间。
  • int getDuration(int i):得到指定index的帧的持续时间。
  • Drawable getFrame(int index):得到指定index的帧Drawable。
  • int getNumberOfFrames():得到当前AnimationDrawable的所有帧数量。
  • boolean isOneShot():当前AnimationDrawable是否执行一次,返回true执行一次,false循环播放。
  • boolean isRunning():当前AnimationDrawable是否正在播放。
  • void setOneShot(boolean oneShot):设置AnimationDrawable是否执行一次,true执行一次,false循环播放

补间动画

代码实现

public static final float ANIMATION_DEFAULT_SIZE = 1.0f;
    public static final float ANIMATION_SCALE = 1.1f;


    public static void zoomOut(View view){
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation animation = new ScaleAnimation(1.0f, ANIMATION_SCALE, 1.0f, ANIMATION_SCALE,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);
        animation.setFillAfter(true);
        animationSet.addAnimation(animation);
        animationSet.setFillAfter(true);
        view.clearAnimation();
        view.startAnimation(animationSet);
    }


    public static void zoomIn(View view){
        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation animation = new ScaleAnimation(ANIMATION_SCALE, 1.0f, ANIMATION_SCALE, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);
        animation.setFillAfter(true);
        animationSet.addAnimation(animation);
        animationSet.setFillAfter(true);
        view.startAnimation(animationSet);

    }

四种常见形式的帧动画,透明度,缩放,平移,旋转;上面是缩放,其他的类似,模仿即可,把ScaleAnimation 换为其他的即可。

xml实现

<?xml version="1.0" encoding="utf-8"?>
// 采用<translate /> 标签表示平移动画
<translate xmlns:android="http://schemas.android.com/apk/res/android"

    // 以下参数是4种动画效果的公共属性,即都有的属性
    android:duration="3000" // 动画持续时间(ms),必须设置,动画才有效果
    android:startOffset ="1000" // 动画延迟开始时间(ms)
    android:fillBefore = “true” // 动画播放完后,视图是否会停留在动画开始的状态,默认为true
    android:fillAfter = “false” // 动画播放完后,视图是否会停留在动画结束的状态,优先于fillBefore值,默认为false
    android:fillEnabled= “true” // 是否应用fillBefore值,对fillAfter值无影响,默认为true
    android:repeatMode= “restart” // 选择重复播放动画模式,restart代表正序重放,reverse代表倒序回放,默认为restart|
    android:repeatCount = “0” // 重放次数(所以动画的播放次数=重放次数+1),为infinite时无限重复
    android:interpolator = @[package:]anim/interpolator_resource // 插值器,即影响动画的播放速度,下面会详细讲
    
    // 以下参数是平移动画特有的属性
    android:fromXDelta="0" // 视图在水平方向x 移动的起始值
    android:toXDelta="500" // 视图在水平方向x 移动的结束值

    android:fromYDelta="0" // 视图在竖直方向y 移动的起始值
    android:toYDelta="500" // 视图在竖直方向y 移动的结束值

    /> 
/ 步骤1:创建 需要设置动画的 视图View AnimationUtils是系统类
        Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
        // 步骤2:创建 动画对象 并传入设置的动画效果xml文件
        mButton.startAnimation(translateAnimation);

属性动画:

  public static void propertityAnimScaleZoom(View view){
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(view, "scaleX", ANIMATION_DEFAULT_SIZE, ANIMATION_SCALE);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(view, "scaleY", ANIMATION_DEFAULT_SIZE, ANIMATION_SCALE);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scaleXAnimator).with(scaleYAnimator);
        animatorSet.setDuration(200);
        animatorSet.start();
    }

    public static void propertityAnimScaleIn(View view){
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(view, "scaleX", ANIMATION_SCALE, ANIMATION_DEFAULT_SIZE);
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(view, "scaleY", ANIMATION_SCALE, ANIMATION_DEFAULT_SIZE);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scaleXAnimator).with(scaleYAnimator);
        animatorSet.setDuration(200);
        animatorSet.start();
    }

属性动画可以改变view的某个属性,而不仅仅针对视图,只要帧动画可以完成的,其都可以实现,其功能是最强大的,是api 11 3.0版本引入的。

这里有个把多种动画组合一起播放的写法:animatorSet.play(scaleXAnimator).with(scaleYAnimator);  其有三个关键方法 with (一起播放),after (之后),before ( 之前); 这三个方法就能够进行组合动画播放了;


文章参考:

动画讲解原理: https://www.jianshu.com/p/733532041f46 (说明和例子很详细)

动画讲解 :https://www.jianshu.com/p/2412d00a0ce4

猜你喜欢

转载自blog.csdn.net/sjh_389510506/article/details/86674798
今日推荐