Android 實(shí)現(xiàn)人臉識別檢測時(shí)掃描以及二維碼掃描動(dòng)畫效果

App現(xiàn)在二維碼掃描、人臉掃描的場景越來越多,掃描的動(dòng)畫效果實(shí)則就是平移動(dòng)畫:TranslateAnimation
現(xiàn)在我呢,用TranslateAnimation實(shí)現(xiàn)一個(gè)人臉掃描的效果,上下來回滑動(dòng)(二維碼只要替換一下BG即可):
(超過5M的gif上傳不了,所以只錄了一個(gè)輪回的,3M多,看著貌似是卡頓,其實(shí)是幀數(shù)太少,具體應(yīng)用到代碼中Run起來看效果)

1.效果圖:

face_scan2.gif

2.Activity代碼如下:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
 
public class MainActivity extends AppCompatActivity {
    private ImageView mIvScan;
    /**
     * 0:從上往下 1:從下往上
     */
    Animation mTop2Bottom, mBottom2Top;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIvScan = findViewById(R.id.scan_line);
 
        mTop2Bottom = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f,
                TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0.7f);
 
        mBottom2Top = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f,
                TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.RELATIVE_TO_PARENT, 0.7f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0f);
 
        mBottom2Top.setRepeatMode(Animation.RESTART);
        mBottom2Top.setInterpolator(new LinearInterpolator());
        mBottom2Top.setDuration(1500);
        mBottom2Top.setFillEnabled(true);//使其可以填充效果從而不回到原地
        mBottom2Top.setFillAfter(true);//不回到起始位置
        //如果不添加setFillEnabled和setFillAfter則動(dòng)畫執(zhí)行結(jié)束后會自動(dòng)回到遠(yuǎn)點(diǎn)
        mBottom2Top.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
 
            }
 
            @Override
            public void onAnimationEnd(Animation animation) {
                mIvScan.startAnimation(mTop2Bottom);
            }
 
            @Override
            public void onAnimationRepeat(Animation animation) {
 
            }
        });
 
        mTop2Bottom.setRepeatMode(Animation.RESTART);
        mTop2Bottom.setInterpolator(new LinearInterpolator());
        mTop2Bottom.setDuration(1500);
        mTop2Bottom.setFillEnabled(true);
        mTop2Bottom.setFillAfter(true);
        mTop2Bottom.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
 
            }
 
            @Override
            public void onAnimationEnd(Animation animation) {
                mIvScan.startAnimation(mBottom2Top);
            }
 
            @Override
            public void onAnimationRepeat(Animation animation) {
 
            }
        });
        mIvScan.startAnimation(mTop2Bottom);
    }
}

3.XML文件,就主要2個(gè)控件,背景圖和實(shí)現(xiàn)動(dòng)畫效果的目標(biāo)ImageView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
    <ImageView
        android:id="@+id/scan_line"
        android:layout_width="250dp"
        android:layout_height="94dp"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        android:src="@mipmap/icon_scan_line" />
 
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:scaleType="centerCrop"
        android:src="@mipmap/bg_frame_face" />
</RelativeLayout>

(布局預(yù)覽)

image.png

Run,看效果。(有好的建議,歡迎評論,一起學(xué)習(xí)交流~_

4:Demo地址

下載地址:https://download.csdn.net/download/u010231454/10847735
轉(zhuǎn)載請注明出處,謝謝~ _http://www.itdecent.cn/p/ff33657a1a6d


作者:碧水逍遙
來源:CSDN
原文:https://blog.csdn.net/u010231454/article/details/84986161
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 了解二維碼這個(gè)東西還是從微信中,當(dāng)時(shí)微信推出二維碼掃描功能,自己感覺挺新穎的,從一張圖片中掃一下竟然能直接加好友,...
    AiPuff閱讀 909評論 0 1
  • android developers系列二(動(dòng)畫) 這里先講一下動(dòng)畫的類型 幀動(dòng)畫 屬性動(dòng)畫 補(bǔ)間動(dòng)畫 下面有詳細(xì)...
    WangYouHu閱讀 587評論 0 1
  • 新版的項(xiàng)目需要添加一個(gè)功能,沒網(wǎng)絡(luò)的時(shí)候提示用戶,有網(wǎng)絡(luò)的時(shí)候提示內(nèi)容自動(dòng)消失,由于項(xiàng)目做了一年多,布局文件較多,...
    gold_tq閱讀 2,290評論 0 1
  • 瑩潔勝如蘭 幽居在山林 雖有串串鈴 何人聽我音
    土撥手作閱讀 244評論 0 0
  • 凡事過去、皆為序幕??愿以後的自己不念過往、不畏將來??
    愛的仙人掌閱讀 229評論 0 2

友情鏈接更多精彩內(nèi)容