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)載請附上博文鏈接!