天天看點

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

繼續閱讀