天天看点

Android 循环读数字动画实现 一、概述二、实现

一、概述

大家好,这篇文章为大家实现一个读数字的动画,非常简单,安卓动画一共有三种:

视图动画(View Animation),帧动画(Frame Animation),属性动画(Property Animation)

先显示一个效果图(卖家秀~)

二、实现

下面我们使用属性动画中的Object Animator为例:

(1)配置权限

<!-- 相机 -->

<uses-permission android:name="android.permission.CAMERA" />

<!-- 录音 -->

<uses-permission android:name="android.permission.RECORD_AUDIO" />

<!-- 视频 -->

<uses-permission android:name="android.permission.RECORD_VIDEO" />

<!-- 写入内存 -->

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

上面的权限按需求添加即可

(2)编写代码

首先编写一个动画,实现动画监听器,在动画开始时显示数字,根据参数mValidateData的数字进行循环显示。

我们可以在onCreate中点击某个按钮然后调用stratReadNumberAnimation()这个方法;

// 声明变量

private Button btnStart;

private TextView tvVolume;

private ImageView ivNumber;

//这个是第三方的一个相机,如可不用

private CameraPreview cameraPreview; 

private File mVideoFile;

private Boolean isVedioStart = false;

private int count = 0;

private String mValidateData = "123456789";

private int counts = mValidateData.length();

// 开始读数字的动画

private void stratReadNumberAnimation() {

    ObjectAnimator alpha = ObjectAnimator.ofFloat(ivNumber, "alpha", 0.5f, 1.0f);

    ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivNumber, "scaleX", 0.8f, 1.2f);

    ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivNumber, "scaleY", 0.8f, 1.3f);

    AnimatorSet set = new AnimatorSet();

    set.addListener(animatorListener);

    set.setDuration(2000);

    set.playTogether(alpha, scaleX, scaleY);

    set.start();

}

// 动画监听器

private Animator.AnimatorListener animatorListener = new Animator.AnimatorListener() {

    @Override

    public void onAnimationStart(Animator animation) {

        count += 1;

        if (ivNumber.getVisibility() == View.INVISIBLE) {

      ivNumber.setVisibility(View.VISIBLE);

        }

        int number = VideoNumber.readChangedNumber(mValidateData, count);

        ivNumber.setImageResource(number);

    }

    @Override

    public void onAnimationEnd(Animator animation) {

        if (count >= counts) {

            ivNumber.setVisibility(View.INVISIBLE);

            count = 0;

            return;

        }

 //    这里注意一个坑,不能重用那个参数animation,会出现不正确循环

//            animation.reset();

//            animation.setAnimationListener(animationListener2);

//            animation.start();

//重新创建animation循环执行动画

        stratReadNumberAnimation(); 

    }

    @Override

    public void onAnimationCancel(Animator animation) {

    }

    @Override

    public void onAnimationRepeat(Animator animation) {

    }

};

public class VideoNumber {

    public static int readChangedNumber(String mValidateData, int count) {

        count = count - 1; //传入count为次数,count作用为坐标,因为坐标从0开始,所以要减1

        int number = Integer.parseInt(String.valueOf(mValidateData.charAt(count)));

        if (number == 0) {

            return R.drawable.image_num0_gold;

        }

        if (number == 1) {

            return R.drawable.image_num1_gold;

        }

        if (number == 2) {

            return R.drawable.image_num2_gold;

        }

        if (number == 3) {

            return R.drawable.image_num3_gold;

        }

        if (number == 4) {

            return R.drawable.image_num4_gold;

        }

        if (number == 5) {

            return R.drawable.image_num5_gold;

        }

        if (number == 6) {

            return R.drawable.image_num6_gold;

        }

        if (number == 7) {

            return R.drawable.image_num7_gold;

        }

        if (number == 8) {

            return R.drawable.image_num8_gold;

        }

        if (number == 9) {

            return R.drawable.image_num9_gold;

        }

        return -1;

    }

}

至此,一个简单的动画就完成啦~~

三、附加

有了录音读数字,是不是需要获取录音的音量大小呢?完整项目获取了音量大小,大家还可以对音量大小进行显示不同的图标~~

源码地址:https://github.com/18824153269/Animator

继续阅读